ssb: Show activity for each muxrpc request.
All checks were successful
Build Tilde Friends / Build-All (push) Successful in 16m23s

This commit is contained in:
Cory McWilliams 2024-10-02 20:43:51 -04:00
parent 9a5db2ec51
commit 1bb9d737d8
4 changed files with 34 additions and 4 deletions

View File

@ -1,5 +1,5 @@
{
"type": "tildefriends-app",
"emoji": "🐌",
"previous": "&7L314QLuVl8kNbFIWuXXeLqIVefUe5S66WvEmOMQb2U=.sha256"
"previous": "&nPhjx+lscaqxp3pwcpLlQ2HyJDPQPssdfzcFKsPEMsI=.sha256"
}

View File

@ -136,7 +136,8 @@ class TfTabConnectionsElement extends LitElement {
<div>
${requests.map(
(x) => html`
<span class="w3-tag w3-small"
<span
class=${'w3-tag w3-small ' + (x.active ? 'w3-blue' : 'w3-black')}
>${x.request_number > 0 ? '🟩' : '🟥'} ${x.name}
<span
class="w3-badge w3-white"

View File

@ -112,6 +112,7 @@ typedef struct _tf_ssb_request_t
tf_ssb_callback_cleanup_t* cleanup;
void* user_data;
tf_ssb_connection_t* dependent_connection;
time_t last_active;
int32_t request_number;
} tf_ssb_request_t;
@ -211,6 +212,7 @@ typedef struct _tf_ssb_t
uv_timer_t broadcast_cleanup_timer;
uv_timer_t broadcast_timer;
uv_timer_t trace_timer;
uv_timer_t request_activity_timer;
uv_tcp_t server;
uint8_t network_key[32];
@ -679,6 +681,12 @@ static bool _tf_ssb_connection_get_request_callback(tf_ssb_connection_t* connect
return false;
}
static void _tf_ssb_request_activity_timer(uv_timer_t* timer)
{
tf_ssb_t* ssb = timer->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;

View File

@ -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)
{