Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unused TURN allocations #1487

Open
quadriq opened this issue May 10, 2024 · 9 comments
Open

Unused TURN allocations #1487

quadriq opened this issue May 10, 2024 · 9 comments

Comments

@quadriq
Copy link

quadriq commented May 10, 2024

Hi,
I noticed, that if I have TURN Server in ICE credentials. like:

{
    "iceServers": [
        {
            "urls": [
                "stun:some_stun_server:3478"
            ]
        },
        {
            "username": "user",
            "credential": "cred",
            "urls": [
                "turn:myturn.host.com:3478?transport=udp",
                "turn:myturn.host.com:3478?transport=tcp"
            ]
        }
    ]
}

The TURN server usually creates 4 allocations(2UDP + 2 TCP), even if the TURN connection is not needed, because WebRTC clients are in the same network and can communicate via the 'host' candidate. I believe this occurs during the process of gathering all available candidates. These are TURN session allocations, not STUN, as I am certain because I run STUN and TURN servers on different machines.

Now my question is, is there an indicator to filter out those 'unused' sessions on the TURN server? Maybe there is a place in the source code where I can look?

I would like to count the real usage of the TURN server by clients. My first thought was to simply check active TURN allocations, but because the allocation is always there, even the turn server was not really used, I am unsure how to achieve this.

Thank you for any ideas.

@dangngoctam00
Copy link

dangngoctam00 commented May 12, 2024

Hello @quadriq,
I know one way but I haven't tested it carefully, you could check function void turn_report_session_usage(void *session, int force_invalid),

  • active allocation will publish message to peer traffic when ss->received_packets + ss->sent_packets + ss->peer_received_packets + ss->peer_sent_packets == 4096, and 4 values in this expression are not equal 0.
  • inactive allocation will also publish this message, but because they only contain refreshed message, so ss->peer_received_packets + ss->peer_sent_packets will be equal to 0.

you could try this way and give me feedback, like I said above, I haven't tested it carefully.
Thanks

@quadriq
Copy link
Author

quadriq commented May 12, 2024

Hi @dangngoctam00 thank you, I will try it out. Does it actually means that "inactive allocation" would always have peer usage (peer_received_packets and, ss->peer_sent_packets) equal zero ?

@dangngoctam00
Copy link

dangngoctam00 commented May 12, 2024

Hi @quadriq, this is what I've observed but I think you can test it yourself when having host-host candidate, it may take a long time to wait total of refreshed message equals to 4096.
I've tested with turnutils_uclient, here I sent refreshed message only, so the traffic of peer equals to 0.

image

@dangngoctam00
Copy link

hello @quadriq , do you have any update on this requirement?

@quadriq
Copy link
Author

quadriq commented Jun 5, 2024

Hi @dangngoctam00 ,
No, sorry, I have not yet done a deep investigation on this issue. However, it is definitely on my to-do list. As soon as I get any results, I will report them here.

@eakraly
Copy link
Collaborator

eakraly commented Jul 15, 2024

Sounds like this approach makes sense - count the connections that have more than 0 bytes sent/received

If you want to completely avoid using TURN when not required you would need to do it in your application. It would be something like:

  • feed the app with remote candidates
  • if it fails to connect, add relay candidates and restart ICE
    Downside is complexity and long latency to connect in this case

Overall, unused connections do not have much cost and auto-close in 10 minutes by default.

@quadriq
Copy link
Author

quadriq commented Sep 12, 2024

Hi, so I finaly was able to run tests.

What I did is introduced a marker in a session and then set it to 1, as soon as packet size hit 4095. So like:

if (( (ss->received_packets + ss->sent_packets + ss->peer_received_packets + ss->peer_sent_packets) & 4095) == 0 ){
         ss->relay_marker = 1;
      }

After running tests I saw that the session with relay_marker == 1 was the one with the most traffic consumption for Relay-Connections. If the connection type was "host" then the marker stayed by 0.

So this way worked for me to determinate if the session was active or not. Do not know if it's a good idea to add such variable to coturn for everybody, but for specific usecases and those who compiles coturn anyway, that looks like a solution of my problem.

Thank you.

@dangngoctam00
Copy link

dangngoctam00 commented Sep 13, 2024

@quadriq
Thank you for you confirmation.
About adding a new field to allocation data, I think it's not a problem because the overhead is not too much and coTURn is not memory sensitive application.

@quadriq
Copy link
Author

quadriq commented Sep 13, 2024

here is more code in case someone would like to reproduce:

ns_turn_session.h

struct _ts_ur_super_session {
 ...
   int relay_marker;
}


ns_ioalib_engine_impl.c

void turn_report_session_usage(void *session, int force_invalid) {

  if (server && (ss->received_packets || ss->sent_packets || force_invalid)) {
     if (( (ss->received_packets + ss->sent_packets + ss->peer_received_packets + ss->peer_sent_packets) & 4095) == 0 ){
           TURN_LOG_FUNC(TURN_LOG_LEVEL_INFO, "--------- LOOKS LIKE TURN SESSION: %018llu \n", (unsigned long long)(ss->id));
           ss->relay_marker = 1;
        }
   ...
  }
...
}



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

No branches or pull requests

3 participants