Skip to content

Commit

Permalink
add select suber retry
Browse files Browse the repository at this point in the history
  • Loading branch information
BruceChar committed Nov 17, 2021
1 parent 1f24c41 commit 558b314
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
23 changes: 16 additions & 7 deletions packages/wsrpc/src/suber/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ async function dataParse(data: WebSocket.Data, chain: string, subType: NodeType,
dat = JSON.parse(dat.result)
}
// dat.error: no need handle
}
}
// else if (subType === NodeType.Mem) {
// // log.debug(`${chain} new memory node ws response method[${dat.method}] ID[${dat.id || dat.subscription}]: ${dat.error}`)
// } else {
Expand Down Expand Up @@ -694,13 +694,22 @@ class Suber {
log.error(`Select suber error: no valid ${type} subers of chain ${chain} `)
return Err(`No valid ${type} suber of chain[${chain}]`)
}
const ind = GG.getID() % keys.length
const suber = subers[keys[ind]]
if (suber.stat !== SuberStat.Active) {
log.error(`${chain} ${type} suber is inactive`)
return Err(`${chain} ${type} suber inactive`)
// add retry
const TRY_CNT = 3
let suber: Suber
for (let i = 0; i < TRY_CNT; i++) {
const ind = GG.getID() % keys.length
suber = subers[keys[ind]] as Suber
if (suber.stat !== SuberStat.Active) {
if (i === 2) {
log.error(`${chain} ${type} suber is inactive`)
return Err(`${chain} ${type} suber inactive`)
}
} else {
break
}
}
return Ok(suber)
return Ok(suber!)
}

static async initChainSuber(chain: string, suberEmiter: Emiter) {
Expand Down
5 changes: 5 additions & 0 deletions packages/wsrpc/src/suber/noder/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ export interface Noder {

export class Noder {

// method: parameter length
static memRpcs: Record<string, number> = {
"state_getKeysPaged": 4,
"state_getStorage": 2,
Expand All @@ -87,6 +88,10 @@ export class Noder {
static async sendRpc(chain: string, data: ReqDataT, resp: Http.ServerResponse, stat: Statistics): PVoidT {

const re = await Suber.selectSuber(chain, NodeType.Node)
if (isErr(re)) {
log.error(`send rpc request error: ${re.value}`)
return
}
const noder = await getNoder(chain, (re.value as Suber).nodeId)

log.info(`new node rpc requst, chain ${chain} method ${data.method} params ${data.params}, select noder: ${noder.nodeId}-${noder.host}-${noder.port}`)
Expand Down

0 comments on commit 558b314

Please sign in to comment.