Skip to content

Commit

Permalink
Better logs. Use normal err variable name. (#203)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
ggreer authored Aug 6, 2024
1 parent cb3f3a4 commit 9f61405
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
14 changes: 9 additions & 5 deletions pkg/uhttp/gocache.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down
11 changes: 10 additions & 1 deletion pkg/uhttp/wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -53,6 +55,7 @@ type (
LogDebug bool
CacheTTL int32
CacheMaxSize int
DisableCache bool
}
)

Expand All @@ -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
)
Expand Down Expand Up @@ -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))
Expand Down

0 comments on commit 9f61405

Please sign in to comment.