Skip to content

Commit

Permalink
use cache to get/store github installation ids (#50)
Browse files Browse the repository at this point in the history
Fixes: #9

Related to #49

merge #49 before merging
this, will rebase after that gets merged
  • Loading branch information
cpanato authored Jan 25, 2024
1 parent 0ba5ef7 commit 22bd3c2
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions pkg/octosts/octosts.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/*
Copyright 2024 Chainguard, Inc.
SPDX-License-Identifier: Apache-2.0
*/

package octosts

import (
Expand All @@ -10,6 +15,7 @@ import (
"github.com/bradleyfalzon/ghinstallation/v2"
"github.com/coreos/go-oidc/v3/oidc"
"github.com/google/go-github/v57/github"
lru "github.com/hashicorp/golang-lru"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/status"
Expand All @@ -25,6 +31,11 @@ func NewSecurityTokenServiceServer(atr *ghinstallation.AppsTransport) pboidc.Sec
return &sts{atr: atr}
}

var (
// installationIDs is an LRU cache of recently used GitHub App installlations IDs.
installationIDs, _ = lru.New2Q(20 /* size */)
)

type sts struct {
pboidc.UnimplementedSecurityTokenServiceServer

Expand Down Expand Up @@ -102,7 +113,10 @@ func (s *sts) Exchange(ctx context.Context, request *pboidc.ExchangeRequest) (*p
}

func (s *sts) lookupInstall(ctx context.Context, owner string) (int64, error) {
// TODO(mattmoor): Cache these lookups.
// check the LRU cache for the installation ID
if v, ok := installationIDs.Get(owner); ok {
return v.(int64), nil
}

client := github.NewClient(&http.Client{
Transport: s.atr,
Expand All @@ -121,11 +135,15 @@ func (s *sts) lookupInstall(ctx context.Context, owner string) (int64, error) {

for _, install := range installs {
if install.Account.GetLogin() == owner {
return install.GetID(), nil
installID := install.GetID()
// store in the LRU cache
installationIDs.Add(owner, installID)
return installID, nil
}
}
page = resp.NextPage
}

return 0, status.Errorf(codes.NotFound, "no installation found for %q", owner)
}

Expand Down

0 comments on commit 22bd3c2

Please sign in to comment.