libobs: Use exponential backoff for reconnecting
Implements exponential backoff for consecutive reconnects, which is useful to prevent too many connections from trying to reconnect back to a service at once over a short period of time in the case of potential service downtime. Exponential backoff causes each subsequent reconnect attempt to double its timeout duration.
This commit is contained in:
parent
691d3b4a73
commit
c1e8d28548
@ -466,6 +466,7 @@ struct obs_output {
|
||||
int reconnect_retry_sec;
|
||||
int reconnect_retry_max;
|
||||
int reconnect_retries;
|
||||
int reconnect_retry_cur_sec;
|
||||
bool reconnecting;
|
||||
pthread_t reconnect_thread;
|
||||
os_event_t *reconnect_stop_event;
|
||||
|
@ -1028,7 +1028,7 @@ static inline void signal_reconnect(struct obs_output *output)
|
||||
{
|
||||
struct calldata params = {0};
|
||||
calldata_set_int(¶ms, "timeout_sec",
|
||||
output->reconnect_retry_sec);
|
||||
output->reconnect_retry_cur_sec);
|
||||
calldata_set_ptr(¶ms, "output", output);
|
||||
signal_handler_signal(output->context.signals, "reconnect", ¶ms);
|
||||
calldata_free(¶ms);
|
||||
@ -1220,7 +1220,7 @@ void obs_output_end_data_capture(obs_output_t *output)
|
||||
static void *reconnect_thread(void *param)
|
||||
{
|
||||
struct obs_output *output = param;
|
||||
unsigned long ms = output->reconnect_retry_sec * 1000;
|
||||
unsigned long ms = output->reconnect_retry_cur_sec * 1000;
|
||||
|
||||
output->reconnect_thread_active = true;
|
||||
|
||||
@ -1238,8 +1238,10 @@ static void output_reconnect(struct obs_output *output)
|
||||
{
|
||||
int ret;
|
||||
|
||||
if (!output->reconnecting)
|
||||
if (!output->reconnecting) {
|
||||
output->reconnect_retry_cur_sec = output->reconnect_retry_sec;
|
||||
output->reconnect_retries = 0;
|
||||
}
|
||||
|
||||
if (output->reconnect_retries >= output->reconnect_retry_max) {
|
||||
output->reconnecting = false;
|
||||
@ -1252,6 +1254,10 @@ static void output_reconnect(struct obs_output *output)
|
||||
os_event_reset(output->reconnect_stop_event);
|
||||
}
|
||||
|
||||
if (output->reconnect_retries) {
|
||||
output->reconnect_retry_cur_sec *= 2;
|
||||
}
|
||||
|
||||
output->reconnect_retries++;
|
||||
|
||||
ret = pthread_create(&output->reconnect_thread, NULL,
|
||||
|
Loading…
x
Reference in New Issue
Block a user