Add rate limit cache and fixes
This change adds a loop that keeps a cache of credentials rate limits as reported by the github API. The cache is updated every 30 seconds and is purely informational for the user. This change also adds some caching improvements. Functions that return values from the cache as lists, will now sort by ID or creation date. Signed-off-by: Gabriel Adrian Samfira <gsamfira@cloudbasesolutions.com>
This commit is contained in:
parent
16af8fd97f
commit
1a719567ff
10 changed files with 276 additions and 23 deletions
|
|
@ -7,6 +7,7 @@ import (
|
|||
|
||||
runnerErrors "github.com/cloudbase/garm-provider-common/errors"
|
||||
"github.com/cloudbase/garm/auth"
|
||||
"github.com/cloudbase/garm/cache"
|
||||
"github.com/cloudbase/garm/params"
|
||||
)
|
||||
|
||||
|
|
@ -15,11 +16,24 @@ func (r *Runner) ListCredentials(ctx context.Context) ([]params.GithubCredential
|
|||
return nil, runnerErrors.ErrUnauthorized
|
||||
}
|
||||
|
||||
// Get the credentials from the store. The cache is always updated after the database successfully
|
||||
// commits the transaction that created/updated the credentials.
|
||||
// If we create a set of credentials then immediately after we call ListCredentials,
|
||||
// there is a posibillity that not all creds will be in the cache.
|
||||
creds, err := r.store.ListGithubCredentials(ctx)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "fetching github credentials")
|
||||
}
|
||||
|
||||
// If we do have cache, update the rate limit for each credential. The rate limits are queried
|
||||
// every 30 seconds and set in cache.
|
||||
credsCache := cache.GetAllGithubCredentialsAsMap()
|
||||
for idx, cred := range creds {
|
||||
inCache, ok := credsCache[cred.ID]
|
||||
if ok {
|
||||
creds[idx].RateLimit = inCache.RateLimit
|
||||
}
|
||||
}
|
||||
return creds, nil
|
||||
}
|
||||
|
||||
|
|
@ -50,6 +64,11 @@ func (r *Runner) GetGithubCredentials(ctx context.Context, id uint) (params.Gith
|
|||
return params.GithubCredentials{}, errors.Wrap(err, "failed to get github credentials")
|
||||
}
|
||||
|
||||
cached, ok := cache.GetGithubCredentials((creds.ID))
|
||||
if ok {
|
||||
creds.RateLimit = cached.RateLimit
|
||||
}
|
||||
|
||||
return creds, nil
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue