Skip to content

Commit

Permalink
dd cache ttl refresh feature (#3245)
Browse files Browse the repository at this point in the history
  • Loading branch information
karen-stepanyan committed Apr 23, 2024
1 parent 52140ac commit 473b33b
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/gorgeous-toys-taste.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@chainlink/tradingeconomics-adapter': patch
---

Added support for cache TTL refresh on heartbeat messages for Forex endpoint
6 changes: 6 additions & 0 deletions packages/sources/tradingeconomics/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@

This document was generated automatically. Please see [README Generator](../../scripts#readme-generator) for more info.

## Known Issues

### CACHE_MAX_AGE interaction with Heartbeat messages

If `CACHE_MAX_AGE` is set below a current heartbeat interval (45000ms), the extended cache TTL feature for out-of-market-hours that relies on heartbeats will not work.

## Environment Variables

| Required? | Name | Description | Type | Options | Default |
Expand Down
5 changes: 5 additions & 0 deletions packages/sources/tradingeconomics/docs/known-issues.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
## Known Issues

### CACHE_MAX_AGE interaction with Heartbeat messages

If `CACHE_MAX_AGE` is set below a current heartbeat interval (45000ms), the extended cache TTL feature for out-of-market-hours that relies on heartbeats will not work.
24 changes: 21 additions & 3 deletions packages/sources/tradingeconomics/src/transport/price-ws.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { WebsocketReverseMappingTransport } from '@chainlink/external-adapter-framework/transports'
import {
WebsocketReverseMappingTransport,
WebSocketTransport,
} from '@chainlink/external-adapter-framework/transports'
import { makeLogger } from '@chainlink/external-adapter-framework/util'
import { BaseEndpointTypes } from '../endpoint/price'

Expand Down Expand Up @@ -27,6 +30,14 @@ type WsTransportTypes = BaseEndpointTypes & {
WsMessage: Message
}
}
/*
Tradingeconomics EA currently does not receive asset prices during off-market hours. When a heartbeat message is received during these hours,
we update the TTL of cache entries that EA is requested to provide a price during off-market hours.
*/
const updateTTL = async (transport: WebSocketTransport<WsTransportTypes>, ttl: number) => {
const params = await transport.subscriptionSet.getAll()
transport.responseCache.writeTTL(transport.name, params, ttl)
}

export const wsTransport: WebsocketReverseMappingTransport<WsTransportTypes, string> =
new WebsocketReverseMappingTransport<WsTransportTypes, string>({
Expand All @@ -35,10 +46,17 @@ export const wsTransport: WebsocketReverseMappingTransport<WsTransportTypes, str
return `${WS_API_ENDPOINT}?client=${API_CLIENT_KEY}:${API_CLIENT_SECRET}`
},
handlers: {
message: (message) => {
if (!message.topic || message.topic === 'keepalive') {
message: (message, context) => {
if (!message.topic) {
return []
}
// Check for a heartbeat message, refresh the TTLs of all requested entries in the cache
if (message.topic === 'keepalive') {
wsTransport.lastMessageReceivedAt = Date.now()
updateTTL(wsTransport, context.adapterSettings.CACHE_MAX_AGE)
return []
}

const pair = wsTransport.getReverseMapping(message.s)
if (!pair) {
logger.error(`Pair not found in websocket reverse map for message symbol - ${message.s}`)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,21 @@ exports[`websocket price endpoint should return success 1`] = `
}
`;

exports[`websocket price endpoint should update the ttl after heartbeat is received 1`] = `
{
"data": {
"result": 0.776530152665828,
},
"result": 0.776530152665828,
"statusCode": 200,
"timestamps": {
"providerDataReceivedUnixMs": 1018,
"providerDataStreamEstablishedUnixMs": 1010,
"providerIndicatedTimeUnixMs": 1659472542655,
},
}
`;

exports[`websocket stock endpoint should return success 1`] = `
{
"data": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
setEnvVariables,
mockWebSocketProvider,
MockWebsocketServer,
runAllUntilTime,
} from '@chainlink/external-adapter-framework/util/testing-utils'
import FakeTimers from '@sinonjs/fake-timers'
import { WebSocketClassProvider } from '@chainlink/external-adapter-framework/transports'
Expand Down Expand Up @@ -52,16 +53,25 @@ describe('websocket', () => {
await testAdapter.api.close()
})

describe('price endpoint', () => {
describe('stock endpoint', () => {
it('should return success', async () => {
const response = await testAdapter.request(dataPrice)
const response = await testAdapter.request(dataStock)
expect(response.json()).toMatchSnapshot()
})
})

describe('stock endpoint', () => {
describe('price endpoint', () => {
it('should return success', async () => {
const response = await testAdapter.request(dataStock)
const response = await testAdapter.request(dataPrice)
expect(response.json()).toMatchSnapshot()
})

it('should update the ttl after heartbeat is received', async () => {
// The cache ttl is 90 seconds. Mocked heartbeat message is sent after 10s after connection which should
// update the ttl and therefore after 93 seconds (from the initial message) we can access the asset
await runAllUntilTime(testAdapter.clock, 93000)
const response = await testAdapter.request(dataPrice)
expect(response.statusCode).toBe(200)
expect(response.json()).toMatchSnapshot()
})
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,9 @@ export const mockWebSocketServer = (url: string) => {
topic: 'USDCAD',
}),
)
setTimeout(() => {
socket.send(JSON.stringify({ topic: 'keepalive' }))
}, 10000)
} else {
// stock endpoint
socket.send(
Expand Down

0 comments on commit 473b33b

Please sign in to comment.