Skip to content

Commit

Permalink
Do DoOptions even for cached responses. (#205)
Browse files Browse the repository at this point in the history
WithJSONResponse and other options weren't called on cached responses. A lot of our connectors still parse the responses manually so we didn't run into this bug in testing.
  • Loading branch information
ggreer authored Aug 7, 2024
1 parent ccaa7f6 commit da05664
Showing 1 changed file with 17 additions and 15 deletions.
32 changes: 17 additions & 15 deletions pkg/uhttp/wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ func (c *BaseHttpClient) Do(req *http.Request, options ...DoOption) (*http.Respo
var (
cacheKey string
err error
resp *http.Response
)
l := ctxzap.Extract(req.Context())
if req.Method == http.MethodGet {
Expand All @@ -204,30 +205,31 @@ func (c *BaseHttpClient) Do(req *http.Request, options ...DoOption) (*http.Respo
return nil, err
}

resp, err := c.baseHttpCache.Get(cacheKey)
resp, err = c.baseHttpCache.Get(cacheKey)
if err != nil {
return nil, err
}
if resp != nil {
if resp == nil {
l.Debug("http cache miss", zap.String("cacheKey", cacheKey), zap.String("url", req.URL.String()))
} else {
l.Debug("http cache hit", zap.String("cacheKey", cacheKey), zap.String("url", req.URL.String()))
return resp, nil
}

l.Debug("http cache miss", zap.String("cacheKey", cacheKey), zap.String("url", req.URL.String()))
}

resp, err := c.HttpClient.Do(req)
if err != nil {
var urlErr *url.Error
if errors.As(err, &urlErr) {
if urlErr.Timeout() {
return nil, status.Error(codes.DeadlineExceeded, fmt.Sprintf("request timeout: %v", urlErr.URL))
if resp == nil {
resp, err = c.HttpClient.Do(req)
if err != nil {
var urlErr *url.Error
if errors.As(err, &urlErr) {
if urlErr.Timeout() {
return nil, status.Error(codes.DeadlineExceeded, fmt.Sprintf("request timeout: %v", urlErr.URL))
}
}
if errors.Is(err, context.DeadlineExceeded) {
return nil, status.Error(codes.DeadlineExceeded, "request timeout")
}
return nil, err
}
if errors.Is(err, context.DeadlineExceeded) {
return nil, status.Error(codes.DeadlineExceeded, "request timeout")
}
return nil, err
}

defer resp.Body.Close()
Expand Down

0 comments on commit da05664

Please sign in to comment.