Skip to content

Commit

Permalink
Elwood Optimize /subscriptions call (#3645)
Browse files Browse the repository at this point in the history
* Reduce the amount of subscription check API calls to when we have valid subscription requests
  • Loading branch information
akuzni2 authored Jan 27, 2025
1 parent b81e24f commit 539e8a3
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/sharp-jobs-draw.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@chainlink/elwood-adapter': patch
---

Reduce the frequency of /subscriptions api calls to check active subscriptions to only when we desire to subscribe to a stream.
7 changes: 7 additions & 0 deletions packages/sources/elwood/src/transport/crypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,13 @@ export const transport: WebSocketTransport<WsTransportTypes> =
// are multiple EAs that share the same key then we should only subscribe to new symbols. For
// the same reason, it is not safe to unsubscribe otherwise another EA expecting the symbol
// may unexpectedly stop receiving data for it without knowing.

// `subscribes` will be populated if there are new subscriptions to be made. This can happen
// if we request a new pair to be subscribed to, or if the subscription set TTL has expired
if (!subscribes || subscribes.length === 0) return

logger.info(`Subscription requests: ${JSON.stringify(subscribes)}`)

const subscriptions = await getSubscriptions(
context.adapterSettings.API_ENDPOINT,
context.adapterSettings.API_KEY,
Expand Down
7 changes: 7 additions & 0 deletions packages/sources/elwood/src/transport/lwba.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,13 @@ export const transport: WebSocketTransport<WsTransportTypes> =
// are multiple EAs that share the same key then we should only subscribe to new symbols. For
// the same reason, it is not safe to unsubscribe otherwise another EA expecting the symbol
// may unexpectedly stop receiving data for it without knowing.

// `subscribes` will be populated if there are new subscriptions to be made. This can happen
// if we request a new pair to be subscribed to, or if the subscription set TTL has expired
if (!subscribes || subscribes.length === 0) return

logger.info(`Subscription requests: ${JSON.stringify(subscribes)}`)

const subscriptions = await getSubscriptions(
context.adapterSettings.API_ENDPOINT,
context.adapterSettings.API_KEY,
Expand Down
3 changes: 2 additions & 1 deletion packages/sources/elwood/test/integration/adapter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,12 @@ describe('websocket', () => {

it('should skip subscribing if already subscribed', async () => {
const symbol = `${dataAlreadySubscribed.base}-${dataAlreadySubscribed.quote}`
mockSubscriptionsResponse(apiKey, [symbol])
const subscriptionScope = mockSubscriptionsResponse(apiKey, [symbol])
const scope = mockSubscribeResponse(apiKey, symbol)
const response = await testAdapter.request(data)
expect(response.json()).toMatchSnapshot()
expect(scope.isDone()).toEqual(false)
expect(subscriptionScope.isDone()).toEqual(false)
})

it('should return success (LWBA)', async () => {
Expand Down

0 comments on commit 539e8a3

Please sign in to comment.