mirror of
https://github.com/pezkuwichain/pezkuwi-telemetry.git
synced 2026-06-12 16:51:02 +00:00
@@ -1,10 +1,9 @@
|
||||
import * as WebSocket from 'ws';
|
||||
import * as EventEmitter from 'events';
|
||||
|
||||
import { noop, timestamp, Maybe, Types, NumStats } from '@dotstats/common';
|
||||
import { noop, timestamp, idGenerator, Maybe, Types, NumStats } from '@dotstats/common';
|
||||
import { parseMessage, getBestBlock, Message, BestBlock, SystemInterval } from './message';
|
||||
import { locate, Location } from './location';
|
||||
import { getId, refreshId } from './nodeId';
|
||||
import { MeanList } from './MeanList';
|
||||
|
||||
const BLOCK_TIME_HISTORY = 10;
|
||||
@@ -12,6 +11,8 @@ const MEMORY_RECORDS = 20;
|
||||
const CPU_RECORDS = 20;
|
||||
const TIMEOUT = (1000 * 60 * 1) as Types.Milliseconds; // 1 minute
|
||||
|
||||
const nextId = idGenerator<Types.NodeId>();
|
||||
|
||||
export interface NodeEvents {
|
||||
on(event: 'location', fn: (location: Location) => void): void;
|
||||
emit(event: 'location', location: Location): void;
|
||||
@@ -64,7 +65,7 @@ export default class Node {
|
||||
messages: Array<Message>,
|
||||
) {
|
||||
this.ip = ip;
|
||||
this.id = getId(address, name);
|
||||
this.id = nextId();
|
||||
this.name = name;
|
||||
this.chain = chain;
|
||||
this.config = config;
|
||||
@@ -213,8 +214,6 @@ export default class Node {
|
||||
this.socket.close();
|
||||
this.socket.terminate();
|
||||
|
||||
refreshId(this.address, this.name, this.id);
|
||||
|
||||
this.events.emit('disconnect');
|
||||
}
|
||||
|
||||
|
||||
@@ -1,59 +0,0 @@
|
||||
import { timestamp, Maybe, Types, idGenerator, Opaque } from '@dotstats/common';
|
||||
|
||||
const CACHE_LIFETIME = (24 * 3600 * 1000) as Types.Milliseconds; // 24h
|
||||
const CACHE_INTERVAL = (3600 * 1000) as Types.Milliseconds; // 1h
|
||||
|
||||
interface NodeIdCache {
|
||||
id: Types.NodeId;
|
||||
ts: Types.Timestamp;
|
||||
}
|
||||
|
||||
type SaltedName = Opaque<string, 'SaltedName'>;
|
||||
|
||||
const nextId = idGenerator<Types.NodeId>();
|
||||
const idCache = new Map<Types.Address | SaltedName, NodeIdCache>();
|
||||
|
||||
function clearCache() {
|
||||
const now = timestamp();
|
||||
|
||||
for (const [pubkey, { ts }] of idCache.entries()) {
|
||||
if ((now - ts) > CACHE_LIFETIME) {
|
||||
idCache.delete(pubkey);
|
||||
}
|
||||
}
|
||||
|
||||
setTimeout(clearCache, CACHE_INTERVAL);
|
||||
}
|
||||
|
||||
clearCache();
|
||||
|
||||
export function getId(_pubkey: Maybe<Types.Address>, _name: Types.NodeName): Types.NodeId {
|
||||
// let cachekey: Types.Address | SaltedName;
|
||||
|
||||
// if (pubkey) {
|
||||
// const cached = idCache.get(pubkey);
|
||||
|
||||
// if (cached) {
|
||||
// return cached.id;
|
||||
// }
|
||||
|
||||
// cachekey = pubkey;
|
||||
// } else {
|
||||
// cachekey = `name:${name}` as SaltedName;
|
||||
// }
|
||||
|
||||
const id = nextId();
|
||||
// const ts = timestamp();
|
||||
|
||||
// idCache.set(cachekey, { id, ts });
|
||||
|
||||
return id;
|
||||
}
|
||||
|
||||
export function refreshId(_pubkey: Maybe<Types.Address>, _name: Types.NodeName, _id: Types.NodeId) {
|
||||
// const cachekey = pubkey ? pubkey : `name:${name}` as SaltedName;
|
||||
// const cachekey = `name:${name}` as SaltedName;
|
||||
// const ts = timestamp();
|
||||
|
||||
// idCache.set(cachekey, { id, ts });
|
||||
}
|
||||
Reference in New Issue
Block a user