Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Add session usage reporting callback to TURN database driver#1794

Open
mesibo wants to merge 6 commits intocoturn:masterfrom
mesibo:issue-1793-usage-reporting
Open

Add session usage reporting callback to TURN database driver#1794
mesibo wants to merge 6 commits intocoturn:masterfrom
mesibo:issue-1793-usage-reporting

Conversation

@mesibo
Copy link

@mesibo mesibo commented Jan 18, 2026

We use Coturn in production at mesibo and identified a gap in dbdriver: there's no callback to capture post-session metrics like duration, bandwidth consumed, timestamps, etc.

We've made the following changes, which could be useful for Coturn. Would appreciate your consideration. These changes are tested in production environments at mesibo.

Changes for reporting usage

Added a report_usage callback to the database driver interface, invoked at session termination. This allows drivers to capture and persist detailed usage statistics in their preferred storage (MySQL, PostgreSQL, etc.).

1. Database Driver Interface

File: src/apps/relay/dbdrivers/dbdriver.h

typedef struct _turn_dbdriver_t {
    // ... existing callbacks
    void (*report_usage)(void *);
} turn_dbdriver_t;

2. Session Termination Hook

File: src/apps/relay/ns_ioalib_engine_impl.c

void turn_report_session_usage(void *session, int force_invalid) {
    // ... existing code
    
    if(force_invalid) {
        const turn_dbdriver_t * dbd = get_dbdriver();
        if(dbd->report_usage)
            dbd->report_usage(session);
    }

   // ... existing code
   ss->received_packets = 0;
   ...
}

@eakraly eakraly changed the title Issue 1793 Add session usage reporting callback to TURN database driver Add session usage reporting callback to TURN database driver Feb 13, 2026
@eakraly
Copy link
Collaborator

eakraly commented Feb 13, 2026

@mesibo thanks for the PR
I have a few follow up questions:

  1. There is no implementation in any of the DB drivers. Do you mind contributing a single implementation?
  2. This should be controlled by a flag so that it is conditionally enabled and not forced upon
    Thanks!

@mesibo
Copy link
Author

mesibo commented Feb 20, 2026

We initially had a MySQL driver implementation of report_usage, but later refactored our approach, removed all db drivers, and built a REST API-based driver instead. We can contribute if it works for you.

On the conditional flag — not sure that's necessary. Like any other function in the dbdriver interface, the implementation can simply choose to ignore it if not needed.

On a broader note, bundling DB drivers into Coturn pulls in external dependencies (libmysqlclient, libpq, etc.) that add unnecessary build and maintenance overhead for everyone. A better approach would be loadable modules, so operators can plug in their own reporting backend without touching the core Coturn build.

Moreover, we felt that DB drivers don't work well with dynamic username generation — one of the reasons we moved away from them in production. We generate usernames not just based on time, but also on call ID to control and gate traffic.
Happy to raise a separate issue to describe this in more detail.

@eakraly
Copy link
Collaborator

eakraly commented Feb 21, 2026

@mesibo Thanks for the explanation - those make sense to me.
Feel free to make additional contributions and I will try to review them asap!

@mesibo
Copy link
Author

mesibo commented Feb 24, 2026

@mesibo Thanks for the explanation - those make sense to me. Feel free to make additional contributions and I will try to review them asap!

New issue added, we can discuss #1815

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants