-
Notifications
You must be signed in to change notification settings - Fork 202
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement retries on grpc and rest query calls
- Loading branch information
1 parent
ef8518c
commit d8c4486
Showing
37 changed files
with
946 additions
and
467 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
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 |
---|---|---|
@@ -1,51 +1,45 @@ | ||
import { grpc } from '@injectivelabs/grpc-web' | ||
import { GrpcUnaryRequestException } from '@injectivelabs/exceptions' | ||
import { isBrowser } from '../utils/helpers' | ||
import { getGrpcTransport } from '../utils/grpc' | ||
import { InjectiveAccountRpc } from '@injectivelabs/indexer-proto-ts' | ||
|
||
if (!isBrowser()) { | ||
grpc.setDefaultTransport(getGrpcTransport() as grpc.TransportFactory) | ||
} | ||
/** | ||
* @hidden | ||
*/ | ||
export default class BaseGrpcConsumer { | ||
protected module: string = '' | ||
|
||
protected endpoint: string | ||
export default class BaseGrpcConsumer extends InjectiveAccountRpc.GrpcWebImpl { | ||
protected module: string = '' | ||
|
||
constructor(endpoint: string) { | ||
this.endpoint = endpoint | ||
super(endpoint, { transport: getGrpcTransport() }) | ||
} | ||
|
||
public getGrpcWebImpl(endpoint: string) { | ||
return new BaseGrpcConsumer(endpoint) | ||
} | ||
|
||
protected request< | ||
TRequest extends grpc.ProtobufMessage, | ||
TResponse extends grpc.ProtobufMessage, | ||
S extends grpc.UnaryMethodDefinition<TRequest, TResponse>, | ||
>(request: TRequest, service: S): Promise<TResponse> { | ||
return new Promise((resolve, reject) => { | ||
grpc.unary(service, { | ||
request, | ||
host: this.endpoint, | ||
onEnd: (res) => { | ||
const { statusMessage, status, message } = res | ||
protected retry<TResponse>( | ||
grpcCall: Function, | ||
retries: number = 3, | ||
delay: number = 1000, | ||
): Promise<TResponse> { | ||
const retryGrpcCall = async (attempt = 1): Promise<any> => { | ||
try { | ||
return await grpcCall() | ||
} catch (e: any) { | ||
if (attempt >= retries) { | ||
throw e | ||
} | ||
|
||
if (status === grpc.Code.OK && message) { | ||
return resolve(message as TResponse) | ||
} | ||
return new Promise((resolve) => | ||
setTimeout( | ||
() => resolve(retryGrpcCall(attempt + 1)), | ||
delay * attempt, | ||
), | ||
) | ||
} | ||
} | ||
|
||
return reject( | ||
new GrpcUnaryRequestException( | ||
new Error(statusMessage || 'The request failed.'), | ||
{ | ||
code: status, | ||
context: `${this.endpoint}?service=${service.methodName}`, | ||
contextModule: this.module, | ||
}, | ||
), | ||
) | ||
}, | ||
}) | ||
}) | ||
return retryGrpcCall() | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.