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
33
cache/credentials_cache.go
vendored
33
cache/credentials_cache.go
vendored
|
|
@ -21,6 +21,16 @@ type GithubCredentials struct {
|
|||
cache map[uint]params.GithubCredentials
|
||||
}
|
||||
|
||||
func (g *GithubCredentials) SetCredentialsRateLimit(credsID uint, rateLimit params.GithubRateLimit) {
|
||||
g.mux.Lock()
|
||||
defer g.mux.Unlock()
|
||||
|
||||
if creds, ok := g.cache[credsID]; ok {
|
||||
creds.RateLimit = rateLimit
|
||||
g.cache[credsID] = creds
|
||||
}
|
||||
}
|
||||
|
||||
func (g *GithubCredentials) SetCredentials(credentials params.GithubCredentials) {
|
||||
g.mux.Lock()
|
||||
defer g.mux.Unlock()
|
||||
|
|
@ -54,6 +64,21 @@ func (g *GithubCredentials) GetAllCredentials() []params.GithubCredentials {
|
|||
for _, cred := range g.cache {
|
||||
creds = append(creds, cred)
|
||||
}
|
||||
|
||||
// Sort the credentials by ID
|
||||
sortByID(creds)
|
||||
return creds
|
||||
}
|
||||
|
||||
func (g *GithubCredentials) GetAllCredentialsAsMap() map[uint]params.GithubCredentials {
|
||||
g.mux.Lock()
|
||||
defer g.mux.Unlock()
|
||||
|
||||
creds := make(map[uint]params.GithubCredentials, len(g.cache))
|
||||
for id, cred := range g.cache {
|
||||
creds[id] = cred
|
||||
}
|
||||
|
||||
return creds
|
||||
}
|
||||
|
||||
|
|
@ -72,3 +97,11 @@ func DeleteGithubCredentials(id uint) {
|
|||
func GetAllGithubCredentials() []params.GithubCredentials {
|
||||
return credentialsCache.GetAllCredentials()
|
||||
}
|
||||
|
||||
func SetCredentialsRateLimit(credsID uint, rateLimit params.GithubRateLimit) {
|
||||
credentialsCache.SetCredentialsRateLimit(credsID, rateLimit)
|
||||
}
|
||||
|
||||
func GetAllGithubCredentialsAsMap() map[uint]params.GithubCredentials {
|
||||
return credentialsCache.GetAllCredentialsAsMap()
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue