-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #210 from /issues/204
feat(ISSUE-204): Reduce "error 429" impact
- Loading branch information
Showing
5 changed files
with
95 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package cache | ||
|
||
import ( | ||
"sync" | ||
"time" | ||
|
||
log "github.com/sirupsen/logrus" | ||
|
||
"github.com/ivanilves/lstags/api/v1/registry/client/auth" | ||
) | ||
|
||
// WaitBetween defines how much we will wait between batches of requests | ||
var WaitBetween time.Duration | ||
|
||
// Token is a structure to hold already obtained tokens | ||
// Prevents excess HTTP requests to be made (error 429) | ||
var Token = token{items: make(map[string]auth.Token)} | ||
|
||
type token struct { | ||
items map[string]auth.Token | ||
mux sync.Mutex | ||
} | ||
|
||
// Exists tells if passed key is already present in cache | ||
func (t *token) Exists(key string) bool { | ||
t.mux.Lock() | ||
defer t.mux.Unlock() | ||
|
||
_, defined := t.items[key] | ||
|
||
if !defined && WaitBetween != 0 { | ||
log.Debugf("Locking token operations for %v (key: %s)", WaitBetween, key) | ||
time.Sleep(WaitBetween) | ||
} | ||
|
||
return defined | ||
} | ||
|
||
// Get gets token for a passed key | ||
func (t *token) Get(key string) auth.Token { | ||
return t.items[key] | ||
} | ||
|
||
// Get sets token for a passed key | ||
func (t *token) Set(key string, value auth.Token) { | ||
t.mux.Lock() | ||
|
||
t.items[key] = value | ||
|
||
t.mux.Unlock() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters