Skip to content

Commit f3fbe15

Browse files
committed
feat: using keep alive connection
1 parent 500b5c4 commit f3fbe15

5 files changed

Lines changed: 38 additions & 12 deletions

File tree

packages/neuron-wallet/src/services/indexer/indexer-rpc.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import Core from '@nervosnetwork/ckb-sdk-core'
2+
import { generateCore } from 'services/sdk-core'
23

34
export default class IndexerRPC {
45
private core: Core
56

67
constructor(url: string) {
7-
this.core = new Core(url)
8+
this.core = generateCore(url)
89
}
910

1011
public deindexLockHash = async (lockHash: string) => {

packages/neuron-wallet/src/services/node.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import Core from '@nervosnetwork/ckb-sdk-core'
22
import { interval, BehaviorSubject, merge } from 'rxjs'
33
import { distinctUntilChanged, sampleTime, flatMap, delay, retry, debounceTime } from 'rxjs/operators'
4+
import https from 'https'
5+
import http from 'http'
46
import { ShouldBeTypeOf } from 'exceptions'
57
import { ConnectionStatusSubject } from 'models/subjects/node'
68
import { CurrentNetworkIDSubject } from 'models/subjects/networks'
@@ -51,7 +53,13 @@ class NodeService {
5153
if (!url.startsWith('http')) {
5254
throw new Error('Protocol of url should be specified')
5355
}
54-
this.core.setNode({ url })
56+
if (url.startsWith('https')) {
57+
const httpsAgent = new https.Agent({ keepAlive: true })
58+
this.core.setNode({ url, httpsAgent })
59+
} else {
60+
const httpAgent = new http.Agent({ keepAlive: true })
61+
this.core.setNode({ url, httpAgent })
62+
}
5563
this.tipNumberSubject.next('0')
5664
this.connectionStatusSubject.next(false)
5765
return this.core
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import Core from '@nervosnetwork/ckb-sdk-core'
2+
import https from 'https'
3+
import http from 'http'
4+
5+
export const generateCore = (url: string): Core => {
6+
const core = new Core(url)
7+
if (url.startsWith('https')) {
8+
const httpsAgent = new https.Agent({ keepAlive: true })
9+
core.rpc.setNode({ url, httpsAgent })
10+
} else {
11+
const httpAgent = new http.Agent({ keepAlive: true })
12+
core.rpc.setNode({ url, httpAgent })
13+
}
14+
return core
15+
}
16+
17+
export default {
18+
generateCore,
19+
}

packages/neuron-wallet/src/services/sync/get-blocks.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import Core from '@nervosnetwork/ckb-sdk-core'
2+
import { generateCore } from 'services/sdk-core'
23

34
import { Block, BlockHeader } from 'types/cell-types'
45
import TypeConvert from 'types/type-convert'
@@ -11,7 +12,7 @@ import { networkSwitchSubject } from './renderer-params'
1112
let core: Core
1213
networkSwitchSubject.subscribe((network: NetworkWithID | undefined) => {
1314
if (network) {
14-
core = new Core(network.remote)
15+
core = generateCore(network.remote)
1516
}
1617
})
1718

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
1-
import Core from '@nervosnetwork/ckb-sdk-core'
2-
import NodeService from 'services/node'
1+
import { blake2b, PERSONAL, hexToBytes } from '@nervosnetwork/ckb-sdk-utils'
32

43
export default class Blake2b {
54
private blake2b: any
6-
private core: Core
75

86
constructor() {
9-
this.core = NodeService.getInstance().core
10-
this.blake2b = this.core.utils.blake2b(32, null, null, this.core.utils.PERSONAL)
7+
this.blake2b = blake2b(32, null, null, PERSONAL)
118
}
129

1310
public update = (message: string): void => {
14-
const msg = this.core.utils.hexToBytes(message.replace(/0x/, ''))
11+
const msg = hexToBytes(message.replace(/0x/, ''))
1512
this.blake2b.update(msg)
1613
}
1714

@@ -20,8 +17,8 @@ export default class Blake2b {
2017
}
2118

2219
public static digest = (message: string): string => {
23-
const blake2b = new Blake2b()
24-
blake2b.update(message)
25-
return blake2b.digest()
20+
const blake2bHash = new Blake2b()
21+
blake2bHash.update(message)
22+
return blake2bHash.digest()
2623
}
2724
}

0 commit comments

Comments
 (0)