Skip to content

Commit

Permalink
sdk/node: add cleanup listeners to socket, re-add reconnection loop f…
Browse files Browse the repository at this point in the history
…or dialed nodes
  • Loading branch information
lithdew committed Jun 17, 2020
1 parent f79206a commit 4210be1
Showing 1 changed file with 30 additions and 30 deletions.
60 changes: 30 additions & 30 deletions nodejs/src/flatend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,40 +212,13 @@ export class Node {
if (!sock) {
sock = await MonteSocket.connect({host, port});

this.#clients.set(addr, sock);

sock.on('data', this._data.bind(this));
sock.on('error', console.error);

this.#clients.set(addr, sock);
sock.once('end', () => this.#clients.delete(addr));
}

// client.sock.once('end', () => {
// client!.services.forEach(service => this.#services.get(service)?.delete(client!));
//
// if (client!.id) this.#table.delete(client!.id.publicKey);
//
// this.#providers.delete(client!.sock);
// this.#clients.delete(client!.addr!);
//
// let count = 8;
//
// const reconnect = async () => {
// if (count-- === 0) {
// console.log(`Tried 8 times reconnecting to ${client!.addr}. Giving up.`);
// return;
// }
//
// console.log(`Trying to reconnect to ${client!.addr}. Sleeping for 1s.`);
//
// try {
// await this.dial(client!.addr!);
// } catch (err) {
// setTimeout(reconnect, 1000);
// }
// };
//
// setTimeout(reconnect, 1000);
// });

await this.probe(sock);
}

Expand Down Expand Up @@ -274,6 +247,12 @@ export class Node {
if (!provider) {
provider = new Provider(id, sock, new Set<string>(packet.services));
this.#providers.set(sock, provider);

sock.once('end', () => {
if (id) this.#table.delete(id.publicKey);
provider!.services.forEach(service => this.#services.get(service)?.delete(provider!));
this.#providers.delete(sock);
});
}

provider.services.forEach(service => {
Expand All @@ -297,6 +276,27 @@ export class Node {
const provider = this.onPeerJoin(sock, packet);

console.log(`Successfully dialed ${provider.addr}. Services: [${packet.services.join(', ')}]`);

sock.once('end', () => {
let count = 8;

const reconnect = async () => {
if (count-- === 0) {
console.log(`Tried 8 times reconnecting to ${provider.addr}. Giving up.`);
return;
}

console.log(`Trying to reconnect to ${provider.addr}. Sleeping for 1s.`);

try {
await this.dial(provider.addr!);
} catch (err) {
setTimeout(reconnect, 1000);
}
};

setTimeout(reconnect, 1000);
});
}

private _data({sock, seq, body}: { sock: MonteSocket, seq: number, body: Buffer }) {
Expand Down

0 comments on commit 4210be1

Please sign in to comment.