blobs.createWants again without setTimeout to fix the test.
git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@4105 ed5197a5-7fde-0310-b194-c3ffbd925b24
This commit is contained in:
parent
ff06e91ac8
commit
4c01f23ee8
35
core/ssb.js
35
core/ssb.js
@ -2,6 +2,8 @@
|
|||||||
var g_wants_requests = {};
|
var g_wants_requests = {};
|
||||||
var g_database = new Database('core');
|
var g_database = new Database('core');
|
||||||
let g_attendants = {};
|
let g_attendants = {};
|
||||||
|
let g_blob_wants_sent = 0;
|
||||||
|
let g_blob_last_requested = '';
|
||||||
const k_use_create_history_stream = false;
|
const k_use_create_history_stream = false;
|
||||||
const k_blobs_concurrent_target = 8;
|
const k_blobs_concurrent_target = 8;
|
||||||
|
|
||||||
@ -80,15 +82,25 @@ function send_blobs_create_wants(connection) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
Object.keys(message.message).forEach(function(id) {
|
Object.keys(message.message).forEach(function(id) {
|
||||||
|
g_blob_wants_sent--;
|
||||||
|
if (g_blob_wants_sent == 0) {
|
||||||
|
request_more_blobs(g_wants_requests[connection.id]);
|
||||||
|
}
|
||||||
if (message.message[id] < 0) {
|
if (message.message[id] < 0) {
|
||||||
|
let blob;
|
||||||
if (g_wants_requests[connection.id]) {
|
if (g_wants_requests[connection.id]) {
|
||||||
let blob = ssb.blobGet(id);
|
blob = ssb.blobGet(id);
|
||||||
if (blob) {
|
if (blob) {
|
||||||
let out_message = {};
|
let out_message = {};
|
||||||
out_message[id] = blob.byteLength;
|
out_message[id] = blob.byteLength;
|
||||||
g_wants_requests[connection.id].send_json(out_message);
|
g_wants_requests[connection.id].send_json(out_message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (!blob && message.message[id] == -1) {
|
||||||
|
let out_message = {};
|
||||||
|
out_message[id] = -2;
|
||||||
|
g_wants_requests[connection.id].send_json(out_message);
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
let expected_bytes = message.message[id];
|
let expected_bytes = message.message[id];
|
||||||
connection.blob_get_queue[id] = expected_bytes;
|
connection.blob_get_queue[id] = expected_bytes;
|
||||||
@ -143,33 +155,24 @@ function blob_want_discovered(request, id) {
|
|||||||
}
|
}
|
||||||
var message = {};
|
var message = {};
|
||||||
message[id] = -1;
|
message[id] = -1;
|
||||||
|
g_blob_wants_sent++;
|
||||||
request.send_json(message);
|
request.send_json(message);
|
||||||
}
|
}
|
||||||
|
|
||||||
function sleep(ms) {
|
function request_more_blobs(request) {
|
||||||
return new Promise(function(resolve, reject) {
|
return ssb.sqlStream(
|
||||||
setTimeout(x => resolve(), ms);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
async function requestMoreBlobs(request) {
|
|
||||||
let last = '';
|
|
||||||
while (true) {
|
|
||||||
await ssb.sqlStream(
|
|
||||||
'SELECT id FROM blob_wants WHERE id > ? ORDER BY id LIMIT 32',
|
'SELECT id FROM blob_wants WHERE id > ? ORDER BY id LIMIT 32',
|
||||||
[last],
|
[g_blob_last_requested],
|
||||||
function(row) {
|
function(row) {
|
||||||
blob_want_discovered(request, row.id);
|
blob_want_discovered(request, row.id);
|
||||||
last = row.id;
|
g_blob_last_requested = row.id;
|
||||||
});
|
});
|
||||||
await sleep(1000);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ssb.addRpc(['blobs', 'createWants'], function(request) {
|
ssb.addRpc(['blobs', 'createWants'], function(request) {
|
||||||
g_wants_requests[request.connection.id] = request;
|
g_wants_requests[request.connection.id] = request;
|
||||||
ssb.addEventListener('blob_want_added', id => blob_want_discovered(request, id));
|
ssb.addEventListener('blob_want_added', id => blob_want_discovered(request, id));
|
||||||
requestMoreBlobs(request);
|
request_more_blobs(request);
|
||||||
});
|
});
|
||||||
|
|
||||||
function notify_attendant_changed(id, type) {
|
function notify_attendant_changed(id, type) {
|
||||||
|
Loading…
Reference in New Issue
Block a user