Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clear http caches at the start of a sync. #237

Merged
merged 1 commit into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
var caches []GoCache
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me, but I'd leave a docstring on caches explaining what it is.


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
Loading