mirror of
https://github.com/roleypoly/roleypoly-v1.git
synced 2025-04-26 04:29:11 +00:00
[rpc-client] add retrying
This commit is contained in:
parent
c3b510f0bb
commit
10d5979ce1
1 changed files with 34 additions and 3 deletions
|
@ -1,6 +1,7 @@
|
||||||
// @flow
|
// @flow
|
||||||
import superagent from 'superagent'
|
import superagent from 'superagent'
|
||||||
import RPCError from './error'
|
import RPCError from './error'
|
||||||
|
import retry from 'async-retry'
|
||||||
|
|
||||||
export type RPCResponse = {
|
export type RPCResponse = {
|
||||||
response?: mixed,
|
response?: mixed,
|
||||||
|
@ -37,7 +38,9 @@ export default class RPCClient {
|
||||||
args: number
|
args: number
|
||||||
}> = []
|
}> = []
|
||||||
|
|
||||||
constructor ({ forceDev, baseUrl = '/api/_rpc' }: { forceDev?: boolean, baseUrl?: string } = {}) {
|
retry: boolean
|
||||||
|
|
||||||
|
constructor ({ forceDev, baseUrl = '/api/_rpc', retry = false }: { forceDev?: boolean, baseUrl?: string, retry?: boolean } = {}) {
|
||||||
this.baseUrl = (process.env.APP_URL || '') + baseUrl
|
this.baseUrl = (process.env.APP_URL || '') + baseUrl
|
||||||
|
|
||||||
if (forceDev != null) {
|
if (forceDev != null) {
|
||||||
|
@ -46,6 +49,8 @@ export default class RPCClient {
|
||||||
this.dev = process.env.NODE_ENV === 'development'
|
this.dev = process.env.NODE_ENV === 'development'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this.retry = retry
|
||||||
|
|
||||||
this.rpc = new Proxy({}, {
|
this.rpc = new Proxy({}, {
|
||||||
get: this.__rpcCall,
|
get: this.__rpcCall,
|
||||||
has: this.__checkCall,
|
has: this.__checkCall,
|
||||||
|
@ -59,7 +64,7 @@ export default class RPCClient {
|
||||||
}
|
}
|
||||||
|
|
||||||
withCookies = (h: string) => {
|
withCookies = (h: string) => {
|
||||||
this.headerMixins['Set-Cookie'] = h
|
this.cookieHeader = h
|
||||||
return this.rpc
|
return this.rpc
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -92,10 +97,35 @@ export default class RPCClient {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async call (fn: string, ...args: any[]): mixed {
|
call (fn: string, ...args: any[]): mixed {
|
||||||
|
// console.debug('rpc call:', { fn, args })
|
||||||
|
if (this.retry) {
|
||||||
|
return this.callWithRetry(fn, ...args)
|
||||||
|
} else {
|
||||||
|
return this.callAsNormal(fn, ...args)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async callWithRetry (fn: string, ...args: any[]): mixed {
|
||||||
|
return retry<mixed>(async (bail) => {
|
||||||
|
try {
|
||||||
|
return await this.callAsNormal(fn, ...args)
|
||||||
|
} catch (e) {
|
||||||
|
if (e instanceof RPCError) {
|
||||||
|
bail(e)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
async callAsNormal (fn: string, ...args: any[]): mixed {
|
||||||
const req: RPCRequest = { fn, args }
|
const req: RPCRequest = { fn, args }
|
||||||
const rq = superagent.post(this.baseUrl).set({ ...this.headerMixins })
|
const rq = superagent.post(this.baseUrl).set({ ...this.headerMixins })
|
||||||
|
|
||||||
|
if (this.cookieHeader != null && this.cookieHeader !== '') {
|
||||||
|
rq.cookies = this.cookieHeader
|
||||||
|
}
|
||||||
|
|
||||||
const rsp = await rq.send(req).ok(() => true)
|
const rsp = await rq.send(req).ok(() => true)
|
||||||
const body: RPCResponse = rsp.body
|
const body: RPCResponse = rsp.body
|
||||||
|
|
||||||
|
@ -121,6 +151,7 @@ export default class RPCClient {
|
||||||
}
|
}
|
||||||
|
|
||||||
// PROXY HANDLERS
|
// PROXY HANDLERS
|
||||||
|
// __rpcCall = (_: {}, fn: string) => this.call.bind(this, fn)
|
||||||
__rpcCall = (_: {}, fn: string) => this.call.bind(this, fn)
|
__rpcCall = (_: {}, fn: string) => this.call.bind(this, fn)
|
||||||
__checkCall = (_: {}, fn: string) => this.dev ? this.__listCalls(_).includes(fn) : true
|
__checkCall = (_: {}, fn: string) => this.dev ? this.__listCalls(_).includes(fn) : true
|
||||||
__listCalls = (_: {}): string[] => this.__rpcAvailable.map(x => x.name)
|
__listCalls = (_: {}): string[] => this.__rpcAvailable.map(x => x.name)
|
||||||
|
|
Loading…
Add table
Reference in a new issue