From da05664e663a03588ac88699dc6192e55ebe2d51 Mon Sep 17 00:00:00 2001 From: Geoff Greer Date: Tue, 6 Aug 2024 21:57:27 -0700 Subject: [PATCH] Do DoOptions even for cached responses. (#205) 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. --- pkg/uhttp/wrapper.go | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/pkg/uhttp/wrapper.go b/pkg/uhttp/wrapper.go index bd6974a8..b48ee7e2 100644 --- a/pkg/uhttp/wrapper.go +++ b/pkg/uhttp/wrapper.go @@ -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 { @@ -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()