mirror of
https://github.com/goharbor/harbor
synced 2025-04-27 11:16:03 +00:00
Add retry with backoff when creating redis cache for chart server
Signed-off-by: Steven Zou <szou@vmware.com>
This commit is contained in:
parent
b54c6b4474
commit
5ccfa9f8ad
@ -3,6 +3,7 @@ package chartserver
|
|||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
|
"math"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
beego_cache "github.com/astaxie/beego/cache"
|
beego_cache "github.com/astaxie/beego/cache"
|
||||||
@ -19,6 +20,7 @@ const (
|
|||||||
cacheDriverMem = "memory"
|
cacheDriverMem = "memory"
|
||||||
cacheDriverRedis = "redis"
|
cacheDriverRedis = "redis"
|
||||||
cacheCollectionName = "helm_chart_cache"
|
cacheCollectionName = "helm_chart_cache"
|
||||||
|
maxTry = 10
|
||||||
)
|
)
|
||||||
|
|
||||||
// ChartCache is designed to cache some processed data for repeated accessing
|
// ChartCache is designed to cache some processed data for repeated accessing
|
||||||
@ -162,15 +164,26 @@ func initCacheDriver(cacheConfig *ChartCacheConfig) beego_cache.Cache {
|
|||||||
hlog.Info("Enable memory cache for chart caching")
|
hlog.Info("Enable memory cache for chart caching")
|
||||||
return beego_cache.NewMemoryCache()
|
return beego_cache.NewMemoryCache()
|
||||||
case cacheDriverRedis:
|
case cacheDriverRedis:
|
||||||
redisCache, err := beego_cache.NewCache(cacheDriverRedis, cacheConfig.Config)
|
// New with retry
|
||||||
if err != nil {
|
count := 0
|
||||||
// Just logged
|
for {
|
||||||
hlog.Errorf("Failed to initialize redis cache: %s", err)
|
count++
|
||||||
return nil
|
redisCache, err := beego_cache.NewCache(cacheDriverRedis, cacheConfig.Config)
|
||||||
}
|
if err != nil {
|
||||||
|
// Just logged
|
||||||
|
hlog.Errorf("Failed to initialize redis cache: %s", err)
|
||||||
|
|
||||||
hlog.Info("Enable redis cache for chart caching")
|
if count < maxTry {
|
||||||
return redisCache
|
<-time.After(time.Duration(backoff(count)) * time.Second)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
hlog.Info("Enable redis cache for chart caching")
|
||||||
|
return redisCache
|
||||||
|
}
|
||||||
default:
|
default:
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
@ -179,3 +192,9 @@ func initCacheDriver(cacheConfig *ChartCacheConfig) beego_cache.Cache {
|
|||||||
hlog.Info("No cache is enabled for chart caching")
|
hlog.Info("No cache is enabled for chart caching")
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// backoff: fast->slow->fast
|
||||||
|
func backoff(count int) int {
|
||||||
|
f := 5 - math.Abs((float64)(count)-5)
|
||||||
|
return (int)(math.Pow(2, f))
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user