Skip to content

Commit

Permalink
[Release] @subql/[email protected], @subql/[email protected], @s…
Browse files Browse the repository at this point in the history
…ubql/[email protected] (#241)

* set unique id to identify used indexers per request

* return calcualted score

* return calcualted score

* rename indexer to runner

* select runner by probability

* rename

* update score when success

* get indexer from context

* add debug logs

* penalize network error with more scores

* update unique id generator

* update debug logger

* refactor to reuse unique id generator

* remove commented codes

* update requestId cache expire timer

* bump

---------

Co-authored-by: cyrbuzz <[email protected]>
  • Loading branch information
icezohu and HuberTRoy authored Jan 22, 2024
1 parent a756992 commit b86b94a
Show file tree
Hide file tree
Showing 16 changed files with 234 additions and 65 deletions.
5 changes: 4 additions & 1 deletion packages/apollo-links/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.3.0] - 2024-01-22

## [1.2.6] - 2024-01-11

## [1.2.5] - 2023-12-21
Expand Down Expand Up @@ -92,7 +94,8 @@ Breaking change for `dictHttpLink` and `deploymentHttpLink`, use `const { link }

- Add Authlink for Apollo client

[unreleased]: https://github.com/subquery/network-clients/compare/v1.2.6...HEAD
[unreleased]: https://github.com/subquery/network-clients/compare/v1.3.0...HEAD
[1.3.0]: https://github.com/subquery/network-clients/compare/v1.2.6...v1.3.0
[1.2.6]: https://github.com/subquery/network-clients/compare/v1.2.4...v1.2.6
[1.2.5]: https://github.com/subquery/network-clients/compare/v1.2.4...v1.2.5
[1.2.4]: https://github.com/subquery/network-clients/compare/v1.2.3...v1.2.4
Expand Down
3 changes: 1 addition & 2 deletions packages/apollo-links/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@subql/apollo-links",
"version": "1.2.6",
"version": "1.3.0",
"description": "SubQuery Network - graphql links",
"main": "dist/index.js",
"author": "SubQuery Pte Limited",
Expand All @@ -15,7 +15,6 @@
"apollo-link-error": "^1.1.13",
"buffer": "^6.0.3",
"cross-fetch": "^4.0.0",
"ethers": "^5.6.8",
"js-base64": "^3.7.5",
"jwt-decode": "^3.1.2",
"lru-cache": "^10.0.1"
Expand Down
3 changes: 2 additions & 1 deletion packages/apollo-links/src/authHttpLink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ function authHttpLink(options: AuthOptions): AuthHttpLink {
const logger = _logger ?? silentLogger();
const orderManager = new OrderManager({
authUrl,
fallbackServiceUrl,
projectId: deploymentId,
projectType,
logger,
Expand All @@ -91,7 +92,7 @@ function authHttpLink(options: AuthOptions): AuthHttpLink {
const retryLink = createRetryLink({ orderManager, logger, maxRetries });
const fallbackLink = new FallbackLink(fallbackServiceUrl, logger);
const httpLink = new DynamicHttpLink({ httpOptions, logger });
const responseLink = new ResponseLink({ authUrl, logger });
const responseLink = new ResponseLink({ authUrl, orderManager, logger });
const errorLink = creatErrorLink({
orderManager,
fallbackLink,
Expand Down
12 changes: 10 additions & 2 deletions packages/apollo-links/src/core/clusterAuthLink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// SPDX-License-Identifier: Apache-2.0

import { ApolloLink, FetchResult, NextLink, Observable, Operation } from '@apollo/client/core';
import { OrderManager } from '@subql/network-support';
import { OrderManager, generateUniqueId } from '@subql/network-support';
import { Subscription } from 'zen-observable-ts';

import { Logger } from '../utils/logger';
Expand Down Expand Up @@ -33,7 +33,7 @@ export class ClusterAuthLink extends ApolloLink {
let sub: Subscription;

this.orderManager
.getRequestParams()
.getRequestParams(this.getRequestId(operation))
.then((params) => {
if (params) {
const { headers, url, type, runner } = params;
Expand Down Expand Up @@ -69,4 +69,12 @@ export class ClusterAuthLink extends ApolloLink {
private tokenToAuthHeader(token: string) {
return { authorization: `Bearer ${token}` };
}

private getRequestId(operation: Operation): string {
let { requestId } = operation.getContext();
if (requestId) return requestId;
requestId = generateUniqueId();
operation.setContext({ requestId });
return requestId;
}
}
7 changes: 5 additions & 2 deletions packages/apollo-links/src/core/responseLink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
// SPDX-License-Identifier: Apache-2.0

import { ApolloLink, FetchResult, NextLink, Observable, Operation } from '@apollo/client/core';
import { ChannelState, OrderType, POST } from '@subql/network-support';
import { ChannelState, OrderManager, OrderType, POST, ScoreType } from '@subql/network-support';
import { Base64 } from 'js-base64';
import { Logger } from '../utils/logger';

export type ResponseLinkOptions = {
authUrl: string;
orderManager: OrderManager;
logger?: Logger;
};

Expand Down Expand Up @@ -41,11 +42,13 @@ export class ResponseLink extends ApolloLink {
override request(operation: Operation, forward: NextLink): Observable<FetchResult> | null {
if (!forward) return null;

const { type } = operation.getContext();
const { type, indexer } = operation.getContext();

return new Observable<FetchResult>((observer) => {
const subscription = forward(operation).subscribe({
next: (response: FetchResult<Record<string, any>> & { state: ChannelState }) => {
this.options.orderManager.updateScore(indexer, ScoreType.SUCCESS);

if (type === OrderType.flexPlan) {
const responseHeaders = operation.getContext().response.headers;
if (responseHeaders) {
Expand Down
5 changes: 4 additions & 1 deletion packages/eth-provider/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [0.2.0] 2024-01-22

## [0.1.0] 2023-11-27

### Added

- Add Auth eth provider for rpc endpoint.

[unreleased]: https://github.com/subquery/eth-provider/compare/v0.1.0...HEAD
[unreleased]: https://github.com/subquery/eth-provider/compare/v0.2.0...HEAD
[0.2.0]: https://github.com/subquery/eth-provider/releases/tag/v0.2.0
[0.1.0]: https://github.com/subquery/eth-provider/releases/tag/v0.1.0
2 changes: 1 addition & 1 deletion packages/eth-provider/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@subql/eth-provider",
"version": "0.1.0",
"version": "0.2.0",
"main": "dist/index.js",
"author": "SubQuery Pte Limited",
"license": "Apache-2.0",
Expand Down
5 changes: 4 additions & 1 deletion packages/eth-provider/src/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
ProjectType,
ResponseFormat,
ScoreType,
generateUniqueId,
silentLogger,
} from '@subql/network-support';
import { Base64 } from 'js-base64';
Expand Down Expand Up @@ -87,8 +88,9 @@ export class SubqueryAuthedRpcProvider extends JsonRpcProvider {
return super._cache[method];
}
let retries = 0;
const requestId = generateUniqueId();
const requestResult: () => Promise<string> = async () => {
const requestParams = await this.orderManager.getRequestParams();
const requestParams = await this.orderManager.getRequestParams(requestId);
if (requestParams) {
// eslint-disable-next-line @typescript-eslint/unbound-method
const { url, headers, type, runner } = requestParams;
Expand All @@ -107,6 +109,7 @@ export class SubqueryAuthedRpcProvider extends JsonRpcProvider {
if (!result) {
throw new Error('Request RPC error');
}
this.orderManager.updateScore(runner, ScoreType.SUCCESS);
return result;
} catch (err) {
if (retries < this.maxRetries) {
Expand Down
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]

## [0.2.0] 2024-01-12

## [0.1.1] 2024-01-11

## [0.1.0] 2023-11-27
Expand All @@ -15,6 +17,7 @@ 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/v0.1.1...HEAD
[unreleased]: https://github.com/subquery/network-support/compare/v0.2.0...HEAD
[0.2.0]: https://github.com/subquery/network-support/releases/tag/v0.2.0
[0.1.1]: https://github.com/subquery/network-support/releases/tag/v0.1.1
[0.1.0]: https://github.com/subquery/network-support/releases/tag/v0.1.0
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": "0.1.1",
"version": "0.2.0",
"main": "dist/index.js",
"author": "SubQuery Pte Limited",
"license": "Apache-2.0",
Expand Down
1 change: 1 addition & 0 deletions packages/network-support/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@

export * from './types';
export * from './orderManager';
export * from './scoreManager';
export * from './utils';
Loading

0 comments on commit b86b94a

Please sign in to comment.