Skip to content

Commit

Permalink
ok
Browse files Browse the repository at this point in the history
  • Loading branch information
smonero committed May 16, 2024
1 parent 83eb1d1 commit b9d2ff8
Showing 1 changed file with 28 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -1,54 +1,67 @@
package clientcreatorpool

Check failure on line 1 in server/neptune/temporalworker/clientcreatorpool/clientcreatorpool.go

View workflow job for this annotation

GitHub Actions / runner / golangci-lint

: # github.com/runatlantis/atlantis/server/neptune/temporalworker/clientcreatorpool [github.com/runatlantis/atlantis/server/neptune/temporalworker/clientcreatorpool.test]

import (
"fmt"

"github.com/palantir/go-githubapp/githubapp"
"github.com/pkg/errors"
ghClient "github.com/runatlantis/atlantis/server/neptune/workflows/activities/github"
"github.com/uber-go/tally/v4"
)

type ClientCreatorPool struct {
NamesToClientCreators map[string]githubapp.ClientCreator
NamesToRateLimitRemaining map[string]int
// keys are app ids aka integration ids
// Note: It would make a lot more sense to use something like a name, or the slug, right?
// The reason why that is not the case, is that then we would have to pass those around. We can't
// modify the clientCreator (it is part of the githubapp library), and you can see that inside, its
// fields are private, and there is no way to associate a given clientCreator with a name. There is
// no slug inside the githubapp.Config, only private keys and app ids, and we don't want to use
// private keys as keys.
idToClientCreator map[int]githubapp.ClientCreator
idToRateLimitRemaning map[int]int
// Note that integration id is NOT installation id. Those are 2 separate things.
}

type ClientCreatorPoolConfig struct {
name string
id int
config githubapp.Config
}

// This function is only called once, when the server starts
func (c *ClientCreatorPool) Initialize(configs []ClientCreatorPoolConfig, scope tally.Scope) error {

c.NamesToClientCreators = make(map[string]githubapp.ClientCreator)
c.NamesToRateLimitRemaining = make(map[string]int)
c.idToClientCreator = make(map[int]githubapp.ClientCreator)
c.idToRateLimitRemaning = make(map[int]int)

for _, config := range configs {
t := fmt.Sprintf("github.app.%d", config.id)
clientCreator, err := githubapp.NewDefaultCachingClientCreator(
config.config,
githubapp.WithClientMiddleware(
ghClient.ClientMetrics(scope.SubScope("app"+config.name)),
// combine the id with app
ghClient.ClientMetrics(scope.SubScope(t)),
))
if err != nil {
return errors.Wrap(err, "client creator")
}
c.NamesToClientCreators[config.name] = clientCreator
c.idToClientCreator[config.id] = clientCreator
// just needs to be non-zero, true value will be updated within 60 seconds by the cron that checks the rate limit
c.NamesToRateLimitRemaining[config.name] = 100
c.idToRateLimitRemaning[config.id] = 100
}

return nil
}

func (c *ClientCreatorPool) GetClientCreatorWithMaxRemainingRateLimit(name string) (githubapp.ClientCreator, error) {
func (c *ClientCreatorPool) GetClientCreatorWithMaxRemainingRateLimit() (githubapp.ClientCreator, error) {
maxSeenSoFar := 0
for clientName, num := range c.NamesToRateLimitRemaining {
theId := 0
for id, num := range c.idToRateLimitRemaning {
if num > maxSeenSoFar {
maxSeenSoFar = num
name = clientName
theId = id
}
}

clientCreator, ok := c.NamesToClientCreators[name]
clientCreator, ok := c.idToClientCreator[theId]
if !ok {
return nil, errors.New("client creator not found")
}
Expand All @@ -57,6 +70,6 @@ func (c *ClientCreatorPool) GetClientCreatorWithMaxRemainingRateLimit(name strin
}

// this func will be used in the crons to update the rate limit remaining
func (c *ClientCreatorPool) SetRateLimitRemaining(name string, remaining int) {
c.NamesToRateLimitRemaining[name] = remaining
func (c *ClientCreatorPool) SetRateLimitRemaining(id int, remaining int) {
c.idToRateLimitRemaning[id] = remaining
}

0 comments on commit b9d2ff8

Please sign in to comment.