Merge branch 'fix_auth_cache' into 'master'

fix auth cache

See merge request integration-system/isp-gate-service!7
This commit is contained in:
yan komissarov committed 2020-03-04 19:12:34 +03:00
commit b8d0c8e6d0
7 files changed
+32 -22

No files matched your search

+1 -1
View File
@@ -1 +1 @@
2.5.3
2.5.4
+2
View File
@@ -1,3 +1,5 @@
## 2.5.4
* fix auth cache
## 2.5.3
* fix grpc multipart error response
## 2.5.2
+2 -2
View File
@@ -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()
+1 -1
View File
@@ -69,7 +69,7 @@
"userSecret": "secret"
},
"authCacheSetting": {
"enableCash": false,
"enableCache": false,
"evictTimeout": "1m"
}
}
+1 -1
View File
@@ -32,7 +32,7 @@ type (
}
Cache struct {
EnableCash bool `schema:"Кеширование,включает кеширования для токена приложения"`
EnableCache bool `schema:"Кеширование,включает кеширования для токена приложения"`
EvictTimeout string `schema:"Время жизни записи в кеше"`
}
+23 -15
View File
@@ -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, 4)
for key, value := range cache {
resp[key] = value
}
return resp
}
+2 -2
View File
@@ -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{},