-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Improve caching logic for http.send #5316
Conversation
✅ Deploy Preview for openpolicyagent ready!
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>
5c43419
to
41f6f73
Compare
There was a problem hiding this 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!
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 |
There was a problem hiding this comment.
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...:
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.
There was a problem hiding this comment.
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 | ||
} | ||
} |
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
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