首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在恢复连接时避免双连接关闭?

如何在恢复连接时避免双连接关闭?
EN

Stack Overflow用户
提问于 2017-06-30 23:46:37
回答 1查看 1.1K关注 0票数 12

在这种情况下:

  1. 客户端失去到zk的网络连接。
  2. 一分钟过去。
  3. 客户端恢复到zk的网络连接。

我有以下恐慌:

代码语言:javascript
复制
panic: close of closed channel

goroutine 2849 [running]:
github.com/samuel/go-zookeeper/zk.(*Conn).Close(0xc420795180)
  github.com/samuel/go-zookeeper/zk/conn.go:253 47
github.com/curator-go/curator.(*handleHolder).internalClose(0xc4203058f0, 0xc420302470, 0x0)
  github.com/curator-go/curator/state.go:136 +0x8d
github.com/curator-go/curator.(*handleHolder).closeAndReset(0xc4203058f0, 0xc42587cd00, 0x1e)
  github.com/curator-go/curator/state.go:122 +0x2f
github.com/curator-go/curator.(*connectionState).reset(0xc420302420, 0x1b71d87, 0xf)
  github.com/curator-go/curator/state.go:234 +0x55
github.com/curator-go/curator.(*connectionState).handleExpiredSession(0xc420302420)
  github.com/curator-go/curator/state.go:351 +0xd9
github.com/curator-go/curator.(*connectionState).checkState(0xc420302420, 0xffffff90, 0x0, 0x0, 0xc425ed2600, 0xed0e5250a)
  github.com/curator-go/curator/state.go:318 +0x9c
github.com/curator-go/curator.(*connectionState).process(0xc420302420, 0xc425ed2680)
  github.com/curator-go/curator/state.go:299 +0x16d
created by github.com/curator-go/curator.(*Watchers).Fire
  github.com/curator-go/curator/watcher.go:64 +0x96

以下是详细的事件顺序:

  1. 客户端失去到zk的网络连接。
  2. 一分钟过去。
  3. 客户端恢复到zk的网络连接。
  4. goroutine调用s.ReregisterAll() -> Conn() -> checkTimeout() -> reset (bc 1分钟已经过去) -> closeAndReset() -> conn.Close() ,可以阻塞第二个
  5. goroutine处理zk.StateExpired (zk集群发送这个bc,它认为这个客户端是死的,因为它没有在2.) -> reset -> closeAndReset() -> conn.Close(),这引起了恐慌,因为conn.Close()已经关闭了连接的c.shouldQuit通道,而s.zooKeeper.getZookeeperConnection从来没有被goroutine A调用,因为它阻塞了第二个,所以没有新的连接。

我尝试过的一个简单的解决方案就是在reset上使用互斥,但现在我得到的helper.GetConnectionString()等于空字符串。什么是最好的方法,以避免这种崩溃,并试图进入一个良好的状态时,客户端失去,然后恢复网络连接?修复是否应该在Github.com/samuel/go-zoo牧民的实现中,不让您关闭已经关闭的连接?

(我已将这个问题提交给这里,但该项目似乎缺乏讨论,因此我要求如此。)

EN

回答 1

Stack Overflow用户

发布于 2018-02-19 20:33:34

zk.Conn有一个State()方法,它返回一个枚举"State",这是以下内容之一:

代码语言:javascript
复制
type State int32
const (
    StateUnknown           State = -1
    StateDisconnected      State = 0
    StateConnecting        State = 1
    StateAuthFailed        State = 4
    StateConnectedReadOnly State = 5
    StateSaslAuthenticated State = 6
    StateExpired           State = -112

    StateConnected  = State(100)
    StateHasSession = State(101)
)

当goroutine B调用conn.Close()时,"conn“在什么状态?

一个可能的解决方案是向goroutine添加一个开关,如果您使用的是conn.Close(),则不调用conn.StateConnecting ()。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44856087

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档