Skip to content

Commit 9c5f410

Browse files
committed
Change cache to avoid memory use
Orignally, the cache was intended to be long lived to handle incoming webhooks at any time. Currently, we are just polling, and just need the cache to handle a single "EnforceAll" run, where we hit the same paths multiple times in that run. Therefore, change the cache to be per-installation, and free it after each "EnforceAll". Signed-off-by: Jeff Mendoza <[email protected]>
1 parent 24b20ac commit 9c5f410

File tree

3 files changed

+8
-10
lines changed

3 files changed

+8
-10
lines changed

pkg/enforce/enforce.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ func EnforceAll(ctx context.Context, ghc ghclients.GhClientsInterface, specificP
156156
}
157157
enforceAllResults[policyName]["totalFailed"] += results["totalFailed"]
158158
}
159+
ghc.Free(iid)
159160
mu.Unlock()
160161

161162
if err != nil {
@@ -170,7 +171,6 @@ func EnforceAll(ctx context.Context, ghc ghclients.GhClientsInterface, specificP
170171
if err := g.Wait(); err != nil {
171172
return enforceAllResults, err
172173
}
173-
ghc.LogCacheSize()
174174
log.Info().
175175
Str("area", "bot").
176176
Int("count", repoCount).

pkg/enforce/enforce_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ func (m MockGhClients) Get(i int64) (*github.Client, error) {
8989
return github.NewClient(&http.Client{}), nil
9090
}
9191

92-
func (m MockGhClients) LogCacheSize() {}
92+
func (m MockGhClients) Free(i int64) {}
9393

9494
func TestRunPolicies(t *testing.T) {
9595
policiesGetPolicies = func() []policydef.Policy {

pkg/ghclients/ghclients.go

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,14 @@ func init() {
4949

5050
type GhClientsInterface interface {
5151
Get(i int64) (*github.Client, error)
52-
LogCacheSize()
52+
Free(i int64)
5353
}
5454

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

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

76+
func (g *GHClients) Free(i int64) {
77+
delete(g.clients, i)
78+
}
79+
7880
// Get gets the client for installation id i, If i is 0 it gets the client for
7981
// the app-level api. If a stored client is not available, it creates a new
8082
// client with auth and caching built in.
@@ -85,7 +87,7 @@ func (g *GHClients) Get(i int64) (*github.Client, error) {
8587

8688
ctr := &httpcache.Transport{
8789
Transport: g.tr,
88-
Cache: g.cache,
90+
Cache: newMemoryCache(),
8991
MarkCachedResponses: true,
9092
}
9193

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

106-
func (g *GHClients) LogCacheSize() {
107-
g.cache.LogCacheSize()
108-
}
109-
110108
func getKeyFromSecretReal(ctx context.Context, keySecretVal string) ([]byte, error) {
111109
v, err := runtimevar.OpenVariable(ctx, keySecretVal)
112110
if err != nil {

0 commit comments

Comments
 (0)