FS-9952: Some code refactoring and added TTL for sessions, currently harcoded at 5 seconds for testing purposes only and should come from config

This commit is contained in:
Shane Bryldt 2017-02-27 18:39:40 +00:00 committed by Mike Jerris
parent 14a99987bb
commit 5d7e40c811
3 changed files with 356 additions and 204 deletions

View File

@ -55,6 +55,12 @@ struct blade_connection_s {
}; };
void *blade_connection_state_thread(ks_thread_t *thread, void *data); void *blade_connection_state_thread(ks_thread_t *thread, void *data);
ks_status_t blade_connection_state_on_disconnect(blade_connection_t *bc);
ks_status_t blade_connection_state_on_new(blade_connection_t *bc);
ks_status_t blade_connection_state_on_connect(blade_connection_t *bc);
ks_status_t blade_connection_state_on_attach(blade_connection_t *bc);
ks_status_t blade_connection_state_on_detach(blade_connection_t *bc);
ks_status_t blade_connection_state_on_ready(blade_connection_t *bc);
KS_DECLARE(ks_status_t) blade_connection_create(blade_connection_t **bcP, KS_DECLARE(ks_status_t) blade_connection_create(blade_connection_t **bcP,
@ -344,9 +350,6 @@ void *blade_connection_state_thread(ks_thread_t *thread, void *data)
{ {
blade_connection_t *bc = NULL; blade_connection_t *bc = NULL;
blade_connection_state_t state; blade_connection_state_t state;
blade_transport_state_callback_t callback = NULL;
blade_connection_state_hook_t hook = BLADE_CONNECTION_STATE_HOOK_SUCCESS;
cJSON *json = NULL;
ks_assert(thread); ks_assert(thread);
ks_assert(data); ks_assert(data);
@ -354,92 +357,28 @@ void *blade_connection_state_thread(ks_thread_t *thread, void *data)
bc = (blade_connection_t *)data; bc = (blade_connection_t *)data;
while (!bc->shutdown) { while (!bc->shutdown) {
state = bc->state; state = bc->state;
hook = BLADE_CONNECTION_STATE_HOOK_SUCCESS;
callback = blade_connection_state_callback_lookup(bc, state);
if (state == BLADE_CONNECTION_STATE_DISCONNECT) { switch (state) {
blade_handle_connections_remove(bc); case BLADE_CONNECTION_STATE_DISCONNECT:
} blade_connection_state_on_disconnect(bc);
// @todo only READY state? break;
if (state != BLADE_CONNECTION_STATE_DETACH && state != BLADE_CONNECTION_STATE_DISCONNECT) { case BLADE_CONNECTION_STATE_NEW:
while (blade_connection_sending_pop(bc, &json) == KS_STATUS_SUCCESS && json) { blade_connection_state_on_new(bc);
ks_status_t ret = bc->transport_callbacks->onsend(bc, json); break;
cJSON_Delete(json); case BLADE_CONNECTION_STATE_CONNECT:
blade_connection_state_on_connect(bc);
if (ret != KS_STATUS_SUCCESS) { break;
blade_connection_disconnect(bc); case BLADE_CONNECTION_STATE_ATTACH:
break; blade_connection_state_on_attach(bc);
} break;
} case BLADE_CONNECTION_STATE_DETACH:
} blade_connection_state_on_detach(bc);
break;
if (state == BLADE_CONNECTION_STATE_READY) { case BLADE_CONNECTION_STATE_READY:
ks_bool_t done = KS_FALSE; blade_connection_state_on_ready(bc);
while (!done) { break;
if (bc->transport_callbacks->onreceive(bc, &json) != KS_STATUS_SUCCESS) { default: break;
blade_connection_disconnect(bc);
break;
}
if (!(done = (json == NULL))) {
blade_session_t *bs = blade_handle_sessions_get(bc->handle, bc->session);
ks_assert(bs);
blade_session_receiving_push(bs, json);
cJSON_Delete(json);
json = NULL;
}
}
}
if (callback) hook = callback(bc, BLADE_CONNECTION_STATE_CONDITION_POST);
if (hook == BLADE_CONNECTION_STATE_HOOK_DISCONNECT && (state == BLADE_CONNECTION_STATE_DETACH || state == BLADE_CONNECTION_STATE_DISCONNECT))
hook = BLADE_CONNECTION_STATE_HOOK_SUCCESS;
if (hook == BLADE_CONNECTION_STATE_HOOK_DISCONNECT) blade_connection_disconnect(bc);
else if (hook == BLADE_CONNECTION_STATE_HOOK_SUCCESS) {
switch (state) {
case BLADE_CONNECTION_STATE_DISCONNECT:
blade_connection_destroy(&bc);
break;
case BLADE_CONNECTION_STATE_NEW:
blade_connection_state_set(bc, BLADE_CONNECTION_STATE_CONNECT);
break;
case BLADE_CONNECTION_STATE_CONNECT:
blade_connection_state_set(bc, BLADE_CONNECTION_STATE_ATTACH);
break;
case BLADE_CONNECTION_STATE_ATTACH:
{
// @todo this is adding a second lock, since we keep it locked in the callback to allow finishing, we don't want get locking here...
// or just try unlocking twice to confirm...
blade_session_t *bs = blade_handle_sessions_get(bc->handle, bc->session);
ks_assert(bs); // should not happen because bs should still be locked
blade_session_connections_add(bs, bc->id);
blade_connection_state_set(bc, BLADE_CONNECTION_STATE_READY);
blade_session_state_set(bs, BLADE_SESSION_STATE_READY); // @todo only set this if it's not already in the READY state from prior connection
blade_session_read_unlock(bs); // unlock the session we locked obtaining it above
blade_session_read_unlock(bs); // unlock the session we expect to be locked during the callback to ensure we can finish attaching
break;
}
case BLADE_CONNECTION_STATE_DETACH:
{
if (bc->session) {
blade_session_t *bs = blade_handle_sessions_get(bc->handle, bc->session);
ks_assert(bs);
blade_session_connections_remove(bs, bc->id);
blade_session_read_unlock(bs);
// keep bc->session for later in case something triggers a reconnect later and needs the old session id for a hint
}
blade_connection_state_set(bc, BLADE_CONNECTION_STATE_DISCONNECT);
break;
}
default: break;
}
} }
if (state == BLADE_CONNECTION_STATE_DISCONNECT) break; if (state == BLADE_CONNECTION_STATE_DISCONNECT) break;
@ -448,6 +387,151 @@ void *blade_connection_state_thread(ks_thread_t *thread, void *data)
return NULL; return NULL;
} }
ks_status_t blade_connection_state_on_disconnect(blade_connection_t *bc)
{
blade_transport_state_callback_t callback = NULL;
ks_assert(bc);
blade_handle_connections_remove(bc);
callback = blade_connection_state_callback_lookup(bc, BLADE_CONNECTION_STATE_DISCONNECT);
if (callback) callback(bc, BLADE_CONNECTION_STATE_CONDITION_POST);
blade_connection_destroy(&bc);
return KS_STATUS_SUCCESS;
}
ks_status_t blade_connection_state_on_new(blade_connection_t *bc)
{
blade_transport_state_callback_t callback = NULL;
blade_connection_state_hook_t hook = BLADE_CONNECTION_STATE_HOOK_SUCCESS;
ks_assert(bc);
callback = blade_connection_state_callback_lookup(bc, BLADE_CONNECTION_STATE_NEW);
if (callback) hook = callback(bc, BLADE_CONNECTION_STATE_CONDITION_POST);
if (hook == BLADE_CONNECTION_STATE_HOOK_DISCONNECT) blade_connection_disconnect(bc);
else if (hook == BLADE_CONNECTION_STATE_HOOK_SUCCESS) {
blade_connection_state_set(bc, BLADE_CONNECTION_STATE_CONNECT);
}
return KS_STATUS_SUCCESS;
}
ks_status_t blade_connection_state_on_connect(blade_connection_t *bc)
{
blade_transport_state_callback_t callback = NULL;
blade_connection_state_hook_t hook = BLADE_CONNECTION_STATE_HOOK_SUCCESS;
ks_assert(bc);
callback = blade_connection_state_callback_lookup(bc, BLADE_CONNECTION_STATE_CONNECT);
if (callback) hook = callback(bc, BLADE_CONNECTION_STATE_CONDITION_POST);
if (hook == BLADE_CONNECTION_STATE_HOOK_DISCONNECT) blade_connection_disconnect(bc);
else if (hook == BLADE_CONNECTION_STATE_HOOK_SUCCESS) {
blade_connection_state_set(bc, BLADE_CONNECTION_STATE_ATTACH);
}
return KS_STATUS_SUCCESS;
}
ks_status_t blade_connection_state_on_attach(blade_connection_t *bc)
{
blade_transport_state_callback_t callback = NULL;
blade_connection_state_hook_t hook = BLADE_CONNECTION_STATE_HOOK_SUCCESS;
ks_assert(bc);
callback = blade_connection_state_callback_lookup(bc, BLADE_CONNECTION_STATE_ATTACH);
if (callback) hook = callback(bc, BLADE_CONNECTION_STATE_CONDITION_POST);
if (hook == BLADE_CONNECTION_STATE_HOOK_DISCONNECT) blade_connection_disconnect(bc);
else if (hook == BLADE_CONNECTION_STATE_HOOK_SUCCESS) {
// @todo this is adding a second lock, since we keep it locked in the callback to allow finishing, we don't want get locking here...
// or just try unlocking twice to confirm...
blade_session_t *bs = blade_handle_sessions_get(bc->handle, bc->session);
ks_assert(bs); // should not happen because bs should still be locked
blade_session_connections_add(bs, bc->id);
blade_connection_state_set(bc, BLADE_CONNECTION_STATE_READY);
blade_session_state_set(bs, BLADE_SESSION_STATE_READY); // @todo only set this if it's not already in the READY state from prior connection
blade_session_read_unlock(bs); // unlock the session we locked obtaining it above
blade_session_read_unlock(bs); // unlock the session we expect to be locked during the callback to ensure we can finish attaching
}
return KS_STATUS_SUCCESS;
}
ks_status_t blade_connection_state_on_detach(blade_connection_t *bc)
{
blade_transport_state_callback_t callback = NULL;
ks_assert(bc);
callback = blade_connection_state_callback_lookup(bc, BLADE_CONNECTION_STATE_DETACH);
if (callback) callback(bc, BLADE_CONNECTION_STATE_CONDITION_POST);
if (bc->session) {
blade_session_t *bs = blade_handle_sessions_get(bc->handle, bc->session);
ks_assert(bs);
blade_session_connections_remove(bs, bc->id);
blade_session_read_unlock(bs);
// keep bc->session for later in case something triggers a reconnect later and needs the old session id for a hint
}
blade_connection_state_set(bc, BLADE_CONNECTION_STATE_DISCONNECT);
return KS_STATUS_SUCCESS;
}
ks_status_t blade_connection_state_on_ready(blade_connection_t *bc)
{
blade_transport_state_callback_t callback = NULL;
blade_connection_state_hook_t hook = BLADE_CONNECTION_STATE_HOOK_SUCCESS;
cJSON *json = NULL;
ks_bool_t done = KS_FALSE;
ks_assert(bc);
while (blade_connection_sending_pop(bc, &json) == KS_STATUS_SUCCESS && json) {
ks_status_t ret = bc->transport_callbacks->onsend(bc, json);
cJSON_Delete(json);
if (ret != KS_STATUS_SUCCESS) {
blade_connection_disconnect(bc);
break;
}
}
while (!done) {
if (bc->transport_callbacks->onreceive(bc, &json) != KS_STATUS_SUCCESS) {
blade_connection_disconnect(bc);
break;
}
if (!(done = (json == NULL))) {
blade_session_t *bs = blade_handle_sessions_get(bc->handle, bc->session);
ks_assert(bs);
blade_session_receiving_push(bs, json);
cJSON_Delete(json);
json = NULL;
}
}
callback = blade_connection_state_callback_lookup(bc, BLADE_CONNECTION_STATE_READY);
if (callback) hook = callback(bc, BLADE_CONNECTION_STATE_CONDITION_POST);
if (hook == BLADE_CONNECTION_STATE_HOOK_DISCONNECT) blade_connection_disconnect(bc);
return KS_STATUS_SUCCESS;
}
/* For Emacs: /* For Emacs:
* Local Variables: * Local Variables:
* mode:c * mode:c

View File

@ -780,7 +780,7 @@ ks_status_t blade_transport_wss_read(blade_transport_wss_t *bt_wss, cJSON **json
if (poll_flags & KS_POLL_READ) { if (poll_flags & KS_POLL_READ) {
kws_opcode_t opcode; kws_opcode_t opcode;
uint8_t *frame_data = NULL; uint8_t *frame_data = NULL;
ks_size_t frame_data_len = kws_read_frame(bt_wss->kws, &opcode, &frame_data); ks_ssize_t frame_data_len = kws_read_frame(bt_wss->kws, &opcode, &frame_data);
if (frame_data_len <= 0) { if (frame_data_len <= 0) {
// @todo error logging, strerror(ks_errno()) // @todo error logging, strerror(ks_errno())

View File

@ -44,13 +44,17 @@ struct blade_session_s {
const char *id; const char *id;
ks_rwl_t *lock; ks_rwl_t *lock;
list_t connections; list_t connections;
ks_time_t ttl;
ks_q_t *sending; ks_q_t *sending;
ks_q_t *receiving; ks_q_t *receiving;
}; };
void *blade_session_state_thread(ks_thread_t *thread, void *data); void *blade_session_state_thread(ks_thread_t *thread, void *data);
ks_status_t blade_session_state_on_destroy(blade_session_t *bs);
ks_status_t blade_session_state_on_hangup(blade_session_t *bs);
ks_status_t blade_session_state_on_ready(blade_session_t *bs);
ks_status_t blade_session_process(blade_session_t *bs, cJSON *json);
KS_DECLARE(ks_status_t) blade_session_create(blade_session_t **bsP, blade_handle_t *bh) KS_DECLARE(ks_status_t) blade_session_create(blade_session_t **bsP, blade_handle_t *bh)
{ {
@ -261,6 +265,8 @@ KS_DECLARE(ks_status_t) blade_session_connections_add(blade_session_t *bs, const
ks_log(KS_LOG_DEBUG, "Session (%s) connection added (%s)\n", bs->id, id); ks_log(KS_LOG_DEBUG, "Session (%s) connection added (%s)\n", bs->id, id);
bs->ttl = 0;
return ret; return ret;
} }
@ -282,6 +288,8 @@ KS_DECLARE(ks_status_t) blade_session_connections_remove(blade_session_t *bs, co
} }
} }
if (list_size(&bs->connections) == 0) bs->ttl = ks_time_now() + (5 * KS_USEC_PER_SEC);
return ret; return ret;
} }
@ -314,28 +322,6 @@ ks_status_t blade_session_connections_choose(blade_session_t *bs, cJSON *json, b
return KS_STATUS_SUCCESS; return KS_STATUS_SUCCESS;
} }
KS_DECLARE(ks_status_t) blade_session_send(blade_session_t *bs, cJSON *json)
{
ks_assert(bs);
ks_assert(json);
// @todo check json for "method", if this is an outgoing request then build up the data for a response to lookup the message id and get back to the request
// this can reuse blade_request_t so that when the blade_response_t is passed up the blade_request_t within it is familiar from inbound requests
if (list_empty(&bs->connections)) {
// @todo cache the blade_request_t here if it exists to gaurentee it's cached before a response could be received
blade_session_sending_push(bs, json);
} else {
blade_connection_t *bc = NULL;
if (blade_session_connections_choose(bs, json, &bc) != KS_STATUS_SUCCESS) return KS_STATUS_FAIL;
// @todo cache the blade_request_t here if it exists to gaurentee it's cached before a response could be received
blade_connection_sending_push(bc, json);
blade_connection_read_unlock(bc);
}
return KS_STATUS_SUCCESS;
}
KS_DECLARE(ks_status_t) blade_session_sending_push(blade_session_t *bs, cJSON *json) KS_DECLARE(ks_status_t) blade_session_sending_push(blade_session_t *bs, cJSON *json)
{ {
cJSON *json_copy = NULL; cJSON *json_copy = NULL;
@ -403,30 +389,11 @@ void *blade_session_state_thread(ks_thread_t *thread, void *data)
switch (state) { switch (state) {
case BLADE_SESSION_STATE_DESTROY: case BLADE_SESSION_STATE_DESTROY:
ks_log(KS_LOG_DEBUG, "Session (%s) state destroy\n", bs->id); blade_session_state_on_destroy(bs);
blade_handle_sessions_remove(bs);
blade_session_destroy(&bs);
return NULL; return NULL;
case BLADE_SESSION_STATE_HANGUP: case BLADE_SESSION_STATE_HANGUP:
{ blade_session_state_on_hangup(bs);
ks_log(KS_LOG_DEBUG, "Session (%s) state hangup\n", bs->id); break;
list_iterator_start(&bs->connections);
while (list_iterator_hasnext(&bs->connections)) {
const char *cid = (const char *)list_iterator_next(&bs->connections);
blade_connection_t *bc = blade_handle_connections_get(bs->handle, cid);
ks_assert(bc);
blade_connection_disconnect(bc);
blade_connection_read_unlock(bc);
}
list_iterator_stop(&bs->connections);
while (!list_empty(&bs->connections)) ks_sleep(100);
blade_session_state_set(bs, BLADE_SESSION_STATE_DESTROY);
break;
}
case BLADE_SESSION_STATE_CONNECT: case BLADE_SESSION_STATE_CONNECT:
ks_log(KS_LOG_DEBUG, "Session (%s) state connect\n", bs->id); ks_log(KS_LOG_DEBUG, "Session (%s) state connect\n", bs->id);
ks_sleep_ms(1000); ks_sleep_ms(1000);
@ -440,17 +407,118 @@ void *blade_session_state_thread(ks_thread_t *thread, void *data)
ks_sleep_ms(1000); ks_sleep_ms(1000);
break; break;
case BLADE_SESSION_STATE_READY: case BLADE_SESSION_STATE_READY:
ks_log(KS_LOG_DEBUG, "Session (%s) state ready\n", bs->id); blade_session_state_on_ready(bs);
// @todo pop from session receiving queue and pass into protocol layer through something like blade_protocol_process()
ks_sleep_ms(1000);
break; break;
default: break; default: break;
} }
if (list_empty(&bs->connections) &&
bs->ttl > 0 &&
bs->state != BLADE_SESSION_STATE_HANGUP &&
bs->state != BLADE_SESSION_STATE_DESTROY &&
ks_time_now() >= bs->ttl) {
ks_log(KS_LOG_DEBUG, "Session (%s) TTL timeout\n", bs->id);
blade_session_hangup(bs);
}
} }
return NULL; return NULL;
} }
ks_status_t blade_session_state_on_destroy(blade_session_t *bs)
{
ks_assert(bs);
ks_log(KS_LOG_DEBUG, "Session (%s) state destroy\n", bs->id);
blade_handle_sessions_remove(bs);
blade_session_destroy(&bs);
// @todo ignoring returns for now, see what makes sense later
return KS_STATUS_SUCCESS;
}
ks_status_t blade_session_state_on_hangup(blade_session_t *bs)
{
ks_assert(bs);
ks_log(KS_LOG_DEBUG, "Session (%s) state hangup\n", bs->id);
list_iterator_start(&bs->connections);
while (list_iterator_hasnext(&bs->connections)) {
const char *cid = (const char *)list_iterator_next(&bs->connections);
blade_connection_t *bc = blade_handle_connections_get(bs->handle, cid);
ks_assert(bc);
blade_connection_disconnect(bc);
blade_connection_read_unlock(bc);
}
list_iterator_stop(&bs->connections);
while (!list_empty(&bs->connections)) ks_sleep(100);
blade_session_state_set(bs, BLADE_SESSION_STATE_DESTROY);
return KS_STATUS_SUCCESS;
}
ks_status_t blade_session_state_on_ready(blade_session_t *bs)
{
cJSON *json = NULL;
ks_assert(bs);
ks_log(KS_LOG_DEBUG, "Session (%s) state ready\n", bs->id);
// @todo for now only process messages if there is a connection available
if (list_size(&bs->connections) > 0) {
// @todo may only want to pop once per call to give sending a chance to keep up
while (blade_session_receiving_pop(bs, &json) == KS_STATUS_SUCCESS && json) {
blade_session_process(bs, json);
cJSON_Delete(json);
}
}
ks_sleep_ms(1000);
return KS_STATUS_SUCCESS;
}
KS_DECLARE(ks_status_t) blade_session_send(blade_session_t *bs, cJSON *json)
{
ks_assert(bs);
ks_assert(json);
// @todo check json for "method", if this is an outgoing request then build up the data for a response to lookup the message id and get back to the request
// this can reuse blade_request_t so that when the blade_response_t is passed up the blade_request_t within it is familiar from inbound requests
if (list_empty(&bs->connections)) {
// @todo cache the blade_request_t here if it exists to gaurentee it's cached before a response could be received
blade_session_sending_push(bs, json);
} else {
blade_connection_t *bc = NULL;
if (blade_session_connections_choose(bs, json, &bc) != KS_STATUS_SUCCESS) return KS_STATUS_FAIL;
// @todo cache the blade_request_t here if it exists to gaurentee it's cached before a response could be received
blade_connection_sending_push(bc, json);
blade_connection_read_unlock(bc);
}
return KS_STATUS_SUCCESS;
}
ks_status_t blade_session_process(blade_session_t *bs, cJSON *json)
{
ks_status_t ret = KS_STATUS_SUCCESS;
ks_assert(bs);
ks_assert(json);
ks_log(KS_LOG_DEBUG, "Session (%s) processing\n", bs->id);
// @todo teardown the message, convert into a blade_request_t or blade_response_t
return ret;
}
/* For Emacs: /* For Emacs:
* Local Variables: * Local Variables:
* mode:c * mode:c