-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
22 changed files
with
391 additions
and
189 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'backend': minor | ||
--- | ||
|
||
Subgraph client will fallback to another URL on failure |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
export async function retryOnFailureWithRotation<T>( | ||
sdkClients: any[], | ||
fn: (sdk: any) => Promise<T>, | ||
retries: number = 3, | ||
): Promise<T> { | ||
let attempts = 0; | ||
let currentSdkIndex = 0; | ||
|
||
while (attempts < retries) { | ||
try { | ||
const sdk = sdkClients[currentSdkIndex]; // Get the current SDK client | ||
return await fn(sdk); // Try the operation using the current SDK | ||
} catch (error) { | ||
attempts += 1; | ||
console.error(`Subgraph failed ${new URL(sdkClients[currentSdkIndex].url).host}:`, error); | ||
|
||
if (attempts < retries) { | ||
// Rotate to the next SDK client | ||
currentSdkIndex = (currentSdkIndex + 1) % sdkClients.length; | ||
console.log(`Retrying with ${new URL(sdkClients[currentSdkIndex].url).host}...`); | ||
} else { | ||
throw new Error('All SDK clients failed after retries.'); | ||
} | ||
} | ||
} | ||
|
||
throw new Error('Unexpected failure, retries exhausted.'); | ||
} | ||
|
||
export function wrapSdkWithRetryAndRotation<T extends object>(sdkClients: T[], retries: number = 3): T { | ||
const wrappedSdk: Partial<T> = {}; | ||
|
||
// Use one SDK as a template for wrapping all functions | ||
const sdk = sdkClients[0]; | ||
|
||
for (const key of Object.keys(sdk)) { | ||
const value = (sdk as any)[key]; | ||
|
||
if (typeof value === 'function') { | ||
// Wrap each SDK method to use retry with rotation | ||
wrappedSdk[key as keyof T] = ((...args: any[]) => | ||
retryOnFailureWithRotation(sdkClients, (sdk) => value.apply(sdk, args), retries)) as T[keyof T]; | ||
} else { | ||
wrappedSdk[key as keyof T] = value; | ||
} | ||
} | ||
|
||
return wrappedSdk as T; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { GraphQLClient } from 'graphql-request'; | ||
import { Cache, CacheClass } from 'memory-cache'; | ||
import { retryOnFailureWithRotation } from './retry-on-failure'; | ||
|
||
export class SubgraphServiceBase<TSdk> { | ||
protected cache: CacheClass<string, any>; | ||
private sdks: TSdk[]; | ||
|
||
constructor( | ||
subgraphUrl: string | string[], | ||
protected chainId: number, | ||
private getSdk: (client: GraphQLClient) => TSdk, | ||
) { | ||
this.cache = new Cache<string, any>(); | ||
|
||
if (Array.isArray(subgraphUrl)) { | ||
this.sdks = subgraphUrl.map((url) => this.getSdk(new GraphQLClient(url))); | ||
} else { | ||
this.sdks = [this.getSdk(new GraphQLClient(subgraphUrl))]; | ||
} | ||
} | ||
|
||
protected async retryOnFailure<T>(fn: (sdk: TSdk) => Promise<T>, retries: number = 3): Promise<T> { | ||
return retryOnFailureWithRotation(this.sdks, fn, retries); | ||
} | ||
} |
Oops, something went wrong.