Make blob store actually not block the main thread.

git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@4352 ed5197a5-7fde-0310-b194-c3ffbd925b24
This commit is contained in:
2023-07-18 23:46:15 +00:00
parent 7fe8f66fd3
commit b0cd58f5aa
4 changed files with 123 additions and 44 deletions

View File

@ -423,24 +423,15 @@ typedef struct _blobs_get_t
size_t received;
size_t expected_size;
bool done;
bool storing;
tf_ssb_t* ssb;
uv_work_t work;
uint8_t buffer[];
} blobs_get_t;
static void _tf_ssb_rpc_blob_store_work(uv_work_t* work)
static void _tf_ssb_rpc_blob_store_callback(const char* id, bool is_new, void* user_data)
{
blobs_get_t* get = work->data;
tf_ssb_db_blob_store(get->ssb, get->buffer, get->received, NULL, 0, NULL);
}
static void _tf_ssb_rpc_blob_store_after_work(uv_work_t* work, int status)
{
blobs_get_t* get = work->data;
if (status != 0)
{
tf_printf("uv_queue_work failed: %s\n", uv_strerror(status));
}
blobs_get_t* get = user_data;
get->storing = false;
if (get->done)
{
tf_free(get);
@ -459,21 +450,13 @@ static void _tf_ssb_rpc_connection_blobs_get_callback(tf_ssb_connection_t* conne
}
else if ((flags & k_ssb_rpc_mask_type) == k_ssb_rpc_flag_json)
{
bool stored = false;
if (JS_ToBool(context, args))
{
get->work.data = get;
int r = uv_queue_work(tf_ssb_get_loop(ssb), &get->work, _tf_ssb_rpc_blob_store_work, _tf_ssb_rpc_blob_store_after_work);
if (r)
{
tf_printf("uv_queue_work failed: %s\n", uv_strerror(r));
get->work.data = NULL;
}
else
{
stored = true;
}
get->storing = true;
tf_ssb_db_blob_store_async(ssb, get->buffer, get->received, _tf_ssb_rpc_blob_store_callback, get);
}
/* TODO: Should we send the response in the callback? */
bool stored = true;
tf_ssb_connection_rpc_send(
connection,
k_ssb_rpc_flag_json | k_ssb_rpc_flag_stream | k_ssb_rpc_flag_end_error,
@ -490,7 +473,7 @@ static void _tf_ssb_rpc_connection_blobs_get_cleanup(tf_ssb_t* ssb, void* user_d
{
blobs_get_t* get = user_data;
get->done = true;
if (!get->work.data)
if (!get->storing)
{
tf_free(get);
}