Merge pull request #7872 from cd1989/config-redis-pool-idletimeout

Config idle timeout for redis pool to avoid jobservice restarting
This commit is contained in:
Wang Yan 2019-08-27 14:46:01 +08:00 committed by GitHub
commit 39f78ae768
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 39 additions and 18 deletions

View File

@ -20,6 +20,7 @@ worker_pool:
#redis://[arbitrary_username:password@]ipaddress:port/database_index
redis_url: {{redis_url}}
namespace: "harbor_job_service_namespace"
idle_timeout_second: 3600
#Loggers for the running job
job_loggers:
- name: "STD_OUTPUT" # logger backend name, only support "FILE" and "STD_OUTPUT"

View File

@ -23,6 +23,7 @@ import (
"strconv"
"strings"
"github.com/goharbor/harbor/src/common/utils/log"
"github.com/goharbor/harbor/src/jobservice/common/utils"
yaml "gopkg.in/yaml.v2"
)
@ -36,6 +37,7 @@ const (
jobServiceWorkers = "JOB_SERVICE_POOL_WORKERS"
jobServiceRedisURL = "JOB_SERVICE_POOL_REDIS_URL"
jobServiceRedisNamespace = "JOB_SERVICE_POOL_REDIS_NAMESPACE"
jobServiceRedisIdleConnTimeoutSecond = "JOB_SERVICE_POOL_REDIS_CONN_IDLE_TIMEOUT_SECOND"
jobServiceAuthSecret = "JOBSERVICE_SECRET"
coreURL = "CORE_URL"
@ -88,6 +90,10 @@ type HTTPSConfig struct {
type RedisPoolConfig struct {
RedisURL string `yaml:"redis_url"`
Namespace string `yaml:"namespace"`
// IdleTimeoutSecond closes connections after remaining idle for this duration. If the value
// is zero, then idle connections are not closed. Applications should set
// the timeout to a value less than the server's timeout.
IdleTimeoutSecond int64 `yaml:"idle_timeout_second"`
}
// PoolConfig keeps worker worker configurations.
@ -247,6 +253,19 @@ func (c *Configuration) loadEnvs() {
}
c.PoolConfig.RedisPoolCfg.Namespace = rn
}
it := utils.ReadEnv(jobServiceRedisIdleConnTimeoutSecond)
if !utils.IsEmptyStr(it) {
if c.PoolConfig.RedisPoolCfg == nil {
c.PoolConfig.RedisPoolCfg = &RedisPoolConfig{}
}
v, err := strconv.Atoi(it)
if err != nil {
log.Warningf("Invalid idle timeout second: %s, will use 0 instead", it)
} else {
c.PoolConfig.RedisPoolCfg.IdleTimeoutSecond = int64(v)
}
}
}
}

View File

@ -23,7 +23,8 @@ import (
"syscall"
"time"
"github.com/goharbor/harbor/src/pkg/scheduler"
"github.com/gomodule/redigo/redis"
"github.com/pkg/errors"
"github.com/goharbor/harbor/src/jobservice/api"
"github.com/goharbor/harbor/src/jobservice/common/utils"
@ -44,8 +45,7 @@ import (
"github.com/goharbor/harbor/src/jobservice/worker"
"github.com/goharbor/harbor/src/jobservice/worker/cworker"
"github.com/goharbor/harbor/src/pkg/retention"
"github.com/gomodule/redigo/redis"
"github.com/pkg/errors"
"github.com/goharbor/harbor/src/pkg/scheduler"
)
const (
@ -100,7 +100,7 @@ func (bs *Bootstrap) LoadAndRun(ctx context.Context, cancel context.CancelFunc)
// Add {} to namespace to void slot issue
namespace := fmt.Sprintf("{%s}", cfg.PoolConfig.RedisPoolCfg.Namespace)
// Get redis connection pool
redisPool := bs.getRedisPool(cfg.PoolConfig.RedisPoolCfg.RedisURL)
redisPool := bs.getRedisPool(cfg.PoolConfig.RedisPoolCfg)
// Do data migration if necessary
rdbMigrator := migration.New(redisPool, namespace)
@ -263,13 +263,14 @@ func (bs *Bootstrap) loadAndRunRedisWorkerPool(
}
// Get a redis connection pool
func (bs *Bootstrap) getRedisPool(redisURL string) *redis.Pool {
func (bs *Bootstrap) getRedisPool(redisPoolConfig *config.RedisPoolConfig) *redis.Pool {
return &redis.Pool{
MaxIdle: 6,
Wait: true,
IdleTimeout: time.Duration(redisPoolConfig.IdleTimeoutSecond) * time.Second,
Dial: func() (redis.Conn, error) {
return redis.DialURL(
redisURL,
redisPoolConfig.RedisURL,
redis.DialConnectTimeout(dialConnectionTimeout),
redis.DialReadTimeout(dialReadTimeout),
redis.DialWriteTimeout(dialWriteTimeout),