-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Options to block startup unless RabbitMQ Transport / Event Handler are connected #3381
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -35,6 +35,7 @@ general: { | |
| #queue_autodelete = false # Whether or not incoming queue should autodelete after janus disconnects from RabbitMQ | ||
| #queue_exclusive = false # Whether or not incoming queue should only allow one subscriber | ||
| #heartbeat = 60 # Defines the seconds without communication that should pass before considering the TCP connection unreachable. | ||
| #block_startup_until_connected = true # Whether to block the server from starting until a connection to the RabbitMQ server is established. | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here. |
||
|
|
||
| #ssl_enabled = false # Whether ssl support must be enabled | ||
| #ssl_verify_peer = true # Whether peer verification must be enabled | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -87,6 +87,7 @@ static GThread *handler_thread; | |
| static GThread *in_thread; | ||
| static void *jns_rmqevh_hdlr(void *data); | ||
| static void *jns_rmqevh_hrtbt(void *data); | ||
| int janus_rabbitmqevh_tryconnect(void); | ||
| int janus_rabbitmqevh_connect(void); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure why there's two functions here now. You made it inline in the transport, why not do the same in the event handler? |
||
|
|
||
| /* Queue of events to handle */ | ||
|
|
@@ -121,6 +122,7 @@ static gboolean ssl_verify_peer = FALSE; | |
| static gboolean ssl_verify_hostname = FALSE; | ||
| static char *route_key = NULL, *exchange = NULL, *exchange_type = NULL ; | ||
| static uint16_t heartbeat = 0; | ||
| static gboolean block_startup_until_connected = FALSE; | ||
| static uint16_t rmqport = AMQP_PROTOCOL_PORT; | ||
| static gboolean declare_outgoing_queue = TRUE; | ||
|
|
||
|
|
@@ -236,6 +238,10 @@ int janus_rabbitmqevh_init(const char *config_path) { | |
| heartbeat = 0; | ||
| } | ||
|
|
||
| item = janus_config_get(config, config_general, janus_config_type_item, "block_startup_until_connected"); | ||
| if(item && item->value && janus_is_true(item->value)) | ||
| block_startup_until_connected = TRUE; | ||
|
|
||
| /* SSL config*/ | ||
| item = janus_config_get(config, config_general, janus_config_type_item, "ssl_enable"); | ||
| if(!item || !item->value || !janus_is_true(item->value)) { | ||
|
|
@@ -346,6 +352,26 @@ int janus_rabbitmqevh_init(const char *config_path) { | |
| } | ||
|
|
||
| int janus_rabbitmqevh_connect(void) { | ||
| uint8_t retry_interval = 5; // Retry interval in seconds | ||
chriswiggins marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| while (!g_atomic_int_get(&stopping)) { | ||
| if (janus_rabbitmqevh_tryconnect() == 0) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Code style: we prefer no space between the check ( and There's a few other occurrences of the same, so please change them there too. |
||
| return 0; | ||
| } | ||
|
|
||
| if (!block_startup_until_connected) { | ||
| break; | ||
| } | ||
|
|
||
| JANUS_LOG(LOG_ERR, "RabbitMQEventHandler: Connection failed, retrying in %d seconds\n", retry_interval); | ||
| g_usleep(retry_interval * 1000000); | ||
| } | ||
|
|
||
| JANUS_LOG(LOG_FATAL, "Failed to connect to RabbitMQ\n"); | ||
| return -1; | ||
| } | ||
|
|
||
| int janus_rabbitmqevh_tryconnect(void){ | ||
| rmq_conn = amqp_new_connection(); | ||
| amqp_socket_t *socket = NULL; | ||
| int status = AMQP_STATUS_OK; | ||
|
|
@@ -665,7 +691,7 @@ static void *jns_rmqevh_hrtbt(void *data) { | |
| } | ||
| if(!g_atomic_int_get(&stopping)) { | ||
| JANUS_LOG(LOG_VERB, "RabbitMQEventHandler: Trying to reconnect\n"); | ||
| int result = janus_rabbitmqevh_connect(); | ||
| int result = janus_rabbitmqevh_tryconnect(); | ||
| if(result < 0) { | ||
| g_usleep(5000000); | ||
| } else { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -190,6 +190,7 @@ static gboolean declare_outgoing_queue = FALSE, declare_outgoing_queue_admin = F | |
| amqp_boolean_t queue_durable = 0, queue_exclusive = 0, queue_autodelete = 0, | ||
| queue_durable_admin = 0, queue_exclusive_admin = 0, queue_autodelete_admin = 0; | ||
| static uint16_t heartbeat = 0; | ||
| static gboolean block_startup_until_connected = FALSE; | ||
|
|
||
| /* Transport implementation */ | ||
| int janus_rabbitmq_init(janus_transport_callbacks *callback, const char *config_path) { | ||
|
|
@@ -312,6 +313,11 @@ int janus_rabbitmq_init(janus_transport_callbacks *callback, const char *config_ | |
| heartbeat = 0; | ||
| } | ||
|
|
||
| /* Check if we need to block startup until RabbitMQ is connected */ | ||
| item = janus_config_get(config, config_general, janus_config_type_item, "block_startup_until_connected"); | ||
| if(item && item->value && janus_is_true(item->value)) | ||
| block_startup_until_connected = TRUE; | ||
|
|
||
| /* Now check if the Janus API must be supported */ | ||
| item = janus_config_get(config, config_general, janus_config_type_item, "enabled"); | ||
| if(item == NULL) { | ||
|
|
@@ -455,7 +461,22 @@ int janus_rabbitmq_init(janus_transport_callbacks *callback, const char *config_ | |
| rmq_client = g_malloc0(sizeof(janus_rabbitmq_client)); | ||
|
|
||
| /* Connect */ | ||
| int result = janus_rabbitmq_connect(); | ||
| uint8_t retry_interval = 5; // Retry interval in seconds | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here. |
||
| int result = -1; | ||
| while(!rmq_client->destroy && !g_atomic_int_get(&stopping)) { | ||
| result = janus_rabbitmq_connect(); | ||
| if (result == 0) { | ||
| break; | ||
| } | ||
|
|
||
| if (!block_startup_until_connected) { | ||
| break; | ||
| } | ||
|
|
||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. All these empty lines are unneeded: I think you can make this block more compact without them. |
||
| JANUS_LOG(LOG_ERR, "RabbitMQ: Connection failed, retrying in %d seconds\n", retry_interval); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure if this should be a |
||
| g_usleep(retry_interval * 1000000); | ||
| } | ||
|
|
||
| if(result < 0) { | ||
| goto error; | ||
| } | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.