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

Change cache to avoid memory use #496

Merged
merged 1 commit into from
Mar 7, 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
2 changes: 1 addition & 1 deletion pkg/enforce/enforce.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ func EnforceAll(ctx context.Context, ghc ghclients.GhClientsInterface, specificP
}
enforceAllResults[policyName]["totalFailed"] += results["totalFailed"]
}
ghc.Free(iid)
mu.Unlock()

if err != nil {
Expand All @@ -170,7 +171,6 @@ func EnforceAll(ctx context.Context, ghc ghclients.GhClientsInterface, specificP
if err := g.Wait(); err != nil {
return enforceAllResults, err
}
ghc.LogCacheSize()
log.Info().
Str("area", "bot").
Int("count", repoCount).
Expand Down
2 changes: 1 addition & 1 deletion pkg/enforce/enforce_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func (m MockGhClients) Get(i int64) (*github.Client, error) {
return github.NewClient(&http.Client{}), nil
}

func (m MockGhClients) LogCacheSize() {}
func (m MockGhClients) Free(i int64) {}

func TestRunPolicies(t *testing.T) {
policiesGetPolicies = func() []policydef.Policy {
Expand Down
14 changes: 6 additions & 8 deletions pkg/ghclients/ghclients.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,14 @@ func init() {

type GhClientsInterface interface {
Get(i int64) (*github.Client, error)
LogCacheSize()
Free(i int64)
}

// GHClients stores clients per-installation for re-use throughout a process.
type GHClients struct {
clients map[int64]*github.Client
tr http.RoundTripper
key []byte
cache *memoryCache
}

// NewGHClients returns a new GHClients. The provided RoundTripper will be
Expand All @@ -71,10 +70,13 @@ func NewGHClients(ctx context.Context, t http.RoundTripper) (*GHClients, error)
clients: make(map[int64]*github.Client),
tr: t,
key: key,
cache: newMemoryCache(),
}, nil
}

func (g *GHClients) Free(i int64) {
delete(g.clients, i)
}

// Get gets the client for installation id i, If i is 0 it gets the client for
// the app-level api. If a stored client is not available, it creates a new
// client with auth and caching built in.
Expand All @@ -85,7 +87,7 @@ func (g *GHClients) Get(i int64) (*github.Client, error) {

ctr := &httpcache.Transport{
Transport: g.tr,
Cache: g.cache,
Cache: newMemoryCache(),
MarkCachedResponses: true,
}

Expand All @@ -103,10 +105,6 @@ func (g *GHClients) Get(i int64) (*github.Client, error) {
return g.clients[i], nil
}

func (g *GHClients) LogCacheSize() {
g.cache.LogCacheSize()
}

func getKeyFromSecretReal(ctx context.Context, keySecretVal string) ([]byte, error) {
v, err := runtimevar.OpenVariable(ctx, keySecretVal)
if err != nil {
Expand Down
Loading