Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement a pattern for dealing with upstream APIs which are slow on the first hit; affects [endpoint] #9233

Merged
merged 7 commits into from
Jun 13, 2023
2 changes: 1 addition & 1 deletion core/base-service/cache-headers.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,14 @@ function coalesceCacheLength({
assert(defaultCacheLengthSeconds !== undefined)

const cacheLength = coalesce(
serviceOverrideCacheLengthSeconds,
serviceDefaultCacheLengthSeconds,
defaultCacheLengthSeconds
)

// Overrides can apply _more_ caching, but not less. Query param overriding
// can request more overriding than service override, but not less.
const candidateOverrides = [
serviceOverrideCacheLengthSeconds,
overrideCacheLengthFromQueryParams(queryParams),
Comment on lines 41 to 50
Copy link
Member Author

@chris48s chris48s Jun 2, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the history on this, see #2755

Basically, the only thing relying on the logic as it stood here was the endpoint badge (we don't want endpoint badge users to be able to set a lower cacheSeconds value than the service default). I've moved this logic so it gets applied in endpoint.service.js so that we can now lower cacheSeconds with a custom exception property if we want. If we left this as it was, we'd only be able to raise cacheSeconds with a custom exception property (but not lower it).

].filter(x => x !== undefined)

Expand Down
4 changes: 2 additions & 2 deletions core/base-service/cache-headers.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,12 @@ describe('Cache header functions', function () {
serviceDefaultCacheLengthSeconds: 900,
serviceOverrideCacheLengthSeconds: 400,
queryParams: {},
}).expect(900)
}).expect(400)
given({
cacheHeaderConfig,
serviceOverrideCacheLengthSeconds: 400,
queryParams: {},
}).expect(777)
}).expect(400)
given({
cacheHeaderConfig,
serviceOverrideCacheLengthSeconds: 900,
Expand Down
8 changes: 3 additions & 5 deletions core/base-service/legacy-request-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,9 @@ function handleRequest(cacheHeaderConfig, handlerOptions) {
// `defaultCacheLengthSeconds` can be overridden by
// `serviceDefaultCacheLengthSeconds` (either by category or on a badge-
// by-badge basis). Then in turn that can be overridden by
// `serviceOverrideCacheLengthSeconds` (which we expect to be used only in
// the dynamic badge) but only if `serviceOverrideCacheLengthSeconds` is
// longer than `serviceDefaultCacheLengthSeconds` and then the `cacheSeconds`
// query param can also override both of those but again only if `cacheSeconds`
// is longer.
// `serviceOverrideCacheLengthSeconds`.
// Then the `cacheSeconds` query param can also override both of those
// but only if `cacheSeconds` is longer.
//
// When the legacy services have been rewritten, all the code in here
// will go away, which should achieve this goal in a simpler way.
Expand Down
6 changes: 3 additions & 3 deletions core/base-service/legacy-request-handler.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ describe('The request handler', function () {
expect(headers['cache-control']).to.equal('max-age=900, s-maxage=900')
})

it('should let live service data override the default cache headers with longer value', async function () {
it('should allow serviceData to override the default cache headers with longer value', async function () {
camp.route(
/^\/testing\/([^/]+)\.(svg|png|gif|jpg|json)$/,
handleRequest(
Expand All @@ -168,7 +168,7 @@ describe('The request handler', function () {
expect(headers['cache-control']).to.equal('max-age=400, s-maxage=400')
})

it('should not let live service data override the default cache headers with shorter value', async function () {
it('should allow serviceData to override the default cache headers with shorter value', async function () {
camp.route(
/^\/testing\/([^/]+)\.(svg|png|gif|jpg|json)$/,
handleRequest(
Expand All @@ -185,7 +185,7 @@ describe('The request handler', function () {
)

const { headers } = await got(`${baseUrl}/testing/123.json`)
expect(headers['cache-control']).to.equal('max-age=300, s-maxage=300')
expect(headers['cache-control']).to.equal('max-age=200, s-maxage=200')
calebcartwright marked this conversation as resolved.
Show resolved Hide resolved
})

it('should set the expires header to current time + cacheSeconds', async function () {
Expand Down