Skip to content

Commit

Permalink
fix kv open check logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Bruce authored and Bruce committed Sep 3, 2021
1 parent 9bbcb61 commit 0b5e054
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 14 deletions.
11 changes: 11 additions & 0 deletions packages/wsrpc/src/global.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,20 @@ const TryCntMap: { [key in string]: number } = {}
const ConnCntMap: { [key in string]: { [key in string]: number } } = {}

const PuberEvt = new EventEmitter()
const KVStatus: Record<string, boolean> = {}

namespace G {

// kv status
export const setKvStatus = (chain: string, status: boolean) => {
KVStatus[`${chain.toLowerCase()}`] = status
}

export const getKvStatus = (chain: string): boolean => {
return KVStatus[`${chain.toLowerCase()}`]
}

// puber event
export const getPuberEvent = (): EventEmitter => {
return PuberEvt
}
Expand Down
13 changes: 3 additions & 10 deletions packages/wsrpc/src/matcher/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,9 @@ namespace Matcher {
const puber = Puber.create(ws, chain, pid)
let kvOk = false
let kvSuber: Suber
const kvre = await Dao.getChainConfig(chain)
if (isErr(kvre)) {
log.error(`get chain ${chain} config error: ${kvre.value}`)
} else {
const conf = kvre.value
if (conf.kvEnable.toString() === 'true') {
kvOk = true
}
}
if (kvOk) {
const kvOpen = GG.getKvStatus(chain)

if (kvOpen) {
let rek = await Suber.selectSuber(chain, SuberTyp.Kv)
if (isErr(rek)) { return rek }
kvSuber = rek.value as Suber
Expand Down
10 changes: 6 additions & 4 deletions packages/wsrpc/src/matcher/suber.ts
Original file line number Diff line number Diff line change
Expand Up @@ -643,14 +643,16 @@ class Suber {
}
const conf = re.value as ChainConfig
const urls = geneUrl(conf)
log.info(`Url of chain [${chain}] is: ${urls} kv enable [${conf.kvEnable}]`)
const kvOpen = conf.kvEnable.toString().toLowerCase() === 'true'
// set kv status
// NOTE: all chain instances must support kv or not
GG.setKvStatus(chain, kvOpen)
log.info(`Url of chain [${chain}] is: ${urls} kv enable [${kvOpen}]`)
for (let i = 0; i < poolSize; i++) {

newSuber(chain, urls[0], SuberTyp.Node, new Set())

// kv Suber
if ((conf.kvEnable.toString()) === 'true') {
log.debug(`chain ${chain} kv subscribe enable: %o`, conf.kvEnable)
if (kvOpen) {
newSuber(chain, urls[1], SuberTyp.Kv, new Set())
}
}
Expand Down

0 comments on commit 0b5e054

Please sign in to comment.