Merge pull request #6379 from steven-zou/add_retry_to_connect_redis

Add retry with backoff when creating redis cache for chart server
This commit is contained in:
Wenkai Yin 2018-11-29 13:52:43 +08:00 committed by GitHub
commit 3ff4402a35
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -3,6 +3,7 @@ package chartserver
import (
"encoding/json"
"errors"
"math"
"time"
beego_cache "github.com/astaxie/beego/cache"
@ -19,6 +20,7 @@ const (
cacheDriverMem = "memory"
cacheDriverRedis = "redis"
cacheCollectionName = "helm_chart_cache"
maxTry = 10
)
// 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")
return beego_cache.NewMemoryCache()
case cacheDriverRedis:
redisCache, err := beego_cache.NewCache(cacheDriverRedis, cacheConfig.Config)
if err != nil {
// Just logged
hlog.Errorf("Failed to initialize redis cache: %s", err)
return nil
}
// New with retry
count := 0
for {
count++
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")
return redisCache
if count < maxTry {
<-time.After(time.Duration(backoff(count)) * time.Second)
continue
}
return nil
}
hlog.Info("Enable redis cache for chart caching")
return redisCache
}
default:
break
}
@ -179,3 +192,9 @@ func initCacheDriver(cacheConfig *ChartCacheConfig) beego_cache.Cache {
hlog.Info("No cache is enabled for chart caching")
return nil
}
// backoff: fast->slow->fast
func backoff(count int) int {
f := 5 - math.Abs((float64)(count)-5)
return (int)(math.Pow(2, f))
}