Replace generator function and other various fixes

pull/270/head
sachaaaaa 6 years ago
parent 09a9cfbf37
commit 5f77f751d9

@ -5,7 +5,8 @@
"node": false "node": false
}, },
"globals": { "globals": {
"console": true "console": true,
"setTimeout": true
}, },
"parserOptions": { "parserOptions": {
"sourceType": "module" "sourceType": "module"

@ -9,7 +9,7 @@ const { rpc } = require('./loki_rpc');
const MINIMUM_SUCCESSFUL_REQUESTS = 2; const MINIMUM_SUCCESSFUL_REQUESTS = 2;
const LOKI_LONGPOLL_HEADER = 'X-Loki-Long-Poll'; const LOKI_LONGPOLL_HEADER = 'X-Loki-Long-Poll';
async function sleep_for(time) { function sleepFor(time) {
return new Promise(resolve => { return new Promise(resolve => {
setTimeout(() => resolve(), time); setTimeout(() => resolve(), time);
}); });
@ -177,7 +177,7 @@ class LokiMessageAPI {
log.info(`Successful storage message to ${pubKey}`); log.info(`Successful storage message to ${pubKey}`);
} }
async *retrieveNextMessage(nodeUrl) { async retrieveNextMessages(nodeUrl, nodeData, ourKey) {
const params = { const params = {
pubKey: ourKey, pubKey: ourKey,
lastHash: nodeData.lastHash || '', lastHash: nodeData.lastHash || '',
@ -188,7 +188,7 @@ class LokiMessageAPI {
[LOKI_LONGPOLL_HEADER]: true, [LOKI_LONGPOLL_HEADER]: true,
}, },
}; };
while (true) {
const result = await rpc( const result = await rpc(
`http://${nodeUrl}`, `http://${nodeUrl}`,
this.snodeServerPort, this.snodeServerPort,
@ -201,29 +201,39 @@ class LokiMessageAPI {
filterIncomingMessages(result.messages) filterIncomingMessages(result.messages)
); );
if (filteredMessages.length) { if (filteredMessages.length) {
yield filteredMessages; return filteredMessages;
}
} }
} }
return [];
} }
async openConnection(callback) { async openConnection(callback) {
const ourKey = window.textsecure.storage.user.getNumber();
while (this.ourSwarmNodes.length > 0) { while (this.ourSwarmNodes.length > 0) {
const url = this.ourSwarmNodes.pop(); const url = Object.keys(this.ourSwarmNodes)[0];
const successive_failures = 0; const nodeData = this.ourSwarmNodes[url];
while (true) { delete this.ourSwarmNodes[url];
// loop breaks upon error let successiveFailures = 0;
while (successiveFailures < 3) {
await sleepFor(successiveFailures * 1000);
try { try {
for await (let messages of retrieveNextMessages(url)) { const messages = await this.retrieveNextMessages(
const lastMessage = _.last(message.messages); url,
nodeData,
ourKey
);
successiveFailures = 0;
if (messages.length) {
const lastMessage = _.last(messages);
nodeData.lashHash = lastMessage.hash;
lokiSnodeAPI.updateLastHash( lokiSnodeAPI.updateLastHash(
url, url,
lastMessage.hash, lastMessage.hash,
lastMessage.expiration lastMessage.expiration
); );
callback(messages);
successive_failures = 0;
} }
callback(messages);
} catch (e) { } catch (e) {
log.warn('Loki retrieve messages:', e); log.warn('Loki retrieve messages:', e);
if (e instanceof textsecure.WrongSwarmError) { if (e instanceof textsecure.WrongSwarmError) {
@ -233,17 +243,12 @@ class LokiMessageAPI {
break; break;
} else if (e instanceof textsecure.NotFoundError) { } else if (e instanceof textsecure.NotFoundError) {
// DNS/Lokinet error, needs to bubble up // DNS/Lokinet error, needs to bubble up
throw new window.textsecure.DNSResolutionError('Retrieving messages'); throw new window.textsecure.DNSResolutionError(
'Retrieving messages'
);
} }
successiveFailures += 1;
} }
successive_failures += 1;
if (successive_failures >= 3)
// Try another snode
break;
await sleep_for(successive_failures * 1000);
} }
} }
} }
@ -251,6 +256,8 @@ class LokiMessageAPI {
async startLongPolling(numConnections, callback) { async startLongPolling(numConnections, callback) {
this.ourSwarmNodes = await lokiSnodeAPI.getOurSwarmNodes(); this.ourSwarmNodes = await lokiSnodeAPI.getOurSwarmNodes();
const promises = [];
for (let i = 0; i < numConnections; i += 1) for (let i = 0; i < numConnections; i += 1)
promises.push(this.openConnection(callback)); promises.push(this.openConnection(callback));
@ -269,22 +276,6 @@ class LokiMessageAPI {
let ourSwarmNodes = await lokiSnodeAPI.getOurSwarmNodes(); let ourSwarmNodes = await lokiSnodeAPI.getOurSwarmNodes();
const filterIncomingMessages = async messages => {
const incomingHashes = messages.map(m => m.hash);
const dupHashes = await window.Signal.Data.getSeenMessagesByHashList(
incomingHashes
);
const newMessages = messages.filter(m => !dupHashes.includes(m.hash));
if (newMessages.length) {
const newHashes = newMessages.map(m => ({
expiresAt: m.expiration,
hash: m.hash,
}));
await window.Signal.Data.saveSeenMessageHashes(newHashes);
}
return newMessages;
};
const nodeComplete = nodeUrl => { const nodeComplete = nodeUrl => {
completedNodes.push(nodeUrl); completedNodes.push(nodeUrl);
delete ourSwarmNodes[nodeUrl]; delete ourSwarmNodes[nodeUrl];

@ -83,17 +83,14 @@
// This blocking call will return only when all attempts // This blocking call will return only when all attempts
// at reaching snodes are exhausted or a DNS error occured // at reaching snodes are exhausted or a DNS error occured
try { try {
await server.startLongPolling( await server.startLongPolling(NUM_CONCURRENT_CONNECTIONS, messages => {
NUM_CONCURRENT_CONNECTIONS,
messages => {
connected = true; connected = true;
callback(connected); callback(connected);
messages.forEach(message => { messages.forEach(message => {
const { data } = message; const { data } = message;
this.handleMessage(data); this.handleMessage(data);
}); });
} });
);
} catch (e) { } catch (e) {
// we'll try again anyway // we'll try again anyway
} }

Loading…
Cancel
Save