feat(relay): bootstrap timeout

This commit is contained in:
Alexey Kasyanchuk
2021-01-06 20:36:40 +03:00
parent 95fc935438
commit 39d12f1d1a

View File

@ -262,7 +262,8 @@ module.exports = function (send, recive, dataDirectory, version, env)
} }
const getServiceJson = (url) => new Promise((resolve) => { const getServiceJson = (url) => new Promise((resolve) => {
http.get(url, (resp) => { let timeout;
const req = http.get(url, (resp) => {
let data = ''; let data = '';
resp.on('data', (chunk) => { resp.on('data', (chunk) => {
@ -270,6 +271,7 @@ module.exports = function (send, recive, dataDirectory, version, env)
}); });
resp.on('end', () => { resp.on('end', () => {
clearTimeout(timeout);
try { try {
resolve(data.length > 0 && JSON.parse(data)) resolve(data.length > 0 && JSON.parse(data))
} catch(e) { } catch(e) {
@ -277,10 +279,16 @@ module.exports = function (send, recive, dataDirectory, version, env)
resolve(false) resolve(false)
} }
}); });
}).on("error", (err) => { });
req.on("error", (err) => {
clearTimeout(timeout);
logTE('http', `${url} error: ` + err.message) logTE('http', `${url} error: ` + err.message)
resolve(false) resolve(false)
}); });
timeout = setTimeout(() => {
logTE('http', `${url} abort by time`)
req.destroy();
}, 3000)
}) })
let p2pBootstrapLoop = null let p2pBootstrapLoop = null
@ -333,7 +341,7 @@ module.exports = function (send, recive, dataDirectory, version, env)
return return
loadBootstrapPeers('https://api.myjson.com/bins/1e5rmh') loadBootstrapPeers('https://api.myjson.com/bins/1e5rmh')
loadBootstrapPeers('https://jsonblob.com/api/jsonBlob/013a4415-3533-11e8-8290-a901f3cf34aa') loadBootstrapPeers('https://jsonblob.com/api/jsonBlob/4d22c8ba-5046-11eb-b13f-81fd0496c154')
}) })
} }
@ -1031,19 +1039,31 @@ module.exports = function (send, recive, dataDirectory, version, env)
'Content-Type' : "application/json", 'Content-Type' : "application/json",
} }
}; };
let timeout;
const req = http.request(options, () => {
logT('close', 'bootstrap peers saved to', host) logT('close', 'bootstrap peers saved to', host)
const req = http.request(options, resolve); clearTimeout(timeout)
req.on('error', resolve) resolve()
});
req.on('error', () => {
logTE('close', 'cant save bootstrap pears to', host)
clearTimeout(timeout)
resolve()
})
req.end(JSON.stringify({ req.end(JSON.stringify({
bootstrap: peersEncripted, bootstrap: peersEncripted,
bootstrapMap: encryptor.encrypt(bootstrapMap), bootstrapMap: encryptor.encrypt(bootstrapMap),
relays: encryptor.encrypt(p2p.relays()) relays: encryptor.encrypt(p2p.relays())
})) }))
setTimeout(() => {
logTE('close', 'abort by time', host)
req.destroy();
}, 4000)
}) })
await Promise.all([ await Promise.all([
saveBootstrapPeers('api.myjson.com', '/bins/1e5rmh'), saveBootstrapPeers('api.myjson.com', '/bins/1e5rmh'),
saveBootstrapPeers('jsonblob.com', '/api/jsonBlob/013a4415-3533-11e8-8290-a901f3cf34aa') saveBootstrapPeers('jsonblob.com', '/api/jsonBlob/4d22c8ba-5046-11eb-b13f-81fd0496c154')
]) ])
} }
} }