Skip to content

Commit

Permalink
ALL-3800 - Fix potential memory issues in LoadBalancer (#1035)
Browse files Browse the repository at this point in the history
* ALL-3800 - Fix potential memory issues in `LoadBalancer`

* ALL-3800 - Fix potential memory issues in `LoadBalancer` - misc

* ALL-3800 - Fix potential memory issues in `LoadBalancer` - misc 2

* ALL-3800 - Fix potential memory issues in `LoadBalancer` - misc 2

* ALL-3800 - Fix potential memory issues in `LoadBalancer` - misc 3

* ALL-3800 - Fix potential memory issues in `LoadBalancer` - misc 4

* ALL-3800 - Fix potential memory issues in `LoadBalancer` - misc 5

* ALL-3800 - Fix potential memory issues in `LoadBalancer` - misc 6

* ALL-3800 - Fix potential memory issues in `LoadBalancer` - misc 7
  • Loading branch information
Smrecz authored Dec 14, 2023
1 parent f57a5ee commit 16e7595
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 34 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## [4.1.34] - 2023.12.14
### Fixed
- Fix potential memory issues with `LoadBalancer`.

## [4.1.33] - 2023.12.1
### Added
- Added RPC support for the XINFIN network. Users can now make RPC calls to these network using the `Network.XINFIN` network.
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@tatumio/tatum",
"version": "4.1.33",
"version": "4.1.34",
"description": "Tatum JS SDK",
"author": "Tatum",
"repository": "https://github.com/tatumio/tatum-js",
Expand Down
9 changes: 7 additions & 2 deletions src/e2e/tatum.faucet.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { Ethereum, Network, TatumSDK } from '../service'
import process from "process";
import { Status } from "../util";

describe('Tatum faucet', () => {
const SEPOLIA_VAULT = '0x712e3a792c974b3e3dbe41229ad4290791c75a82'
Expand Down Expand Up @@ -39,14 +41,17 @@ describe('Tatum faucet', () => {
expect(res.error?.code).toBe('faucet.balance')
})

it('should return valid transaction hash', async () => {
it.skip('should return success', async () => {
const tatum = await TatumSDK.init<Ethereum>({
network: Network.ETHEREUM_SEPOLIA,
apiKey: {
v4: process.env.V4_API_KEY_TESTNET,
}
})
const res = await tatum.faucet.fund(SEPOLIA_VAULT)

await tatum.destroy()
expect(res.data?.txId).toBeDefined()
expect(res.status).toBe(Status.SUCCESS)
})
})
})
14 changes: 5 additions & 9 deletions src/e2e/tatum.fee.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { ApiVersion, Network, TatumSDK } from '../service'
import { Bitcoin, Ethereum } from '../service'
import { ApiVersion, Bitcoin, Ethereum, Network, TatumSDK } from '../service'
import { Status } from '../util'

describe('Fee', () => {
Expand All @@ -11,16 +10,14 @@ describe('Fee', () => {
version: ApiVersion.V3,
})

try{
try {
const { data, status } = await tatum.fee.getCurrentFee()

expect(status).toBe(Status.SUCCESS)
expect(data.gasPrice.fast).toBeDefined()
}
finally {
} finally {
await tatum.destroy()
}

})

it('should return fee for btc testnet', async () => {
Expand All @@ -31,13 +28,12 @@ describe('Fee', () => {
version: ApiVersion.V3,
})

try{
try {
const { data, status } = await tatum.fee.getCurrentFee()
await tatum.destroy()
expect(status).toBe(Status.SUCCESS)
expect(data.fast).toBeDefined()
}
finally {
} finally {
await tatum.destroy()
}
})
Expand Down
4 changes: 2 additions & 2 deletions src/service/rpc/evm/EvmUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ export const EvmUtils = {
return true
}

const possibleArchiveMethod = POSSIBLE_ARCHIVE_METHODS.find(
(possibleArchiveMethod) => rpc.method.includes(possibleArchiveMethod.method),
const possibleArchiveMethod = POSSIBLE_ARCHIVE_METHODS.find((possibleArchiveMethod) =>
rpc.method.includes(possibleArchiveMethod.method),
)
if (possibleArchiveMethod) {
const param = rpc?.params?.[possibleArchiveMethod.index]
Expand Down
44 changes: 24 additions & 20 deletions src/service/rpc/generic/LoadBalancer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export class LoadBalancer implements AbstractRpcInterface {
[RpcNodeType.NORMAL]: {} as UrlIndex,
[RpcNodeType.ARCHIVE]: {} as UrlIndex,
}
private timeout: unknown
private interval: NodeJS.Timer
private network: Network
private noActiveNode = false

Expand All @@ -95,20 +95,22 @@ export class LoadBalancer implements AbstractRpcInterface {
await this.initRemoteHostsUrls()
}

// TODO: consider removing this because we already have a timeout in checkStatuses()
if (!config.rpc?.oneTimeLoadBalancing) {
this.timeout = setTimeout(() => this.checkStatuses(), Constant.OPEN_RPC.LB_INTERVAL)
// Check if we are running in Node.js environment
if (typeof process !== 'undefined' && process.release && process.release.name === 'node') {
process.on('exit', () => this.destroy())
}
if (typeof process !== 'undefined' && process.release && process.release.name === 'node') {
process.on('exit', () => this.destroy())
}

if (config.rpc?.oneTimeLoadBalancing) {
Utils.log({ id: this.id, message: 'oneTimeLoadBalancing enabled' })
setTimeout(() => this.checkStatuses(), Constant.OPEN_RPC.LB_INTERVAL)
} else {
await this.checkStatuses()
this.interval = setInterval(() => this.checkStatuses(), Constant.OPEN_RPC.LB_INTERVAL)
}
}

destroy() {
clearTimeout(this.timeout as number)
Utils.log({ id: this.id, message: 'Destroying LoadBalancer instance' })
clearInterval(this.interval)
process.off('exit', () => this.destroy())
}

private initCustomNodes(nodes: RpcNode[]) {
Expand Down Expand Up @@ -139,16 +141,18 @@ export class LoadBalancer implements AbstractRpcInterface {
}

private async checkStatuses() {
await this.checkStatus(RpcNodeType.NORMAL)
await this.checkStatus(RpcNodeType.ARCHIVE)
this.checkIfNoActiveNodes()

const { rpc } = Container.of(this.id).get(CONFIG)
if (!rpc?.oneTimeLoadBalancing) {
if (this.timeout) {
this.destroy()
}
this.timeout = setTimeout(() => this.checkStatuses(), Constant.OPEN_RPC.LB_INTERVAL)
try {
await this.checkStatus(RpcNodeType.NORMAL)
await this.checkStatus(RpcNodeType.ARCHIVE)
this.checkIfNoActiveNodes()
} catch (e) {
Utils.log({
id: this.id,
message: `LoadBalancing failed to check statuses. Error: ${JSON.stringify(
e,
Object.getOwnPropertyNames(e),
)}`,
})
}
}

Expand Down

0 comments on commit 16e7595

Please sign in to comment.