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:
		@@ -15,6 +15,8 @@
 | 
			
		||||
#define _countof(a) ((int)(sizeof((a)) / sizeof(*(a))))
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
static void _tf_ssb_connection_send_history_stream(tf_ssb_connection_t* connection, int32_t request_number, const char* author, int64_t sequence, bool keys);
 | 
			
		||||
 | 
			
		||||
static void _tf_ssb_rpc_gossip_ping_callback(tf_ssb_connection_t* connection, uint8_t flags, int32_t request_number, JSValue args, const uint8_t* message, size_t size, void* user_data)
 | 
			
		||||
{
 | 
			
		||||
	char buffer[256];
 | 
			
		||||
@@ -675,20 +677,42 @@ static void _tf_ssb_rpc_connection_tunnel_isRoom_callback(tf_ssb_connection_t* c
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
typedef struct _tf_ssb_connection_send_history_stream_t
 | 
			
		||||
{
 | 
			
		||||
	int32_t request_number;
 | 
			
		||||
	char author[k_id_base64_len];
 | 
			
		||||
	int64_t sequence;
 | 
			
		||||
	bool keys;
 | 
			
		||||
} tf_ssb_connection_send_history_stream_t;
 | 
			
		||||
 | 
			
		||||
static void _tf_ssb_connection_send_history_stream_callback(tf_ssb_connection_t* connection, void* user_data)
 | 
			
		||||
{
 | 
			
		||||
	tf_ssb_connection_send_history_stream_t* request = user_data;
 | 
			
		||||
	if (tf_ssb_connection_is_connected(connection))
 | 
			
		||||
	{
 | 
			
		||||
		_tf_ssb_connection_send_history_stream(connection, request->request_number, request->author, request->sequence, request->keys);
 | 
			
		||||
	}
 | 
			
		||||
	tf_free(request);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void _tf_ssb_connection_send_history_stream(tf_ssb_connection_t* connection, int32_t request_number, const char* author, int64_t sequence, bool keys)
 | 
			
		||||
{
 | 
			
		||||
	tf_ssb_t* ssb = tf_ssb_connection_get_ssb(connection);
 | 
			
		||||
	JSContext* context = tf_ssb_get_context(ssb);
 | 
			
		||||
	sqlite3* db = tf_ssb_get_db(ssb);
 | 
			
		||||
	sqlite3_stmt* statement;
 | 
			
		||||
	if (sqlite3_prepare(db, "SELECT previous, author, id, sequence, timestamp, hash, content, signature, sequence_before_author FROM messages WHERE author = ?1 AND sequence >= ?2 ORDER BY sequence", -1, &statement, NULL) == SQLITE_OK)
 | 
			
		||||
	const int k_max = 32;
 | 
			
		||||
	int64_t max_sequence_seen = 0;
 | 
			
		||||
	if (sqlite3_prepare(db, "SELECT previous, author, id, sequence, timestamp, hash, content, signature, sequence_before_author FROM messages WHERE author = ?1 AND sequence >= ?2 AND sequence < ?3 ORDER BY sequence", -1, &statement, NULL) == SQLITE_OK)
 | 
			
		||||
	{
 | 
			
		||||
		if (sqlite3_bind_text(statement, 1, author, -1, NULL) == SQLITE_OK &&
 | 
			
		||||
			sqlite3_bind_int64(statement, 2, sequence) == SQLITE_OK)
 | 
			
		||||
			sqlite3_bind_int64(statement, 2, sequence) == SQLITE_OK &&
 | 
			
		||||
			sqlite3_bind_int64(statement, 3, sequence + k_max) == SQLITE_OK)
 | 
			
		||||
		{
 | 
			
		||||
			while (sqlite3_step(statement) == SQLITE_ROW)
 | 
			
		||||
			{
 | 
			
		||||
				JSValue message = JS_UNDEFINED;
 | 
			
		||||
				max_sequence_seen = sqlite3_column_int64(statement, 3);
 | 
			
		||||
 | 
			
		||||
				JSValue formatted = tf_ssb_format_message(
 | 
			
		||||
					context,
 | 
			
		||||
@@ -717,6 +741,19 @@ static void _tf_ssb_connection_send_history_stream(tf_ssb_connection_t* connecti
 | 
			
		||||
		}
 | 
			
		||||
		sqlite3_finalize(statement);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if (max_sequence_seen == sequence + k_max - 1)
 | 
			
		||||
	{
 | 
			
		||||
		tf_ssb_connection_send_history_stream_t* async = tf_malloc(sizeof(tf_ssb_connection_send_history_stream_t));
 | 
			
		||||
		*async = (tf_ssb_connection_send_history_stream_t)
 | 
			
		||||
		{
 | 
			
		||||
			.request_number = request_number,
 | 
			
		||||
			.sequence = max_sequence_seen + 1,
 | 
			
		||||
			.keys = keys,
 | 
			
		||||
		};
 | 
			
		||||
		snprintf(async->author, sizeof(async->author), "%s", author);
 | 
			
		||||
		tf_ssb_connection_schedule_idle(connection, _tf_ssb_connection_send_history_stream_callback, async);
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void _tf_ssb_rpc_createHistoryStream(tf_ssb_connection_t* connection, uint8_t flags, int32_t request_number, JSValue args, const uint8_t* message, size_t size, void* user_data)
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user