diff --git a/pkg/connectorbuilder/connectorbuilder.go b/pkg/connectorbuilder/connectorbuilder.go index 8bd090d7..a66b6ec5 100644 --- a/pkg/connectorbuilder/connectorbuilder.go +++ b/pkg/connectorbuilder/connectorbuilder.go @@ -21,6 +21,7 @@ import ( "github.com/conductorone/baton-sdk/pkg/pagination" "github.com/conductorone/baton-sdk/pkg/types" "github.com/conductorone/baton-sdk/pkg/types/tasks" + "github.com/conductorone/baton-sdk/pkg/uhttp" ) type ResourceSyncer interface { @@ -351,6 +352,13 @@ func (b *builderImpl) ListResourceTypes( tt := tasks.ListResourceTypesType var out []*v2.ResourceType + l := ctxzap.Extract(ctx) + // Clear all http caches at the start of a sync. This must be run in the child process, which is why it's in this function and not in syncer.go + err := uhttp.ClearCaches(ctx) + if err != nil { + l.Warn("error clearing http caches", zap.Error(err)) + } + for _, rb := range b.resourceBuilders { out = append(out, rb.ResourceType(ctx)) } diff --git a/pkg/uhttp/gocache.go b/pkg/uhttp/gocache.go index c29d4296..7fbf0b04 100644 --- a/pkg/uhttp/gocache.go +++ b/pkg/uhttp/gocache.go @@ -158,8 +158,10 @@ func (g *GoCache) Delete(key string) error { return nil } -func (g *GoCache) Clear() error { +func (g *GoCache) Clear(ctx context.Context) error { + l := ctxzap.Extract(ctx) if g.rootLibrary == nil { + l.Debug("clear: rootLibrary is nil") return nil } @@ -167,7 +169,12 @@ func (g *GoCache) Clear() error { if err != nil { return err } + err = g.rootLibrary.ResetStats() + if err != nil { + return err + } + l.Debug("reset cache") return nil } diff --git a/pkg/uhttp/wrapper.go b/pkg/uhttp/wrapper.go index 7c393f1d..377ca9e1 100644 --- a/pkg/uhttp/wrapper.go +++ b/pkg/uhttp/wrapper.go @@ -39,6 +39,22 @@ type WrapperResponse struct { StatusCode int } +// Keep a handle on all caches so we can clear them later. +var caches []GoCache + +func ClearCaches(ctx context.Context) error { + l := ctxzap.Extract(ctx) + l.Debug("clearing caches") + var err error + for _, cache := range caches { + err = cache.Clear(ctx) + if err != nil { + err = errors.Join(err, err) + } + } + return err +} + type ( HttpClient interface { HttpClient() *http.Client @@ -116,6 +132,7 @@ func NewBaseHttpClientWithContext(ctx context.Context, httpClient *http.Client) l.Error("error creating http cache", zap.Error(err)) return nil, err } + caches = append(caches, cache) return &BaseHttpClient{ HttpClient: httpClient,