Skip to content

Commit

Permalink
fix: avoid initializing multiple connections (#1096)
Browse files Browse the repository at this point in the history
## High Level Overview of Change

Title says it all. There are multiple connections made to a node in the
current implementation, at different ports (which all forward to the
same place on livenet). While the connection isn't sustained, it still
has to be made and canceled.

### Context of Change

There is a lot of load on s2 right now, so saving a connection or two
might make a difference.

### Type of Change

- [x] Bug fix (non-breaking change which fixes an issue)

### TypeScript/Hooks Update

N/A

## Before / After

<!--
If just refactoring / back-end changes, this can be just an in-English
description of the change at a technical level.
If a UI change, screenshots should be included.
-->

## Test Plan

Tested locally.
  • Loading branch information
mvadari authored Dec 2, 2024
1 parent 9eea4fa commit 1e89d53
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 27 deletions.
14 changes: 8 additions & 6 deletions server/lib/rippled.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ const utils = require('./utils')
const streams = require('./streams')

const RIPPLEDS = []
process.env.VITE_RIPPLED_HOST?.split(',').forEach((d) => {
const rippled = d.split(':')
RIPPLEDS.push(
`wss://${rippled[0]}:${rippled[1] || process.env.VITE_RIPPLED_WS_PORT}`,
`wss://${rippled[0]}`,
)
process.env.VITE_RIPPLED_HOST?.split(',').forEach((host) => {
if (host?.includes(':')) {
RIPPLEDS.push(`wss://${host}`)
} else if (process.env.VITE_RIPPLED_WS_PORT) {
RIPPLEDS.push(`wss://${host}:${process.env.VITE_RIPPLED_WS_PORT}`)
} else {
RIPPLEDS.push(`wss://${host}`)
}
})

const RIPPLED_CLIENT = new XrplClient(RIPPLEDS, { tryAllNodes: true })
Expand Down
7 changes: 5 additions & 2 deletions server/routes/v1/tokens.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,15 @@ async function fetchXRPLMetaTokens(offset) {
},
)
.then((resp) => resp.data)
.catch((e) => log.error(e))
.catch((e) => {
log.error(e)
return { count: 0 }
})
}

async function cacheXRPLMetaTokens() {
let offset = 0
let tokensDataBatch = []
let tokensDataBatch = {}
const allTokensFetched = []

tokensDataBatch = await fetchXRPLMetaTokens(0)
Expand Down
10 changes: 6 additions & 4 deletions src/containers/shared/SocketContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,13 @@ function getSocket(rippledUrl?: string): ExplorerXrplClient {

if (host?.includes(':')) {
wsUrls.push(`${prefix}://${host}`)
} else if (process.env.VITE_RIPPLED_WS_PORT) {
wsUrls.push(`${prefix}://${host}:${process.env.VITE_RIPPLED_WS_PORT}`)
if (process.env.VITE_ENVIRONMENT === 'custom') {
wsUrls.push(`${prefix}://${host}`)
}
} else {
wsUrls.push.apply(wsUrls, [
`${prefix}://${host}:${process.env.VITE_RIPPLED_WS_PORT}`,
`${prefix}://${host}:443`,
])
wsUrls.push(`${prefix}://${host}`)
}
})

Expand Down
21 changes: 6 additions & 15 deletions src/containers/shared/test/SocketContext.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ describe('getSocket', () => {
const client = getSocket()
expect(XrplClient).toHaveBeenNthCalledWith(
1,
['wss://somewhere.com:51233', 'wss://somewhere.com:443'],
['wss://somewhere.com:51233'],
{
tryAllNodes: true,
},
Expand All @@ -47,12 +47,7 @@ describe('getSocket', () => {
const client = getSocket()
expect(XrplClient).toHaveBeenNthCalledWith(
1,
[
'wss://somewhere.com:51233',
'wss://somewhere.com:443',
'wss://elsewhere.com:51233',
'wss://elsewhere.com:443',
],
['wss://somewhere.com:51233', 'wss://elsewhere.com:51233'],
{
tryAllNodes: true,
},
Expand All @@ -71,7 +66,7 @@ describe('getSocket', () => {
const client = getSocket()
expect(XrplClient).toHaveBeenNthCalledWith(
1,
['ws://somewhere.com:51233', 'ws://somewhere.com:443'],
['ws://somewhere.com:51233'],
{
tryAllNodes: true,
},
Expand Down Expand Up @@ -120,13 +115,9 @@ describe('getSocket', () => {

it('should use ws when supplied entry is for a localhost', () => {
const client = getSocket('localhost')
expect(XrplClient).toHaveBeenNthCalledWith(
1,
['ws://localhost:51233', 'ws://localhost:443'],
{
tryAllNodes: true,
},
)
expect(XrplClient).toHaveBeenNthCalledWith(1, ['ws://localhost:51233'], {
tryAllNodes: true,
})

expect((client as any).p2pSocket).not.toBeDefined()
})
Expand Down

0 comments on commit 1e89d53

Please sign in to comment.