From 4c97100f73f88eae04bf2b936417b062714632a0 Mon Sep 17 00:00:00 2001 From: vinllen Date: Fri, 15 Mar 2019 18:04:31 +0800 Subject: [PATCH] support tcp keepalive --- conf/redis-shake.conf | 6 ++++++ src/redis-shake/common/utils.go | 5 ++++- src/redis-shake/configure/configure.go | 1 + src/redis-shake/sync.go | 2 +- 4 files changed, 12 insertions(+), 2 deletions(-) diff --git a/conf/redis-shake.conf b/conf/redis-shake.conf index 7a0eb2b..fff9078 100644 --- a/conf/redis-shake.conf +++ b/conf/redis-shake.conf @@ -126,6 +126,12 @@ sender.count = 5000 # 用于metric统计时延的队列 sender.delay_channel_size = 65535 +# enable keep_alive option in TCP when connecting redis. +# the unit is second. +# 0 means disable. +# TCP keep-alive保活参数,单位秒,0表示不启用。 +keep_alive = 0 + # ----------------splitter---------------- # below variables are useless for current opensource version so don't set. diff --git a/src/redis-shake/common/utils.go b/src/redis-shake/common/utils.go index 3be3bc9..483a6ef 100644 --- a/src/redis-shake/common/utils.go +++ b/src/redis-shake/common/utils.go @@ -34,7 +34,10 @@ func OpenRedisConnWithTimeout(target, auth_type, passwd string, readTimeout, wri } func OpenNetConn(target, auth_type, passwd string) net.Conn { - c, err := net.Dial("tcp", target) + d := net.Dialer{ + KeepAlive: time.Duration(conf.Options.KeepAlive) * time.Second, + } + c, err := d.Dial("tcp", target) if err != nil { log.PanicErrorf(err, "cannot connect to '%s'", target) } diff --git a/src/redis-shake/configure/configure.go b/src/redis-shake/configure/configure.go index ec16e8d..44437ad 100644 --- a/src/redis-shake/configure/configure.go +++ b/src/redis-shake/configure/configure.go @@ -39,6 +39,7 @@ type Configuration struct { SenderSize uint64 `config:"sender.size"` SenderCount uint `config:"sender.count"` SenderDelayChannelSize uint `config:"sender.delay_channel_size"` + KeepAlive uint `config:"keep_alive"` // inner variables ReplaceHashTag bool `config:"replace_hash_tag"` diff --git a/src/redis-shake/sync.go b/src/redis-shake/sync.go index 5c86495..6521d90 100644 --- a/src/redis-shake/sync.go +++ b/src/redis-shake/sync.go @@ -361,7 +361,7 @@ func (cmd *CmdSync) SyncCommand(reader *bufio.Reader, target, auth_type, passwd offset, err := utils.GetFakeSlaveOffset(srcConn) if err != nil { // log.PurePrintf("%s\n", NewLogItem("GetFakeSlaveOffsetFail", "WARN", NewErrorLogDetail("", err.Error()))) - log.Warnf("Event:GetFakeSlaveOffsetFail\tId:%s\tError:%s", conf.Options.Id, err.Error()) + log.Warnf("Event:GetFakeSlaveOffsetFail\tId:%s\tWarn:%s", conf.Options.Id, err.Error()) // Reconnect while network error happen if err == io.EOF {