Skip to content

Commit

Permalink
改为短连接,防止长连接长时间不用断开不可用
Browse files Browse the repository at this point in the history
  • Loading branch information
shiguanghuxian committed Sep 29, 2019
1 parent 248ee23 commit cb6c1f8
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 13 deletions.
38 changes: 26 additions & 12 deletions program/etcdv3/etcdv3.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
8 changes: 7 additions & 1 deletion program/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 服务
Expand Down Expand Up @@ -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()
}
}
}
}

Expand Down

0 comments on commit cb6c1f8

Please sign in to comment.