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

feat: Implement appconfig package #1625

Merged
merged 3 commits into from Mar 6, 2024

Conversation

jachym-tousek-keboola
Copy link
Contributor

@jachym-tousek-keboola jachym-tousek-keboola commented Feb 28, 2024

Jira: https://keboola.atlassian.net/browse/PSGO-386

Changes:

  • Added appconfig package to load configuration from Sandboxes API.
  • Unused for now, proxy will use it in next PR.

@jachym-tousek-keboola jachym-tousek-keboola force-pushed the jt-PSGO-386-sandboxes-service branch 2 times, most recently from 9370855 to 90f17ad Compare February 29, 2024 10:50
Comment on lines +49 to +62
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)
Copy link
Contributor Author

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

Comment on lines +70 to +74
type SandboxesError struct {
Message string `json:"error"`
ExceptionID string `json:"exceptionId"`
request *http.Request
response *http.Response
}
Copy link
Contributor Author

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.

Copy link
Collaborator

Choose a reason for hiding this comment

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

🤦‍♂️ ok ✔️

Comment on lines +96 to +101
// 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
}
Copy link
Contributor Author

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.

Copy link
Collaborator

Choose a reason for hiding this comment

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

✔️

Comment on lines +26 to +32
type attempt struct {
delay time.Duration
responses []*http.Response
expectedETag string
expectedErrorCode int
expectedConfig AppProxyConfig
}
Copy link
Contributor Author

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.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Nice test 👍

Copy link
Collaborator

@michaljurecko michaljurecko left a 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

Comment on lines +70 to +74
type SandboxesError struct {
Message string `json:"error"`
ExceptionID string `json:"exceptionId"`
request *http.Request
response *http.Response
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

🤦‍♂️ ok ✔️

Comment on lines +96 to +101
// 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
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

✔️

Comment on lines +272 to +273
loader := NewLoader(log.NewDebugLogger(), clk, url)
loader.sender = loader.sender.(client.Client).WithTransport(transport)
Copy link
Collaborator

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Nice improvement, ty!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Addressed in #1635

Comment on lines +26 to +32
type attempt struct {
delay time.Duration
responses []*http.Response
expectedETag string
expectedErrorCode int
expectedConfig AppProxyConfig
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nice test 👍

Copy link
Collaborator

@michaljurecko michaljurecko left a comment

Choose a reason for hiding this comment

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

LGTM

@jachym-tousek-keboola jachym-tousek-keboola merged commit b0e6d92 into main Mar 6, 2024
12 of 13 checks passed
@jachym-tousek-keboola jachym-tousek-keboola deleted the jt-PSGO-386-sandboxes-service branch March 6, 2024 08:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants