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

Improve caching logic for http.send #5316

Merged

Conversation

anderseknert
Copy link
Member

In order to calculate if a cached entry expired or not, we would previously read the headers stored with the request, and parse each of the relevant date-related ones and use those to compare with the current time. #4960 improved this by allowing us to ignore the Date header completely when force_cache is enabled.

We now go one step further, and calculate and store an expiresAt value at the time the entry is created. This allows for a much simpler check of expiry, and avoids re-parsing the header values with each lookup.

Fixes #5300

Signed-off-by: Anders Eknert anders@eknert.com

@netlify
Copy link

netlify bot commented Oct 27, 2022

Deploy Preview for openpolicyagent ready!

Name Link
🔨 Latest commit 41f6f73
🔍 Latest deploy log https://app.netlify.com/sites/openpolicyagent/deploys/635a4be31381c50008893734
😎 Deploy Preview https://deploy-preview-5316--openpolicyagent.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

In order to calculate if a cached entry expired or not, we would previously
read the headers stored with the request, and parse each of the relevant date-related
ones and use those to compare with the current time. open-policy-agent#4960 improved this by allowing
us to ignore the Date header completely when force_cache is enabled.

We now go one step further, and calculate and store an expiresAt value at the time the
entry is created. This allows for a much simpler check of expiry, and avoids re-parsing
the header values with each lookup.

Fixes open-policy-agent#5300

Signed-off-by: Anders Eknert <anders@eknert.com>
@anderseknert anderseknert force-pushed the improve-http-send-caching branch from 5c43419 to 41f6f73 Compare October 27, 2022 09:14
Copy link
Contributor

@srenatus srenatus left a comment

Choose a reason for hiding this comment

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

Such a nice cleanup. Thanks!

Comment on lines +915 to +924
if maxAge != -1 {
createdAt, err := getResponseHeaderDate(headers)
if err != nil {
return time.Time{}, err
}
expiresAt = createdAt.Add(time.Second * time.Duration(maxAge))
} else {
expiresAt = getResponseHeaderExpires(headers)
}
return expiresAt, nil
Copy link
Contributor

Choose a reason for hiding this comment

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

[nit] It's the same, I'm just trying how this variant feels...:

Suggested change
if maxAge != -1 {
createdAt, err := getResponseHeaderDate(headers)
if err != nil {
return time.Time{}, err
}
expiresAt = createdAt.Add(time.Second * time.Duration(maxAge))
} else {
expiresAt = getResponseHeaderExpires(headers)
}
return expiresAt, nil
if maxAge != -1 {
createdAt, err := getResponseHeaderDate(headers)
if err != nil {
return time.Time{}, err
}
return createdAt.Add(time.Second * time.Duration(maxAge)), nil
}
return getResponseHeaderExpires(headers), nil

I think I'd slightly prefer this. But it's not a blocker, of course.

Copy link
Member Author

Choose a reason for hiding this comment

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

I like that it's more compact, but OTOH it's somewhat easier for me to read that the "expiresAt" is ultimately what is returned... I guess one can figure that out from the function name, but still :)

} else {
_, err := parseResponseHeaders(resp.Header)
var err error
expiresAt, err = expiryFromHeaders(resp.Header)
if err != nil {
return nil, err
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

👍


// check the freshness of the cached response
if isCachedResponseFresh(c.bctx, headers, c.forceCacheParams) {
if getCurrentTime(c.bctx).Before(cachedRespData.ExpiresAt) {
Copy link
Contributor

Choose a reason for hiding this comment

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

👍

@anderseknert anderseknert merged commit 1c8ac42 into open-policy-agent:main Oct 27, 2022
@anderseknert anderseknert deleted the improve-http-send-caching branch October 27, 2022 09:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Improve http.send cache lookup and freshness logic
2 participants