From 1bb9d737d8c7ff6b7019179b7a166eb78df03bdc Mon Sep 17 00:00:00 2001 From: Cory McWilliams Date: Wed, 2 Oct 2024 20:43:51 -0400 Subject: [PATCH] ssb: Show activity for each muxrpc request. --- apps/ssb.json | 2 +- apps/ssb/tf-tab-connections.js | 3 ++- src/ssb.c | 28 ++++++++++++++++++++++++++-- src/ssb.connections.c | 5 +++++ 4 files changed, 34 insertions(+), 4 deletions(-) diff --git a/apps/ssb.json b/apps/ssb.json index 8745153b..f6d7bcc5 100644 --- a/apps/ssb.json +++ b/apps/ssb.json @@ -1,5 +1,5 @@ { "type": "tildefriends-app", "emoji": "🐌", - "previous": "&7L314QLuVl8kNbFIWuXXeLqIVefUe5S66WvEmOMQb2U=.sha256" + "previous": "&nPhjx+lscaqxp3pwcpLlQ2HyJDPQPssdfzcFKsPEMsI=.sha256" } diff --git a/apps/ssb/tf-tab-connections.js b/apps/ssb/tf-tab-connections.js index b6a4c282..865e0564 100644 --- a/apps/ssb/tf-tab-connections.js +++ b/apps/ssb/tf-tab-connections.js @@ -136,7 +136,8 @@ class TfTabConnectionsElement extends LitElement {
${requests.map( (x) => html` - ${x.request_number > 0 ? '🟩' : '🟥'} ${x.name} data; + _tf_ssb_notify_connections_changed(ssb, k_tf_ssb_change_update, NULL); +} + void tf_ssb_connection_add_request(tf_ssb_connection_t* connection, int32_t request_number, const char* name, tf_ssb_rpc_callback_t* callback, tf_ssb_callback_cleanup_t* cleanup, void* user_data, tf_ssb_connection_t* dependent_connection) { @@ -690,6 +698,7 @@ void tf_ssb_connection_add_request(tf_ssb_connection_t* connection, int32_t requ assert(!existing->cleanup); assert(!existing->user_data); assert(!existing->dependent_connection); + existing->last_active = time(NULL); existing->callback = callback; existing->cleanup = cleanup; existing->user_data = user_data; @@ -704,6 +713,7 @@ void tf_ssb_connection_add_request(tf_ssb_connection_t* connection, int32_t requ .cleanup = cleanup, .user_data = user_data, .dependent_connection = dependent_connection, + .last_active = time(NULL), }; snprintf(request.name, sizeof(request.name), "%s", name); int index = tf_util_insert_index(&request_number, connection->requests, connection->requests_count, sizeof(tf_ssb_request_t), _request_compare); @@ -714,9 +724,12 @@ void tf_ssb_connection_add_request(tf_ssb_connection_t* connection, int32_t requ } connection->requests[index] = request; connection->requests_count++; - connection->ssb->request_count++; } + if (uv_timer_get_due_in(&connection->ssb->request_activity_timer) == 0) + { + uv_timer_start(&connection->ssb->request_activity_timer, _tf_ssb_request_activity_timer, 3000, 0); + } _tf_ssb_notify_connections_changed(connection->ssb, k_tf_ssb_change_update, connection); } @@ -2216,6 +2229,11 @@ tf_ssb_t* tf_ssb_create(uv_loop_t* loop, JSContext* context, const char* db_path uv_timer_start(&ssb->trace_timer, _tf_ssb_trace_timer, 100, 100); uv_unref((uv_handle_t*)&ssb->trace_timer); + ssb->request_activity_timer.data = ssb; + uv_timer_init(ssb->loop, &ssb->request_activity_timer); + uv_timer_start(&ssb->request_activity_timer, _tf_ssb_request_activity_timer, 3000, 0); + uv_unref((uv_handle_t*)&ssb->request_activity_timer); + if (!_tf_ssb_load_keys(ssb)) { tf_printf("Generating a new keypair.\n"); @@ -2418,6 +2436,11 @@ void tf_ssb_destroy(tf_ssb_t* ssb) uv_close((uv_handle_t*)&ssb->trace_timer, _tf_ssb_on_handle_close); } + if (ssb->request_activity_timer.data && !uv_is_closing((uv_handle_t*)&ssb->request_activity_timer)) + { + uv_close((uv_handle_t*)&ssb->request_activity_timer, _tf_ssb_on_handle_close); + } + if (ssb->server.data && !uv_is_closing((uv_handle_t*)&ssb->server)) { uv_close((uv_handle_t*)&ssb->server, _tf_ssb_on_handle_close); @@ -2434,7 +2457,7 @@ void tf_ssb_destroy(tf_ssb_t* ssb) tf_printf("Waiting for closes.\n"); while (ssb->broadcast_listener.data || ssb->broadcast_sender.data || ssb->broadcast_timer.data || ssb->broadcast_cleanup_timer.data || ssb->trace_timer.data || - ssb->server.data || ssb->ref_count) + ssb->server.data || ssb->ref_count || ssb->request_activity_timer.data) { uv_run(ssb->loop, UV_RUN_ONCE); } @@ -4185,6 +4208,7 @@ JSValue tf_ssb_connection_requests_to_object(tf_ssb_connection_t* connection) JSValue request = JS_NewObject(context); JS_SetPropertyStr(context, request, "name", JS_NewString(context, connection->requests[i].name)); JS_SetPropertyStr(context, request, "request_number", JS_NewInt32(context, connection->requests[i].request_number)); + JS_SetPropertyStr(context, request, "active", JS_NewBool(context, time(NULL) - connection->requests[i].last_active < 3)); JS_SetPropertyUint32(context, object, i, request); } return object; diff --git a/src/ssb.connections.c b/src/ssb.connections.c index 8ab356be..547ee27a 100644 --- a/src/ssb.connections.c +++ b/src/ssb.connections.c @@ -21,6 +21,11 @@ typedef struct _tf_ssb_connections_t static void _tf_ssb_connections_changed_callback(tf_ssb_t* ssb, tf_ssb_change_t change, tf_ssb_connection_t* connection, void* user_data) { + if (!connection) + { + return; + } + tf_ssb_connections_t* connections = user_data; switch (change) {