Skip to content

Commit 955130d

Browse files
shkapercanxuemianbaoStarpTech
authored
update QuickLRU option (2) (#21)
Co-authored-by: ke hu <[email protected]> Co-authored-by: Dustin Deus <[email protected]>
1 parent cf87423 commit 955130d

File tree

4 files changed

+86
-1
lines changed

4 files changed

+86
-1
lines changed

package-lock.json

Lines changed: 30 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,10 @@
6060
"graphql": "^15.3.0 || ^16.0.0"
6161
},
6262
"devDependencies": {
63+
"@sinonjs/fake-timers": "^8.1.0",
6364
"@tsconfig/node12": "^1.0.9",
65+
"@types/node": "^14.17.3",
66+
"@types/sinonjs__fake-timers": "^8.1.0",
6467
"@types/node": "^17.0.5",
6568
"abort-controller": "^3.0.0",
6669
"apollo-datasource-rest": "^3.4.0",

src/http-data-source.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ export abstract class HTTPDataSource<TContext = any> extends DataSource {
9393
super()
9494
this.memoizedResults = new QuickLRU({
9595
maxSize: this.options?.lru?.maxSize ? this.options.lru.maxSize : 100,
96+
maxAge: this.options?.lru?.maxAge,
9697
})
9798
this.pool = options?.pool ?? new Pool(this.baseURL, options?.clientOptions)
9899
this.globalRequestOptions = options?.requestOptions
@@ -325,7 +326,6 @@ export abstract class HTTPDataSource<TContext = any> extends DataSource {
325326
})
326327
.catch((err) => this.logger?.error(err))
327328
}
328-
329329
return response
330330
} catch (error: any) {
331331
this.onError?.(error, request)

test/http-data-source.test.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import querystring from 'querystring'
66
import { HTTPDataSource, Request, Response, RequestError } from '../src'
77
import { AddressInfo } from 'net'
88
import { KeyValueCacheSetOptions } from 'apollo-server-caching'
9+
import FakeTimers from '@sinonjs/fake-timers'
910

1011
const agent = new Agent({
1112
keepAliveTimeout: 10, // milliseconds
@@ -1336,6 +1337,57 @@ test('Should only cache GET successful responses with the correct status code',
13361337
t.is(cacheMap.size, 0)
13371338
})
13381339

1340+
test.serial('Global maxAge should be used when no maxAge was set or similar.', async (t) => {
1341+
const path = '/'
1342+
1343+
const clock = FakeTimers.install()
1344+
t.teardown(clock.uninstall.bind(clock))
1345+
1346+
const server = http.createServer((req, res) => {
1347+
t.is(req.method, 'GET')
1348+
res.writeHead(200)
1349+
res.end()
1350+
res.socket?.unref()
1351+
})
1352+
1353+
t.teardown(server.close.bind(server))
1354+
1355+
server.listen()
1356+
1357+
const baseURL = `http://localhost:${(server.address() as AddressInfo)?.port}`
1358+
1359+
let testResponse: Response<any> | {
1360+
memoized: boolean
1361+
} = {
1362+
memoized: false,
1363+
}
1364+
const maxAge = 10000
1365+
const dataSource = new (class extends HTTPDataSource {
1366+
constructor() {
1367+
super(baseURL, {
1368+
lru: {
1369+
maxAge: maxAge
1370+
}
1371+
})
1372+
}
1373+
getFoo() {
1374+
return this.get(path)
1375+
}
1376+
onResponse(_: Request, response: Response<any>) {
1377+
testResponse = response
1378+
return response
1379+
}
1380+
})()
1381+
1382+
await dataSource.getFoo()
1383+
t.is(testResponse.memoized, false)
1384+
await dataSource.getFoo()
1385+
t.is(testResponse.memoized, true)
1386+
clock.tick(maxAge)
1387+
await dataSource.getFoo()
1388+
t.is(testResponse.memoized, false)
1389+
})
1390+
13391391
test('Response is not cached due to origin error', async (t) => {
13401392
const path = '/'
13411393

0 commit comments

Comments
 (0)