forked from heroku/docker-registry-client
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
增加token验证缓存机制,如存在token则使用。认证失败重新获取token。
- Loading branch information
kychen
committed
May 27, 2020
1 parent
d13aff6
commit e4c7b7f
Showing
3 changed files
with
76 additions
and
6 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
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,39 @@ | ||
package registry | ||
|
||
import ( | ||
"strings" | ||
"sync" | ||
) | ||
|
||
type TokenPool struct { | ||
tokens map[string]string | ||
rwm *sync.RWMutex | ||
} | ||
|
||
func NewTokenPool() *TokenPool { | ||
return &TokenPool{ | ||
tokens: make(map[string]string), | ||
rwm: &sync.RWMutex{}, | ||
} | ||
} | ||
|
||
func (t *TokenPool) GetToken(scope string) string { | ||
if scope == "" { | ||
return "" | ||
} | ||
|
||
if _, ok := t.tokens[scope]; ok { | ||
return t.tokens[scope] | ||
} | ||
return "" | ||
} | ||
|
||
func (t *TokenPool) SetToken(scope, token string) { | ||
// repository:gds-eip/eip-api:pull | ||
if l := strings.Split(scope, ":"); len(l) == 3 { | ||
t.rwm.Lock() | ||
t.tokens[l[2]] = token | ||
t.rwm.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