Skip to content

Commit e49983b

Browse files
committed
Optional sleep in http transport
1 parent 7aadb2a commit e49983b

2 files changed

Lines changed: 52 additions & 1 deletion

File tree

src/transports/http.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,9 @@ export interface HttpTransportConfig<T extends HttpTransportGenerics> {
9393
res: AxiosResponse<T['Provider']['ResponseBody']>,
9494
adapterSettings: T['Settings'],
9595
) => ProviderResult<T>[]
96+
97+
// Optional custom sleep time for the background execute loop
98+
backgroundSleepMs?: number
9699
}
97100

98101
/**
@@ -189,6 +192,11 @@ export class HttpTransport<T extends HttpTransportGenerics> extends Subscription
189192
// We're not sleeping here on purpose. We sleep when there are no entries in the subscription set to avoid polling too
190193
// frequently, but if we have entries we want the background execute to be re-run ASAP so we can prepare the next batch
191194
// of requests, and the sleep to rate-limit will be performed by the rate-limiter in the Requester.
195+
// Unless config.backgroundSleepMs is set
196+
if (this.config.backgroundSleepMs) {
197+
logger.trace(`Sleep ${this.config.backgroundSleepMs}ms - config.backgroundSleepMs`)
198+
await sleep(this.config.backgroundSleepMs)
199+
}
192200
return
193201
}
194202

test/transports/http.test.ts

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,12 @@ const BACKGROUND_EXECUTE_MS_HTTP = 1000
9393
class MockHttpTransport extends HttpTransport<HttpTransportTypes> {
9494
backgroundExecuteCalls = 0
9595

96-
constructor(private callSuper = false) {
96+
constructor(
97+
private callSuper = false,
98+
backgroundSleepMs?: number,
99+
) {
97100
super({
101+
backgroundSleepMs,
98102
prepareRequests: (params) => ({
99103
params,
100104
request: {
@@ -203,6 +207,45 @@ test.serial('sends request to DP and returns response', async (t) => {
203207
})
204208
})
205209

210+
test.serial(
211+
'backgroundSleepMs delays data provider requests until the configured sleep has elapsed on the fake clock',
212+
async (t) => {
213+
const bgSleepMs = 2_000
214+
const fromSymbol = 'BGSLEEPCHK'
215+
let dataProviderRequests = 0
216+
217+
axiosMock
218+
.onPost(URL + endpoint, {
219+
pairs: [{ base: fromSymbol, quote: to }],
220+
})
221+
.reply(() => {
222+
dataProviderRequests++
223+
return [200, {}]
224+
})
225+
226+
const adapter = new Adapter({
227+
name: 'TEST',
228+
defaultEndpoint: 'test',
229+
endpoints: [
230+
new AdapterEndpoint({
231+
name: 'test',
232+
inputParameters,
233+
transport: new MockHttpTransport(true, bgSleepMs),
234+
}),
235+
],
236+
})
237+
238+
const testAdapter = await TestAdapter.startWithMockedCache(adapter, t.context)
239+
await testAdapter.request({ from: fromSymbol, to })
240+
241+
t.is(dataProviderRequests, 0)
242+
await runAllUntilTime(t.context.clock, bgSleepMs / 2)
243+
t.is(dataProviderRequests, 0)
244+
await runAllUntilTime(t.context.clock, bgSleepMs)
245+
t.is(dataProviderRequests, 1)
246+
},
247+
)
248+
206249
test.serial(
207250
'per minute rate limit of 4 with one batch transport results in a call every 15s',
208251
async (t) => {

0 commit comments

Comments
 (0)