Skip to content

Commit 0e894a7

Browse files
authored
Make TURN REST API timeout configurable in janus.jcfg (#2470)
1 parent 9e1ea1e commit 0e894a7

File tree

6 files changed

+28
-11
lines changed

6 files changed

+28
-11
lines changed

conf/janus.jcfg.sample.in

+6-3
Original file line numberDiff line numberDiff line change
@@ -294,12 +294,15 @@ nat: {
294294
# which is currently available in both rfc5766-turn-server and coturn.
295295
# You enable this by specifying the address of your TURN REST API backend,
296296
# the HTTP method to use (GET or POST) and, if required, the API key Janus
297-
# must provide. Notice that the 'opaque_id' provided via Janus API will be
298-
# used as the username for a specific PeerConnection by default; if that one
299-
# is missing, the 'session_id' will be used as the username instead.
297+
# must provide. The timeout can be configured in seconds, with a default of
298+
# 10 seconds and a minimum of 1 second. Notice that the 'opaque_id' provided
299+
# via Janus API will be used as the username for a specific PeerConnection
300+
# by default; if that one is missing, the 'session_id' will be used as the
301+
# username instead.
300302
#turn_rest_api = "http://yourbackend.com/path/to/api"
301303
#turn_rest_api_key = "anyapikeyyoumayhaveset"
302304
#turn_rest_api_method = "GET"
305+
#turn_rest_api_timeout = 10
303306

304307
# You can also choose which interfaces should be explicitly used by the
305308
# gateway for the purpose of ICE candidates gathering, thus excluding

ice.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -1125,7 +1125,7 @@ int janus_ice_set_turn_server(gchar *turn_server, uint16_t turn_port, gchar *tur
11251125
return 0;
11261126
}
11271127

1128-
int janus_ice_set_turn_rest_api(gchar *api_server, gchar *api_key, gchar *api_method) {
1128+
int janus_ice_set_turn_rest_api(gchar *api_server, gchar *api_key, gchar *api_method, uint api_timeout) {
11291129
#ifndef HAVE_TURNRESTAPI
11301130
JANUS_LOG(LOG_ERR, "Janus has been built with no libcurl support, TURN REST API unavailable\n");
11311131
return -1;
@@ -1135,7 +1135,7 @@ int janus_ice_set_turn_rest_api(gchar *api_server, gchar *api_key, gchar *api_me
11351135
JANUS_LOG(LOG_ERR, "Invalid TURN REST API backend: not an HTTP address\n");
11361136
return -1;
11371137
}
1138-
janus_turnrest_set_backend(api_server, api_key, api_method);
1138+
janus_turnrest_set_backend(api_server, api_key, api_method, api_timeout);
11391139
JANUS_LOG(LOG_INFO, "TURN REST API backend: %s\n", api_server ? api_server : "(disabled)");
11401140
#endif
11411141
return 0;

ice.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,9 @@ int janus_ice_set_turn_server(gchar *turn_server, uint16_t turn_port, gchar *tur
7070
* @param[in] api_server TURN REST API backend (NULL to disable the API)
7171
* @param[in] api_key API key to use, if required
7272
* @param[in] api_method HTTP method to use (POST by default)
73+
* @param[in] api_timeout total timeout for HTTP method in seconds
7374
* @returns 0 in case of success, a negative integer on errors */
74-
int janus_ice_set_turn_rest_api(gchar *api_server, gchar *api_key, gchar *api_method);
75+
int janus_ice_set_turn_rest_api(gchar *api_server, gchar *api_key, gchar *api_method, uint api_timeout);
7576
/*! \brief Method to get the STUN server IP address
7677
* @returns The currently used STUN server IP address, if available, or NULL if not */
7778
char *janus_ice_get_stun_server(void);

janus.c

+11-1
Original file line numberDiff line numberDiff line change
@@ -4643,6 +4643,7 @@ gint main(int argc, char *argv[])
46434643
char *turn_rest_api = NULL, *turn_rest_api_key = NULL;
46444644
#ifdef HAVE_TURNRESTAPI
46454645
char *turn_rest_api_method = NULL;
4646+
uint turn_rest_api_timeout = 10;
46464647
#endif
46474648
uint16_t rtp_min_port = 0, rtp_max_port = 0;
46484649
gboolean ice_lite = FALSE, ice_tcp = FALSE, full_trickle = FALSE, ipv6 = FALSE,
@@ -4757,6 +4758,15 @@ gint main(int argc, char *argv[])
47574758
item = janus_config_get(config, config_nat, janus_config_type_item, "turn_rest_api_method");
47584759
if(item && item->value)
47594760
turn_rest_api_method = (char *)item->value;
4761+
item = janus_config_get(config, config_nat, janus_config_type_item, "turn_rest_api_timeout");
4762+
if(item && item->value) {
4763+
int rst = atoi(item->value);
4764+
if(rst <= 0) { /* Don't allow user to set 0 seconds i.e., infinite wait */
4765+
JANUS_LOG(LOG_WARN, "Ignoring turn_rest_api_timeout as it's not a positive integer, leaving at default (10 seconds)\n");
4766+
} else {
4767+
turn_rest_api_timeout = rst;
4768+
}
4769+
}
47604770
#endif
47614771
/* Do we need a limited number of static event loops, or is it ok to have one per handle (the default)? */
47624772
item = janus_config_get(config, config_general, janus_config_type_item, "event_loops");
@@ -4785,7 +4795,7 @@ gint main(int argc, char *argv[])
47854795
JANUS_LOG(LOG_WARN, "A TURN REST API backend specified in the settings, but libcurl support has not been built\n");
47864796
}
47874797
#else
4788-
if(janus_ice_set_turn_rest_api(turn_rest_api, turn_rest_api_key, turn_rest_api_method) < 0) {
4798+
if(janus_ice_set_turn_rest_api(turn_rest_api, turn_rest_api_key, turn_rest_api_method, turn_rest_api_timeout) < 0) {
47894799
JANUS_LOG(LOG_FATAL, "Invalid TURN REST API configuration: %s (%s, %s)\n", turn_rest_api, turn_rest_api_key, turn_rest_api_method);
47904800
exit(1);
47914801
}

turnrest.c

+4-2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
static const char *api_server = NULL;
3232
static const char *api_key = NULL;
3333
static gboolean api_http_get = FALSE;
34+
static uint api_timeout;
3435
static janus_mutex api_mutex = JANUS_MUTEX_INITIALIZER;
3536

3637

@@ -69,7 +70,7 @@ void janus_turnrest_deinit(void) {
6970
janus_mutex_unlock(&api_mutex);
7071
}
7172

72-
void janus_turnrest_set_backend(const char *server, const char *key, const char *method) {
73+
void janus_turnrest_set_backend(const char *server, const char *key, const char *method, const uint timeout) {
7374
janus_mutex_lock(&api_mutex);
7475

7576
/* Get rid of the old values first */
@@ -93,6 +94,7 @@ void janus_turnrest_set_backend(const char *server, const char *key, const char
9394
api_http_get = FALSE;
9495
}
9596
}
97+
api_timeout = timeout;
9698
}
9799
janus_mutex_unlock(&api_mutex);
98100
}
@@ -163,7 +165,7 @@ janus_turnrest_response *janus_turnrest_request(const char *user) {
163165
/* FIXME Some servers don't like a POST with no data */
164166
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, query_string);
165167
}
166-
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 10L); /* FIXME Max 10 seconds */
168+
curl_easy_setopt(curl, CURLOPT_TIMEOUT, api_timeout);
167169
/* For getting data, we use an helper struct and the libcurl callback */
168170
janus_turnrest_buffer data;
169171
data.buffer = g_malloc0(1);

turnrest.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,9 @@ void janus_turnrest_deinit(void);
3131
* @param server The REST API server address (pass NULL to disable the
3232
* TURN REST API entirely)
3333
* @param key The API key, if any (pass NULL if it's not required)
34-
* @param method The HTTP method to use, POST or GET (NULL means POST) */
35-
void janus_turnrest_set_backend(const char *server, const char *key, const char *method);
34+
* @param method The HTTP method to use, POST or GET (NULL means POST)
35+
* @param timeout The timeout in seconds */
36+
void janus_turnrest_set_backend(const char *server, const char *key, const char *method, const uint timeout);
3637
/*! \brief Get the currently set TURN REST API backend
3738
* @returns The currently set TURN REST API backend */
3839
const char *janus_turnrest_get_backend(void);

0 commit comments

Comments
 (0)