Send history streams in batches. Should block the main thread less.

git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@4127 ed5197a5-7fde-0310-b194-c3ffbd925b24
This commit is contained in:
2023-01-17 02:17:29 +00:00
parent 73863f9418
commit 67d34bf70e
3 changed files with 88 additions and 2 deletions

View File

@ -204,6 +204,12 @@ typedef struct _tf_ssb_connection_message_request_t
bool keys;
} tf_ssb_connection_message_request_t;
typedef struct _tf_ssb_connection_scheduled_t
{
tf_ssb_scheduled_callback_t* callback;
void* user_data;
} tf_ssb_connection_scheduled_t;
typedef struct _tf_ssb_connection_t
{
tf_ssb_t* ssb;
@ -266,6 +272,9 @@ typedef struct _tf_ssb_connection_t
tf_ssb_connection_message_request_t* message_requests;
int message_requests_count;
tf_ssb_connection_scheduled_t* scheduled;
int scheduled_count;
} tf_ssb_connection_t;
static JSClassID _connection_class_id;
@ -477,6 +486,17 @@ static void _tf_ssb_connection_box_stream_send(tf_ssb_connection_t* connection,
}
}
void tf_ssb_connection_schedule_idle(tf_ssb_connection_t* connection, tf_ssb_scheduled_callback_t* callback, void* user_data)
{
connection->scheduled = tf_resize_vec(connection->scheduled, sizeof(tf_ssb_connection_scheduled_t) * (connection->scheduled_count + 1));
connection->scheduled[connection->scheduled_count++] = (tf_ssb_connection_scheduled_t)
{
.callback = callback,
.user_data = user_data,
};
uv_async_send(&connection->async);
}
static int _request_compare(const void* a, const void* b)
{
int32_t ai = *(const int32_t*)a;
@ -1062,6 +1082,13 @@ bool tf_ssb_connection_is_client(tf_ssb_connection_t* connection)
return connection->state == k_tf_ssb_state_verified;
}
bool tf_ssb_connection_is_connected(tf_ssb_connection_t* connection)
{
return
connection->state == k_tf_ssb_state_verified ||
connection->state == k_tf_ssb_state_server_verified;
}
const char* tf_ssb_connection_get_host(tf_ssb_connection_t* connection)
{
return connection->host;
@ -1607,6 +1634,17 @@ void tf_ssb_append_message(tf_ssb_t* ssb, JSValue message)
tf_ssb_append_message_with_keys(ssb, author, ssb->priv, message);
}
static void _tf_ssb_connection_dispatch_scheduled(tf_ssb_connection_t* connection)
{
if (connection->scheduled_count)
{
tf_ssb_connection_scheduled_t scheduled = connection->scheduled[0];
memmove(connection->scheduled, connection->scheduled + 1, sizeof(tf_ssb_connection_scheduled_t) * (connection->scheduled_count - 1));
connection->scheduled_count--;
scheduled.callback(connection, scheduled.user_data);
}
}
void _tf_ssb_connection_destroy(tf_ssb_connection_t* connection, const char* reason)
{
tf_ssb_t* ssb = connection->ssb;
@ -1614,6 +1652,12 @@ void _tf_ssb_connection_destroy(tf_ssb_connection_t* connection, const char* rea
{
connection->destroy_reason = reason;
}
while (connection->scheduled_count)
{
_tf_ssb_connection_dispatch_scheduled(connection);
}
tf_free(connection->scheduled);
connection->scheduled = NULL;
while (connection->requests)
{
_tf_ssb_connection_remove_request(connection, connection->requests->request_number);
@ -2185,6 +2229,7 @@ static void _tf_ssb_connection_process_message_async(uv_async_t* async)
{
uv_async_send(&connection->async);
}
_tf_ssb_connection_dispatch_scheduled(connection);
}
tf_ssb_connection_t* tf_ssb_connection_create(tf_ssb_t* ssb, const char* host, const struct sockaddr_in* addr, const uint8_t* public_key)