Log backend connections

This commit is contained in:
maciejhirsz
2018-07-26 15:26:14 +02:00
parent a04fae4de4
commit 2f0f250d81
5 changed files with 28 additions and 5 deletions
+4
View File
@@ -88,5 +88,9 @@ export default class Aggregator {
for (const chain of this.chains.values()) { for (const chain of this.chains.values()) {
chain.timeoutCheck(now); chain.timeoutCheck(now);
} }
for (const feed of this.feeds.values()) {
feed.ping();
}
} }
} }
+13 -2
View File
@@ -2,7 +2,7 @@ import * as WebSocket from 'ws';
import * as EventEmitter from 'events'; import * as EventEmitter from 'events';
import Node from './Node'; import Node from './Node';
import Chain from './Chain'; import Chain from './Chain';
import { VERSION, timestamp, Maybe, FeedMessage, Types, idGenerator } from '@dotstats/common'; import { VERSION, noop, timestamp, Maybe, FeedMessage, Types, idGenerator } from '@dotstats/common';
import { Location } from './location'; import { Location } from './location';
const nextId = idGenerator<Types.FeedId>(); const nextId = idGenerator<Types.FeedId>();
@@ -16,6 +16,7 @@ export default class Feed {
private socket: WebSocket; private socket: WebSocket;
private messages: Array<FeedMessage.Message> = []; private messages: Array<FeedMessage.Message> = [];
private waitingForPong = false;
constructor(socket: WebSocket) { constructor(socket: WebSocket) {
this.id = nextId(); this.id = nextId();
@@ -24,6 +25,7 @@ export default class Feed {
socket.on('message', (data) => this.handleCommand(data.toString())); socket.on('message', (data) => this.handleCommand(data.toString()));
socket.on('error', () => this.disconnect()); socket.on('error', () => this.disconnect());
socket.on('close', () => this.disconnect()); socket.on('close', () => this.disconnect());
socket.on('pong', () => this.waitingForPong = false);
} }
public static feedVersion(): FeedMessage.Message { public static feedVersion(): FeedMessage.Message {
@@ -131,6 +133,15 @@ export default class Feed {
} }
} }
public ping() {
if (this.waitingForPong) {
this.disconnect();
return;
}
this.waitingForPong = true;
this.socket.ping(noop);
}
private sendMessages = () => { private sendMessages = () => {
const data = FeedMessage.serialize(this.messages); const data = FeedMessage.serialize(this.messages);
this.messages = []; this.messages = [];
@@ -165,7 +176,7 @@ export default class Feed {
private disconnect() { private disconnect() {
this.socket.removeAllListeners(); this.socket.removeAllListeners();
this.socket.close(); this.socket.terminate();
this.events.emit('disconnect'); this.events.emit('disconnect');
} }
+1 -3
View File
@@ -1,6 +1,6 @@
import * as WebSocket from 'ws'; import * as WebSocket from 'ws';
import * as EventEmitter from 'events'; import * as EventEmitter from 'events';
import { timestamp, Maybe, Types, idGenerator } from '@dotstats/common'; import { noop, timestamp, Maybe, Types, idGenerator } from '@dotstats/common';
import { parseMessage, getBestBlock, Message, BestBlock, SystemInterval } from './message'; import { parseMessage, getBestBlock, Message, BestBlock, SystemInterval } from './message';
import { locate, Location } from './location'; import { locate, Location } from './location';
@@ -14,8 +14,6 @@ export interface NodeEvents {
emit(event: 'location', location: Location): void; emit(event: 'location', location: Location): void;
} }
function noop() {}
export default class Node { export default class Node {
public readonly id: Types.NodeId; public readonly id: Types.NodeId;
public readonly name: Types.NodeName; public readonly name: Types.NodeName;
+8
View File
@@ -33,6 +33,14 @@ incomingTelemetry.on('connection', async (socket, req) => {
} }
}); });
function logClients() {
const feed = telemetryFeed.clients.size;
const node = incomingTelemetry.clients.size;
console.log(`[System] ${feed} open telemetry connections; ${node} open feed connections`);
}
telemetryFeed.on('connection', (socket: WebSocket) => { telemetryFeed.on('connection', (socket: WebSocket) => {
aggregator.addFeed(new Feed(socket)); aggregator.addFeed(new Feed(socket));
}); });
+2
View File
@@ -33,3 +33,5 @@ export function sleep(time: Milliseconds): Promise<void> {
} }
export const timestamp = Date.now as () => Timestamp; export const timestamp = Date.now as () => Timestamp;
export function noop() {}