From 9f6140538a35d626b1c720b06a8095cd1f4d7498 Mon Sep 17 00:00:00 2001 From: Geoff Greer Date: Tue, 6 Aug 2024 16:24:00 -0700 Subject: [PATCH] Better logs. Use normal err variable name. (#203) * Better logs. Use normal err variable name. * Only cache if status code is 200 OK. Other 2xx codes might not be something we want to cache. --- pkg/uhttp/gocache.go | 14 +++++++++----- pkg/uhttp/wrapper.go | 11 ++++++++++- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/pkg/uhttp/gocache.go b/pkg/uhttp/gocache.go index f39cbed9..2cbbde41 100644 --- a/pkg/uhttp/gocache.go +++ b/pkg/uhttp/gocache.go @@ -23,17 +23,21 @@ type GoCache struct { func NewGoCache(ctx context.Context, cfg CacheConfig) (GoCache, error) { l := ctxzap.Extract(ctx) + if cfg.DisableCache { + l.Debug("http cache disabled") + return GoCache{}, nil + } config := bigCache.DefaultConfig(time.Duration(cfg.CacheTTL) * time.Second) config.Verbose = cfg.LogDebug config.Shards = 4 config.HardMaxCacheSize = cfg.CacheMaxSize // value in MB, 0 value means no size limit - cache, initErr := bigCache.New(ctx, config) - if initErr != nil { - l.Error("in-memory cache error", zap.Any("NewGoCache", initErr)) - return GoCache{}, initErr + cache, err := bigCache.New(ctx, config) + if err != nil { + l.Error("http cache initialization error", zap.Error(err)) + return GoCache{}, err } - l.Debug("in-memory cache config", zap.Any("config", config)) + l.Debug("http cache config", zap.Any("config", config)) gc := GoCache{ rootLibrary: cache, } diff --git a/pkg/uhttp/wrapper.go b/pkg/uhttp/wrapper.go index 9a3dff33..bd6974a8 100644 --- a/pkg/uhttp/wrapper.go +++ b/pkg/uhttp/wrapper.go @@ -10,6 +10,8 @@ import ( "io" "net/http" "net/url" + "os" + "strconv" v2 "github.com/conductorone/baton-sdk/pb/c1/connector/v2" "github.com/conductorone/baton-sdk/pkg/helpers" @@ -53,6 +55,7 @@ type ( LogDebug bool CacheTTL int32 CacheMaxSize int + DisableCache bool } ) @@ -67,11 +70,17 @@ func NewBaseHttpClient(httpClient *http.Client) *BaseHttpClient { func NewBaseHttpClientWithContext(ctx context.Context, httpClient *http.Client) (*BaseHttpClient, error) { l := ctxzap.Extract(ctx) + disableCache, err := strconv.ParseBool(os.Getenv("BATON_DISABLE_HTTP_CACHE")) + if err != nil { + disableCache = false + } + var ( config = CacheConfig{ LogDebug: l.Level().Enabled(zap.DebugLevel), CacheTTL: int32(3600), // seconds CacheMaxSize: int(2048), // MB + DisableCache: disableCache, } ok bool ) @@ -262,7 +271,7 @@ func (c *BaseHttpClient) Do(req *http.Request, options ...DoOption) (*http.Respo return resp, status.Error(codes.Unknown, fmt.Sprintf("unexpected status code: %d", resp.StatusCode)) } - if req.Method == http.MethodGet { + if req.Method == http.MethodGet && resp.StatusCode == http.StatusOK { err := c.baseHttpCache.Set(cacheKey, resp) if err != nil { l.Debug("error setting cache", zap.String("cacheKey", cacheKey), zap.String("url", req.URL.String()), zap.Error(err))