From 7896baa2e884af9b8a35f534125f1e9f63a79fd0 Mon Sep 17 00:00:00 2001 From: huangjunshi Date: Fri, 21 May 2021 17:20:11 +0800 Subject: [PATCH] update gcache pool --- lib/base/base_hmap.go | 5 +++++ lib/example/gcacheclient/example_gcacheclient.go | 13 +++++++------ lib/gcache/pool/gcache_pool.go | 9 ++++++++- 3 files changed, 20 insertions(+), 7 deletions(-) diff --git a/lib/base/base_hmap.go b/lib/base/base_hmap.go index fc60c31..da0cd1e 100644 --- a/lib/base/base_hmap.go +++ b/lib/base/base_hmap.go @@ -15,6 +15,11 @@ func NewHmap() *Hmap { } } +// Data : +func (h *Hmap) Data() map[string]interface{} { + return h.data +} + // Len : func (h *Hmap) Len() int { return h.size diff --git a/lib/example/gcacheclient/example_gcacheclient.go b/lib/example/gcacheclient/example_gcacheclient.go index f386e07..0fc9a2b 100644 --- a/lib/example/gcacheclient/example_gcacheclient.go +++ b/lib/example/gcacheclient/example_gcacheclient.go @@ -1,6 +1,7 @@ package examplegcacheclient import ( + "strconv" "time" "github.com/jameschz/go-base/lib/gcache" @@ -55,12 +56,12 @@ func TestDriver() { cache.Close() // test connection timeout - // for i := 0; i < 5; i++ { - // time.Sleep(10 * time.Second) - // cache = gcache.D("default") - // cache.Set("test11", "test1") - // cache.Close() - // } + for i := 0; i < 5; i++ { + time.Sleep(10 * time.Second) + cache = gcache.D("default") + cache.Set("test.timeout."+strconv.Itoa(i), "test1") + cache.Close() + } } diff --git a/lib/gcache/pool/gcache_pool.go b/lib/gcache/pool/gcache_pool.go index 5e289c3..1c890c0 100644 --- a/lib/gcache/pool/gcache_pool.go +++ b/lib/gcache/pool/gcache_pool.go @@ -52,7 +52,7 @@ func createDataSource(driver *gcachedriver.Driver, node string) *gcachebase.Data Addr: addr, DB: db, // Pool Settings - PoolSize: driver.PoolIdleMinSize, + PoolSize: driver.PoolInitSize, MinIdleConns: driver.PoolIdleMinSize, IdleTimeout: time.Duration(driver.PoolIdleTimeoutMin * int(time.Minute)), }) @@ -105,6 +105,13 @@ func Init() (err error) { _cPool.Set(cKey, createDataSource(cDriver, cNode)) } } + // heart beat + go func() { + time.Sleep(1 * time.Minute) + for _, ds := range _cPool.Data() { + ds.(*gcachebase.DataSource).RedisConn.Ping() + } + }() // for debug debugPrint("gcachepool.Init", _cPool) // init ok status