Skip to content

Commit

Permalink
[release] @subql/[email protected]
Browse files Browse the repository at this point in the history
* feat: simplely add http version score

* try update http version logic

* style: remove debug

* [release] @subql/[email protected]

---------

Co-authored-by: Jacob <[email protected]>
  • Loading branch information
cool-firer and icezohu authored May 16, 2024
1 parent d52a18f commit 6a6817b
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 36 deletions.
5 changes: 4 additions & 1 deletion packages/network-support/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [1.2.5] 2024-05-16

## [1.2.4] 2024-05-13

## [1.2.3] 2024-05-12
Expand Down Expand Up @@ -35,7 +37,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- It's a internal library.

[unreleased]: https://github.com/subquery/network-support/compare/v1.2.4...HEAD
[unreleased]: https://github.com/subquery/network-support/compare/v1.2.5...HEAD
[1.2.5]: https://github.com/subquery/network-support/releases/tag/v1.2.5
[1.2.4]: https://github.com/subquery/network-support/releases/tag/v1.2.4
[1.2.3]: https://github.com/subquery/network-support/releases/tag/v1.2.3
[1.2.2]: https://github.com/subquery/network-support/releases/tag/v1.2.2
Expand Down
2 changes: 1 addition & 1 deletion packages/network-support/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@subql/network-support",
"version": "1.2.4",
"version": "1.2.5",
"main": "dist/index.js",
"author": "SubQuery Pte Limited",
"license": "Apache-2.0",
Expand Down
7 changes: 6 additions & 1 deletion packages/network-support/src/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ export function createFetch(
}
}
const { url, headers, type, runner, channelId } = requestParams;
let httpVersion = 1;

try {
const _res = await customFetch(
url,
Expand All @@ -77,6 +79,9 @@ export function createFetch(
},
overrideFetch
);

httpVersion = Number(_res.headers.get('httpVersion')) || 1;

let res: object;
if (type === OrderType.flexPlan) {
[res] = orderManager.extractChannelState(
Expand All @@ -97,7 +102,7 @@ export function createFetch(
res = await _res.json();
}

orderManager.updateScore(runner, ScoreType.SUCCESS);
orderManager.updateScore(runner, ScoreType.SUCCESS, httpVersion);

return {
status: _res.status,
Expand Down
6 changes: 3 additions & 3 deletions packages/network-support/src/orderManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ export class OrderManager {

private async selectRunner(orders: Order[]): Promise<Order | undefined> {
if (!orders.length) return;
const scores = await Promise.all(orders.map((o) => this.scoreManager.getScore(o.indexer)));
const scores = await Promise.all(orders.map((o) => this.scoreManager.getBonusScore(o.indexer)));
const random = Math.random() * scores.reduce((a, b) => a + b, 0);
this.logger?.debug(`selectRunner: indexers: ${orders.map((o) => o.indexer)}`);
this.logger?.debug(`selectRunner: scores: ${scores}`);
Expand Down Expand Up @@ -405,8 +405,8 @@ export class OrderManager {
return this.scoreManager.getScore(runner);
}

async updateScore(runner: string, errorType: ScoreType) {
await this.scoreManager.updateScore(runner, errorType);
async updateScore(runner: string, errorType: ScoreType, httpVersion?: number) {
await this.scoreManager.updateScore(runner, errorType, httpVersion);
}

cleanup() {
Expand Down
57 changes: 27 additions & 30 deletions packages/network-support/src/scoreManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const scoresDelta = {

type ScoreStoreType = {
score: number;
httpVersion?: number;
lastUpdate: number;
lastFailed: number;
};
Expand All @@ -46,44 +47,36 @@ export class ScoreManager {

async getScore(runner: string) {
const key = this.getCacheKey(runner);
let score = await this.scoreStore.get<number | ScoreStoreType>(key);

if (score === undefined) {
score = {
score: 100,
lastUpdate: 0,
lastFailed: 0,
};
this.scoreStore.set(key, score);
}

if (typeof score === 'number') {
return Math.max(score, this.minScore);
}

const score = (await this.scoreStore.get<ScoreStoreType>(key)) || {
score: 100,
httpVersion: 1,
lastUpdate: 0,
lastFailed: 0,
};
return this.calculatedScore(score);
}

private calculatedScore(score: ScoreStoreType) {
return Math.min(score.score + Math.floor((Date.now() - score.lastUpdate) / 600_000), 100);
}

async updateScore(runner: string, errorType: ScoreType) {
async getBonusScore(runner: string) {
return (await this.getScore(runner)) * ((await this.getHttpVersion(runner)) == 2 ? 10 : 1);
}

async updateScore(runner: string, errorType: ScoreType, httpVersion?: number) {
if (!runner) {
this.logger?.debug('updateScore: runner is empty');
return;
}

const key = this.getCacheKey(runner);
let score = (await this.scoreStore.get<number | ScoreStoreType>(key)) ?? 100;

if (typeof score === 'number') {
score = {
score: score,
lastUpdate: 0,
lastFailed: 0,
};
}
const score = (await this.scoreStore.get<ScoreStoreType>(key)) || {
score: 100,
httpVersion,
lastUpdate: 0,
lastFailed: 0,
};

if (errorType !== ScoreType.SUCCESS) {
this.logger?.debug(`updateScore type: ${runner} ${errorType}`);
Expand All @@ -92,17 +85,21 @@ export class ScoreManager {

const delta = scoresDelta[errorType];

score = {
score: Math.min(Math.max(score.score + delta, this.minScore), 100),
lastUpdate: Date.now(),
lastFailed: errorType === ScoreType.SUCCESS ? 0 : Date.now(),
};
score.score = Math.min(Math.max(score.score + delta, this.minScore), 100);
score.httpVersion = httpVersion || score.httpVersion;
score.lastUpdate = Date.now();
score.lastFailed = errorType === ScoreType.SUCCESS ? 0 : Date.now();

this.logger?.debug(`updateScore after: ${runner} ${JSON.stringify(score)}`);

this.scoreStore.set(key, score);
}

async getHttpVersion(runner: string) {
const key = this.getCacheKey(runner);
return (await this.scoreStore.get<ScoreStoreType>(key))?.httpVersion || 1;
}

private getCacheKey(runner: string): string {
return `$query-score-${runner}-${this.projectId}`;
}
Expand Down

0 comments on commit 6a6817b

Please sign in to comment.