From 539e8a3ed516efd7f0dd5fe2ac23f7f918bbeecc Mon Sep 17 00:00:00 2001 From: Alex Date: Mon, 27 Jan 2025 11:27:04 -0700 Subject: [PATCH] Elwood Optimize /subscriptions call (#3645) * Reduce the amount of subscription check API calls to when we have valid subscription requests --- .changeset/sharp-jobs-draw.md | 5 +++++ packages/sources/elwood/src/transport/crypto.ts | 7 +++++++ packages/sources/elwood/src/transport/lwba.ts | 7 +++++++ packages/sources/elwood/test/integration/adapter.test.ts | 3 ++- 4 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 .changeset/sharp-jobs-draw.md diff --git a/.changeset/sharp-jobs-draw.md b/.changeset/sharp-jobs-draw.md new file mode 100644 index 0000000000..077e56b29c --- /dev/null +++ b/.changeset/sharp-jobs-draw.md @@ -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. diff --git a/packages/sources/elwood/src/transport/crypto.ts b/packages/sources/elwood/src/transport/crypto.ts index d9d640f714..cb24a01cb7 100644 --- a/packages/sources/elwood/src/transport/crypto.ts +++ b/packages/sources/elwood/src/transport/crypto.ts @@ -102,6 +102,13 @@ export const transport: WebSocketTransport = // 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, diff --git a/packages/sources/elwood/src/transport/lwba.ts b/packages/sources/elwood/src/transport/lwba.ts index 5aab43781c..0f8aa5f716 100644 --- a/packages/sources/elwood/src/transport/lwba.ts +++ b/packages/sources/elwood/src/transport/lwba.ts @@ -104,6 +104,13 @@ export const transport: WebSocketTransport = // 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, diff --git a/packages/sources/elwood/test/integration/adapter.test.ts b/packages/sources/elwood/test/integration/adapter.test.ts index a181f18c3d..52fc2a6733 100644 --- a/packages/sources/elwood/test/integration/adapter.test.ts +++ b/packages/sources/elwood/test/integration/adapter.test.ts @@ -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 () => {