Skip to content

Commit

Permalink
Make TURN REST API timeout configurable in janus.jcfg (meetecho#2470)
Browse files Browse the repository at this point in the history
  • Loading branch information
evorw authored Dec 3, 2020
1 parent 9e1ea1e commit 0e894a7
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 11 deletions.
9 changes: 6 additions & 3 deletions conf/janus.jcfg.sample.in
Original file line number Diff line number Diff line change
Expand Up @@ -294,12 +294,15 @@ nat: {
# which is currently available in both rfc5766-turn-server and coturn.
# You enable this by specifying the address of your TURN REST API backend,
# the HTTP method to use (GET or POST) and, if required, the API key Janus
# must provide. Notice that the 'opaque_id' provided via Janus API will be
# used as the username for a specific PeerConnection by default; if that one
# is missing, the 'session_id' will be used as the username instead.
# must provide. The timeout can be configured in seconds, with a default of
# 10 seconds and a minimum of 1 second. Notice that the 'opaque_id' provided
# via Janus API will be used as the username for a specific PeerConnection
# by default; if that one is missing, the 'session_id' will be used as the
# username instead.
#turn_rest_api = "http://yourbackend.com/path/to/api"
#turn_rest_api_key = "anyapikeyyoumayhaveset"
#turn_rest_api_method = "GET"
#turn_rest_api_timeout = 10

# You can also choose which interfaces should be explicitly used by the
# gateway for the purpose of ICE candidates gathering, thus excluding
Expand Down
4 changes: 2 additions & 2 deletions ice.c
Original file line number Diff line number Diff line change
Expand Up @@ -1125,7 +1125,7 @@ int janus_ice_set_turn_server(gchar *turn_server, uint16_t turn_port, gchar *tur
return 0;
}

int janus_ice_set_turn_rest_api(gchar *api_server, gchar *api_key, gchar *api_method) {
int janus_ice_set_turn_rest_api(gchar *api_server, gchar *api_key, gchar *api_method, uint api_timeout) {
#ifndef HAVE_TURNRESTAPI
JANUS_LOG(LOG_ERR, "Janus has been built with no libcurl support, TURN REST API unavailable\n");
return -1;
Expand All @@ -1135,7 +1135,7 @@ int janus_ice_set_turn_rest_api(gchar *api_server, gchar *api_key, gchar *api_me
JANUS_LOG(LOG_ERR, "Invalid TURN REST API backend: not an HTTP address\n");
return -1;
}
janus_turnrest_set_backend(api_server, api_key, api_method);
janus_turnrest_set_backend(api_server, api_key, api_method, api_timeout);
JANUS_LOG(LOG_INFO, "TURN REST API backend: %s\n", api_server ? api_server : "(disabled)");
#endif
return 0;
Expand Down
3 changes: 2 additions & 1 deletion ice.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,9 @@ int janus_ice_set_turn_server(gchar *turn_server, uint16_t turn_port, gchar *tur
* @param[in] api_server TURN REST API backend (NULL to disable the API)
* @param[in] api_key API key to use, if required
* @param[in] api_method HTTP method to use (POST by default)
* @param[in] api_timeout total timeout for HTTP method in seconds
* @returns 0 in case of success, a negative integer on errors */
int janus_ice_set_turn_rest_api(gchar *api_server, gchar *api_key, gchar *api_method);
int janus_ice_set_turn_rest_api(gchar *api_server, gchar *api_key, gchar *api_method, uint api_timeout);
/*! \brief Method to get the STUN server IP address
* @returns The currently used STUN server IP address, if available, or NULL if not */
char *janus_ice_get_stun_server(void);
Expand Down
12 changes: 11 additions & 1 deletion janus.c
Original file line number Diff line number Diff line change
Expand Up @@ -4643,6 +4643,7 @@ gint main(int argc, char *argv[])
char *turn_rest_api = NULL, *turn_rest_api_key = NULL;
#ifdef HAVE_TURNRESTAPI
char *turn_rest_api_method = NULL;
uint turn_rest_api_timeout = 10;
#endif
uint16_t rtp_min_port = 0, rtp_max_port = 0;
gboolean ice_lite = FALSE, ice_tcp = FALSE, full_trickle = FALSE, ipv6 = FALSE,
Expand Down Expand Up @@ -4757,6 +4758,15 @@ gint main(int argc, char *argv[])
item = janus_config_get(config, config_nat, janus_config_type_item, "turn_rest_api_method");
if(item && item->value)
turn_rest_api_method = (char *)item->value;
item = janus_config_get(config, config_nat, janus_config_type_item, "turn_rest_api_timeout");
if(item && item->value) {
int rst = atoi(item->value);
if(rst <= 0) { /* Don't allow user to set 0 seconds i.e., infinite wait */
JANUS_LOG(LOG_WARN, "Ignoring turn_rest_api_timeout as it's not a positive integer, leaving at default (10 seconds)\n");
} else {
turn_rest_api_timeout = rst;
}
}
#endif
/* Do we need a limited number of static event loops, or is it ok to have one per handle (the default)? */
item = janus_config_get(config, config_general, janus_config_type_item, "event_loops");
Expand Down Expand Up @@ -4785,7 +4795,7 @@ gint main(int argc, char *argv[])
JANUS_LOG(LOG_WARN, "A TURN REST API backend specified in the settings, but libcurl support has not been built\n");
}
#else
if(janus_ice_set_turn_rest_api(turn_rest_api, turn_rest_api_key, turn_rest_api_method) < 0) {
if(janus_ice_set_turn_rest_api(turn_rest_api, turn_rest_api_key, turn_rest_api_method, turn_rest_api_timeout) < 0) {
JANUS_LOG(LOG_FATAL, "Invalid TURN REST API configuration: %s (%s, %s)\n", turn_rest_api, turn_rest_api_key, turn_rest_api_method);
exit(1);
}
Expand Down
6 changes: 4 additions & 2 deletions turnrest.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
static const char *api_server = NULL;
static const char *api_key = NULL;
static gboolean api_http_get = FALSE;
static uint api_timeout;
static janus_mutex api_mutex = JANUS_MUTEX_INITIALIZER;


Expand Down Expand Up @@ -69,7 +70,7 @@ void janus_turnrest_deinit(void) {
janus_mutex_unlock(&api_mutex);
}

void janus_turnrest_set_backend(const char *server, const char *key, const char *method) {
void janus_turnrest_set_backend(const char *server, const char *key, const char *method, const uint timeout) {
janus_mutex_lock(&api_mutex);

/* Get rid of the old values first */
Expand All @@ -93,6 +94,7 @@ void janus_turnrest_set_backend(const char *server, const char *key, const char
api_http_get = FALSE;
}
}
api_timeout = timeout;
}
janus_mutex_unlock(&api_mutex);
}
Expand Down Expand Up @@ -163,7 +165,7 @@ janus_turnrest_response *janus_turnrest_request(const char *user) {
/* FIXME Some servers don't like a POST with no data */
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, query_string);
}
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 10L); /* FIXME Max 10 seconds */
curl_easy_setopt(curl, CURLOPT_TIMEOUT, api_timeout);
/* For getting data, we use an helper struct and the libcurl callback */
janus_turnrest_buffer data;
data.buffer = g_malloc0(1);
Expand Down
5 changes: 3 additions & 2 deletions turnrest.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ void janus_turnrest_deinit(void);
* @param server The REST API server address (pass NULL to disable the
* TURN REST API entirely)
* @param key The API key, if any (pass NULL if it's not required)
* @param method The HTTP method to use, POST or GET (NULL means POST) */
void janus_turnrest_set_backend(const char *server, const char *key, const char *method);
* @param method The HTTP method to use, POST or GET (NULL means POST)
* @param timeout The timeout in seconds */
void janus_turnrest_set_backend(const char *server, const char *key, const char *method, const uint timeout);
/*! \brief Get the currently set TURN REST API backend
* @returns The currently set TURN REST API backend */
const char *janus_turnrest_get_backend(void);
Expand Down

0 comments on commit 0e894a7

Please sign in to comment.