Skip to content

Commit

Permalink
Clear http caches at the start of a sync.
Browse files Browse the repository at this point in the history
  • Loading branch information
ggreer committed Oct 3, 2024
1 parent 00c9e78 commit 5ddc342
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
8 changes: 8 additions & 0 deletions pkg/connectorbuilder/connectorbuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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))
}
Expand Down
9 changes: 8 additions & 1 deletion pkg/uhttp/gocache.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,16 +158,23 @@ 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
}

err := g.rootLibrary.Reset()
if err != nil {
return err
}
err = g.rootLibrary.ResetStats()
if err != nil {
return err
}

l.Debug("reset cache")
return nil
}

Expand Down
17 changes: 17 additions & 0 deletions pkg/uhttp/wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,22 @@ type WrapperResponse struct {
StatusCode int
}

// Keep a handle on all caches so we can clear them later

Check failure on line 42 in pkg/uhttp/wrapper.go

View workflow job for this annotation

GitHub Actions / go-lint

Comment should end in a period (godot)
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
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 5ddc342

Please sign in to comment.