From b470f47f5f0c16f6d3ce19cab1dba99a32ecc574 Mon Sep 17 00:00:00 2001 From: luke <123917244@qq.com> Date: Fri, 6 Sep 2024 14:47:36 +0800 Subject: [PATCH] feat: refactor --- packages/network-support/src/scoreManager.ts | 20 +++++------ packages/network-support/src/types.ts | 2 +- packages/network-support/src/utils/index.ts | 1 - .../network-support/src/utils/timeBarer.ts | 35 ------------------- 4 files changed, 9 insertions(+), 49 deletions(-) delete mode 100644 packages/network-support/src/utils/timeBarer.ts diff --git a/packages/network-support/src/scoreManager.ts b/packages/network-support/src/scoreManager.ts index cb552458..162b32f6 100644 --- a/packages/network-support/src/scoreManager.ts +++ b/packages/network-support/src/scoreManager.ts @@ -10,7 +10,6 @@ import { getLatencyScoreWeight, } from './utils/score'; import { Version } from './utils/version'; -import timeBarrier from './utils/timeBarer'; type Options = { logger: Logger; @@ -164,17 +163,14 @@ export class ScoreManager { this.scoreStore.set(key, score); extraLog = extraLog || {}; - if (score.score <= 1) { - const inserted = timeBarrier.set(`${this.projectId}_${runner}`); - if (inserted && this.notifyFunc) { - this.notifyFunc({ - text: `*Title*: score down from ${before} to ${score.score}\n*DeploymentId*: ${ - this.projectId - }\n*Indexer*: ${runner}\n*Info*: ${errorType} - ${JSON.stringify( - extraLog - )}\n*TimeBarrier*: ${timeBarrier.inspect()}\n*Time*: ${new Date().toISOString()}`, - }); - } + if (score.score <= 1 && this.notifyFunc) { + this.notifyFunc(`${this.projectId}_${runner}`, { + text: `*Title*: score down from ${before} to ${score.score}\n*DeploymentId*: ${ + this.projectId + }\n*Indexer*: ${runner}\n*Info*: ${errorType} - ${JSON.stringify( + extraLog + )}\n*Time*: ${new Date().toISOString()}`, + }); } this.logger?.info({ diff --git a/packages/network-support/src/types.ts b/packages/network-support/src/types.ts index ad677052..422ff1e3 100644 --- a/packages/network-support/src/types.ts +++ b/packages/network-support/src/types.ts @@ -84,4 +84,4 @@ export type ScoreWithDetail = { scoreDetail: Record; }; -export type NotifyFunc = (data: any, headers?: Record) => Promise; +export type NotifyFunc = (id: string, data: any, headers?: Record) => Promise; diff --git a/packages/network-support/src/utils/index.ts b/packages/network-support/src/utils/index.ts index df8845e3..e3a47a67 100644 --- a/packages/network-support/src/utils/index.ts +++ b/packages/network-support/src/utils/index.ts @@ -8,4 +8,3 @@ export * from './auth'; export * from './uniqueId'; export * from './common'; export * from './score'; -export * from './timeBarer'; diff --git a/packages/network-support/src/utils/timeBarer.ts b/packages/network-support/src/utils/timeBarer.ts deleted file mode 100644 index b7f72e68..00000000 --- a/packages/network-support/src/utils/timeBarer.ts +++ /dev/null @@ -1,35 +0,0 @@ -// Copyright 2020-2024 SubQuery Pte Ltd authors & contributors -// SPDX-License-Identifier: Apache-2.0 - -class TimeBarrier { - private ttl: number; - private timers: Map; - - constructor(ttl: number) { - this.ttl = ttl; - this.timers = new Map(); - } - - set(key: string): boolean { - if (this.timers.has(key)) { - return false; - } - this.timers.set( - key, - setTimeout(() => this.timers.delete(key), this.ttl) - ); - return true; - } - - inspect() { - return `TimeBarrier size:${this.timers.size}`; - } -} - -const defaultTTL = 1000 * 60 * 10; - -export default new TimeBarrier(defaultTTL); - -export function createTimeBarrier(ttl: number) { - return new TimeBarrier(ttl); -}