fix auth cache
This commit is contained in:
1 parent
7e10fb5217
commit
675a917c0c
7 files changed
+32
-22
No files matched your search
@@ -1,3 +1,5 @@
|
||||
## 2.5.4
|
||||
* fix auth cache
|
||||
## 2.5.3
|
||||
* fix grpc multipart error response
|
||||
## 2.5.2
|
||||
|
||||
@@ -37,11 +37,11 @@ type authenticate struct {
|
||||
}
|
||||
|
||||
func ReceiveConfiguration(conf conf.Cache) {
|
||||
if conf.EnableCash {
|
||||
if conf.EnableCache {
|
||||
if timeout, err := time.ParseDuration(conf.EvictTimeout); err != nil {
|
||||
log.Fatalf(stdcodes.ModuleInvalidRemoteConfig, "invalid timeout '%s'", timeout)
|
||||
} else {
|
||||
auth.verify = veritification.NewCacheableVerify(timeout)
|
||||
auth.verify = veritification.NewCacheablesVerify(timeout)
|
||||
}
|
||||
} else {
|
||||
auth.verify = veritification.NewRuntimeVerify()
|
||||
|
||||
@@ -69,7 +69,7 @@
|
||||
"userSecret": "secret"
|
||||
},
|
||||
"authCacheSetting": {
|
||||
"enableCash": false,
|
||||
"enableCache": false,
|
||||
"evictTimeout": "1m"
|
||||
}
|
||||
}
|
||||
@@ -32,7 +32,7 @@ type (
|
||||
}
|
||||
|
||||
Cache struct {
|
||||
EnableCash bool `schema:"Кеширование,включает кеширования для токена приложения"`
|
||||
EnableCache bool `schema:"Кеширование,включает кеширования для токена приложения"`
|
||||
EvictTimeout string `schema:"Время жизни записи в кеше"`
|
||||
}
|
||||
|
||||
|
||||
@@ -6,7 +6,7 @@ import (
|
||||
)
|
||||
|
||||
type (
|
||||
cacheableVerify struct {
|
||||
cacheablesVerify struct {
|
||||
cache map[string]cacheInfo
|
||||
timeout time.Duration
|
||||
rv *runtimeVerify
|
||||
@@ -19,34 +19,42 @@ type (
|
||||
}
|
||||
)
|
||||
|
||||
func (cv *cacheableVerify) ApplicationToken(token string) (map[string]string, error) {
|
||||
func (cv *cacheablesVerify) ApplicationToken(token string) (map[string]string, error) {
|
||||
cv.lock.RLock()
|
||||
cache, ok := cv.cache[token]
|
||||
cv.lock.RUnlock()
|
||||
if ok {
|
||||
if time.Now().Sub(cache.created) > cv.timeout {
|
||||
return cache.identity, nil
|
||||
if time.Now().Sub(cache.created) < cv.timeout {
|
||||
return cv.copyCache(cache.identity), nil
|
||||
}
|
||||
}
|
||||
|
||||
cv.lock.Lock()
|
||||
defer cv.lock.Unlock()
|
||||
if cache, ok = cv.cache[token]; ok {
|
||||
if time.Now().Sub(cache.created) > cv.timeout {
|
||||
return cache.identity, nil
|
||||
} else {
|
||||
delete(cv.cache, token)
|
||||
cache, ok = cv.cache[token]
|
||||
if ok {
|
||||
if time.Now().Sub(cache.created) < cv.timeout {
|
||||
return cv.copyCache(cache.identity), nil
|
||||
}
|
||||
delete(cv.cache, token)
|
||||
}
|
||||
if identity, err := cv.rv.ApplicationToken(token); err != nil {
|
||||
|
||||
identity, err := cv.rv.ApplicationToken(token)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
cv.cache[token] = cacheInfo{created: time.Now(), identity: identity}
|
||||
resp := identity
|
||||
return resp, nil
|
||||
}
|
||||
cv.cache[token] = cacheInfo{created: time.Now(), identity: identity}
|
||||
return cv.copyCache(identity), nil
|
||||
}
|
||||
|
||||
func (cv *cacheableVerify) Identity(identity map[string]string, uri string) (map[string]string, error) {
|
||||
func (cv *cacheablesVerify) Identity(identity map[string]string, uri string) (map[string]string, error) {
|
||||
return cv.rv.Identity(identity, uri)
|
||||
}
|
||||
|
||||
func (cv *cacheablesVerify) copyCache(cache map[string]string) map[string]string {
|
||||
resp := make(map[string]string)
|
||||
for key, value := range cache {
|
||||
resp[key] = value
|
||||
}
|
||||
return resp
|
||||
}
|
||||
@@ -15,8 +15,8 @@ func NewRuntimeVerify() Verify {
|
||||
return &runtimeVerify{}
|
||||
}
|
||||
|
||||
func NewCacheableVerify(timeout time.Duration) Verify {
|
||||
return &cacheableVerify{
|
||||
func NewCacheablesVerify(timeout time.Duration) Verify {
|
||||
return &cacheablesVerify{
|
||||
cache: make(map[string]cacheInfo),
|
||||
timeout: timeout,
|
||||
rv: &runtimeVerify{},
|
||||
|
||||
Reference in new issue
Block a user