Skip to content

Commit

Permalink
feat: using keep alive connection
Browse files Browse the repository at this point in the history
  • Loading branch information
classicalliu committed Aug 29, 2019
1 parent 500b5c4 commit f3fbe15
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 12 deletions.
3 changes: 2 additions & 1 deletion packages/neuron-wallet/src/services/indexer/indexer-rpc.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import Core from '@nervosnetwork/ckb-sdk-core'
import { generateCore } from 'services/sdk-core'

export default class IndexerRPC {
private core: Core

constructor(url: string) {
this.core = new Core(url)
this.core = generateCore(url)
}

public deindexLockHash = async (lockHash: string) => {
Expand Down
10 changes: 9 additions & 1 deletion packages/neuron-wallet/src/services/node.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import Core from '@nervosnetwork/ckb-sdk-core'
import { interval, BehaviorSubject, merge } from 'rxjs'
import { distinctUntilChanged, sampleTime, flatMap, delay, retry, debounceTime } from 'rxjs/operators'
import https from 'https'
import http from 'http'
import { ShouldBeTypeOf } from 'exceptions'
import { ConnectionStatusSubject } from 'models/subjects/node'
import { CurrentNetworkIDSubject } from 'models/subjects/networks'
Expand Down Expand Up @@ -51,7 +53,13 @@ class NodeService {
if (!url.startsWith('http')) {
throw new Error('Protocol of url should be specified')
}
this.core.setNode({ url })
if (url.startsWith('https')) {
const httpsAgent = new https.Agent({ keepAlive: true })
this.core.setNode({ url, httpsAgent })
} else {
const httpAgent = new http.Agent({ keepAlive: true })
this.core.setNode({ url, httpAgent })
}
this.tipNumberSubject.next('0')
this.connectionStatusSubject.next(false)
return this.core
Expand Down
19 changes: 19 additions & 0 deletions packages/neuron-wallet/src/services/sdk-core.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import Core from '@nervosnetwork/ckb-sdk-core'
import https from 'https'
import http from 'http'

export const generateCore = (url: string): Core => {
const core = new Core(url)
if (url.startsWith('https')) {
const httpsAgent = new https.Agent({ keepAlive: true })
core.rpc.setNode({ url, httpsAgent })
} else {
const httpAgent = new http.Agent({ keepAlive: true })
core.rpc.setNode({ url, httpAgent })
}
return core
}

export default {
generateCore,
}
3 changes: 2 additions & 1 deletion packages/neuron-wallet/src/services/sync/get-blocks.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Core from '@nervosnetwork/ckb-sdk-core'
import { generateCore } from 'services/sdk-core'

import { Block, BlockHeader } from 'types/cell-types'
import TypeConvert from 'types/type-convert'
Expand All @@ -11,7 +12,7 @@ import { networkSwitchSubject } from './renderer-params'
let core: Core
networkSwitchSubject.subscribe((network: NetworkWithID | undefined) => {
if (network) {
core = new Core(network.remote)
core = generateCore(network.remote)
}
})

Expand Down
15 changes: 6 additions & 9 deletions packages/neuron-wallet/src/utils/blake2b.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
import Core from '@nervosnetwork/ckb-sdk-core'
import NodeService from 'services/node'
import { blake2b, PERSONAL, hexToBytes } from '@nervosnetwork/ckb-sdk-utils'

export default class Blake2b {
private blake2b: any
private core: Core

constructor() {
this.core = NodeService.getInstance().core
this.blake2b = this.core.utils.blake2b(32, null, null, this.core.utils.PERSONAL)
this.blake2b = blake2b(32, null, null, PERSONAL)
}

public update = (message: string): void => {
const msg = this.core.utils.hexToBytes(message.replace(/0x/, ''))
const msg = hexToBytes(message.replace(/0x/, ''))
this.blake2b.update(msg)
}

Expand All @@ -20,8 +17,8 @@ export default class Blake2b {
}

public static digest = (message: string): string => {
const blake2b = new Blake2b()
blake2b.update(message)
return blake2b.digest()
const blake2bHash = new Blake2b()
blake2bHash.update(message)
return blake2bHash.digest()
}
}

0 comments on commit f3fbe15

Please sign in to comment.