From cb6c1f86041054906137ffb375227c8fe55d9237 Mon Sep 17 00:00:00 2001 From: zuoxiupeng Date: Sun, 29 Sep 2019 17:58:37 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E4=B8=BA=E7=9F=AD=E8=BF=9E=E6=8E=A5?= =?UTF-8?q?=EF=BC=8C=E9=98=B2=E6=AD=A2=E9=95=BF=E8=BF=9E=E6=8E=A5=E9=95=BF?= =?UTF-8?q?=E6=97=B6=E9=97=B4=E4=B8=8D=E7=94=A8=E6=96=AD=E5=BC=80=E4=B8=8D?= =?UTF-8?q?=E5=8F=AF=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- program/etcdv3/etcdv3.go | 38 ++++++++++++++++++++++++++------------ program/http.go | 8 +++++++- 2 files changed, 33 insertions(+), 13 deletions(-) diff --git a/program/etcdv3/etcdv3.go b/program/etcdv3/etcdv3.go index 51ace68..281daf9 100644 --- a/program/etcdv3/etcdv3.go +++ b/program/etcdv3/etcdv3.go @@ -81,23 +81,37 @@ func NewEtcdCli(etcdCfg *config.EtcdServer) (*Etcd3Client, error) { }, nil } +// Close 关闭连接 +func (c *Etcd3Client) Close() error { + return c.Client.Close() +} + // GetEtcdCli 获取一个etcd cli对象 func GetEtcdCli(etcdCfg *config.EtcdServer) (*Etcd3Client, error) { if etcdCfg == nil { return nil, errors.New("etcdCfg is nil") } - val, ok := etcdClis.Load(etcdCfg.Name) - if ok == false { - if len(etcdCfg.Address) > 0 { - cli, err := NewEtcdCli(etcdCfg) - if err != nil { - return nil, err - } - return cli, nil + if len(etcdCfg.Address) > 0 { + cli, err := NewEtcdCli(etcdCfg) + if err != nil { + return nil, err } - return nil, errors.New("Getting etcd client error") + return cli, nil } - return &Etcd3Client{ - Client: val.(*clientv3.Client), - }, nil + return nil, errors.New("Getting etcd client error") + + // val, ok := etcdClis.Load(etcdCfg.Name) + // if ok == false { + // if len(etcdCfg.Address) > 0 { + // cli, err := NewEtcdCli(etcdCfg) + // if err != nil { + // return nil, err + // } + // return cli, nil + // } + // return nil, errors.New("Getting etcd client error") + // } + // return &Etcd3Client{ + // Client: val.(*clientv3.Client), + // }, nil } diff --git a/program/http.go b/program/http.go index 293fb7e..3545d52 100644 --- a/program/http.go +++ b/program/http.go @@ -12,7 +12,7 @@ import ( "github.com/shiguanghuxian/etcd-manage/program/config" "github.com/shiguanghuxian/etcd-manage/program/etcdv3" "github.com/shiguanghuxian/etcd-manage/program/logger" - "github.com/shiguanghuxian/etcd-manage/program/v1" + v1 "github.com/shiguanghuxian/etcd-manage/program/v1" ) // http 服务 @@ -130,6 +130,12 @@ func (p *Program) middlewareEtcd() gin.HandlerFunc { c.Next() + etcdCli, exists := c.Get("EtcdServer") + if exists == true { + if cli, ok := etcdCli.(*etcdv3.Etcd3Client); ok == true { + cli.Close() + } + } } }