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
feat: Implement appconfig package #1625
Conversation
9370855
to
90f17ad
Compare
cacheControl := response.ResponseHeader().Get("Cache-Control") | ||
if cacheControl == "" { | ||
return nil | ||
} | ||
|
||
cacheDirectives, err := cacheobject.ParseResponseCacheControl(cacheControl) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if cacheDirectives.NoStore || cacheDirectives.NoCache != nil { | ||
return nil | ||
} | ||
|
||
result.maxAge = time.Second * time.Duration(cacheDirectives.MaxAge) |
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.
Cache-Control header handling:
- of header is missing or configured to disable caching then don't cache anything
- if MaxAge is set then use it
type SandboxesError struct { | ||
Message string `json:"error"` | ||
ExceptionID string `json:"exceptionId"` | ||
request *http.Request | ||
response *http.Response | ||
} |
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.
Difference to QueueError: There was a discrepancy between http code and the code in json response. Pepa said I should use the http code so I removed the code that was handling the code from json.
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.
🤦♂️ ok ✔️
// An error other than 404 is considered a temporary failure. Keep using the stale cache for staleCacheFallbackDuration as fallback. | ||
if fallbackItem != nil && now.Before(fallbackItem.expiresAt.Add(staleCacheFallbackDuration)) { | ||
logger.Warnf(ctx, `Using stale cache for app "%s": %s`, appID, err.Error()) | ||
|
||
return fallbackItem.config, 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.
Pepa wanted me to use the stale cache configuration on error while refreshing the configuration. In my opinion this should only be the case for temporary errors. This is only for errors of sandboxes api itself, invalid configuration is handled elsewhere.
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.
✔️
type attempt struct { | ||
delay time.Duration | ||
responses []*http.Response | ||
expectedETag string | ||
expectedErrorCode int | ||
expectedConfig AppProxyConfig | ||
} |
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.
The logic inside Loader is mainly about correct handling of ETag and Cache-Control headers - deciding when to contact the sandboxed api and when to use the cache. So the test cases need to do several attempts to get the config - some attempts are after a delay, others with incorrect etag, then the stale cache usage etc. Writing this test was the most complicated part.
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.
Nice test 👍
90f17ad
to
b4e4599
Compare
b4e4599
to
32482ad
Compare
ac6817c
to
cdfa7cd
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.
Nice, I have some notes
type SandboxesError struct { | ||
Message string `json:"error"` | ||
ExceptionID string `json:"exceptionId"` | ||
request *http.Request | ||
response *http.Response | ||
} |
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.
🤦♂️ ok ✔️
// An error other than 404 is considered a temporary failure. Keep using the stale cache for staleCacheFallbackDuration as fallback. | ||
if fallbackItem != nil && now.Before(fallbackItem.expiresAt.Add(staleCacheFallbackDuration)) { | ||
logger.Warnf(ctx, `Using stale cache for app "%s": %s`, appID, err.Error()) | ||
|
||
return fallbackItem.config, 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.
✔️
loader := NewLoader(log.NewDebugLogger(), clk, url) | ||
loader.sender = loader.sender.(client.Client).WithTransport(transport) |
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.
Please, inject the client as a dep. via constructor.
What if you need to mock the transport at a higher level?
You can also switch the test to blackbox mode, package ..._test
, you will only test the public interface of the Loader, and it will lead to a better design.
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.
Nice improvement, ty!
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.
Addressed in #1635
type attempt struct { | ||
delay time.Duration | ||
responses []*http.Response | ||
expectedETag string | ||
expectedErrorCode int | ||
expectedConfig AppProxyConfig | ||
} |
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.
Nice test 👍
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.
LGTM
Jira: https://keboola.atlassian.net/browse/PSGO-386
Changes: