From b9b60f19ad5b2a32fb9ef7b04edca0cd1b09f3e1 Mon Sep 17 00:00:00 2001 From: siddontang Date: Thu, 30 Oct 2014 11:11:45 +0800 Subject: [PATCH] add conn keep alive check and bug fix --- config/config.go | 5 +++-- config/config.toml | 5 +++++ etc/ledis.conf | 5 +++++ server/client_resp.go | 8 +++++++- 4 files changed, 20 insertions(+), 3 deletions(-) diff --git a/config/config.go b/config/config.go index 3d197e3..f11c335 100644 --- a/config/config.go +++ b/config/config.go @@ -111,8 +111,9 @@ type Config struct { Snapshot SnapshotConfig `toml:"snapshot"` - ConnReadBufferSize int `toml:"conn_read_buffer_size"` - ConnWriteBufferSize int `toml:"conn_write_buffer_size"` + ConnReadBufferSize int `toml:"conn_read_buffer_size"` + ConnWriteBufferSize int `toml:"conn_write_buffer_size"` + ConnKeepaliveInterval int `toml:"conn_keepavlie_interval"` TTLCheckInterval int `toml:"ttl_check_interval"` } diff --git a/config/config.toml b/config/config.toml index fc227f4..6b6722b 100644 --- a/config/config.toml +++ b/config/config.toml @@ -44,9 +44,14 @@ db_sync_commit = 0 use_replication = false # set connection buffer, you can increase them appropriately +# more size, more memory used conn_read_buffer_size = 10240 conn_write_buffer_size = 10240 +# if connection receives no data after n seconds, it may be dead, close +# 0 to disable and not check +conn_keepavlie_interval = 0 + # checking TTL (time to live) data every n seconds # if you set big, the expired data may not be deleted immediately ttl_check_interval = 1 diff --git a/etc/ledis.conf b/etc/ledis.conf index fc227f4..6b6722b 100644 --- a/etc/ledis.conf +++ b/etc/ledis.conf @@ -44,9 +44,14 @@ db_sync_commit = 0 use_replication = false # set connection buffer, you can increase them appropriately +# more size, more memory used conn_read_buffer_size = 10240 conn_write_buffer_size = 10240 +# if connection receives no data after n seconds, it may be dead, close +# 0 to disable and not check +conn_keepavlie_interval = 0 + # checking TTL (time to live) data every n seconds # if you set big, the expired data may not be deleted immediately ttl_check_interval = 1 diff --git a/server/client_resp.go b/server/client_resp.go index 2ce4f8d..30a8f87 100644 --- a/server/client_resp.go +++ b/server/client_resp.go @@ -13,6 +13,7 @@ import ( "runtime" "strconv" "strings" + "time" ) var errReadRequest = errors.New("invalid request protocol") @@ -81,8 +82,13 @@ func (c *respClient) run() { c.app.removeSlave(c.client, handleQuit) }() + kc := time.Duration(c.app.cfg.ConnKeepaliveInterval) * time.Second done := make(chan error) for { + if kc > 0 { + c.conn.SetReadDeadline(time.Now().Add(kc)) + } + // I still don't know why use goroutine can improve performance // if someone knows and benchamrks with another different result without goroutine, please tell me go func() { @@ -91,7 +97,7 @@ func (c *respClient) run() { c.handleRequest(reqData) } - done <- nil + done <- err }() // reqData, err := c.readRequest()