zk client: github.com/samuel/go-zookeepernode
zookeeper是一款流行的分佈式協調組件,被普遍用於leader選舉、分佈式鎖、服務發現、名稱服務、配置中心等場景。git
zk client與zk server在創建鏈接、保持鏈接、斷開鏈接的過程當中,會經歷各類狀態。以下所示github
const ( // 暫未使用 StateUnknown State = -1 // 與zk server之間的鏈接斷開(也包含初始狀態),此時zk client會不斷重連 StateDisconnected State = 0 // 與zk server創建鏈接以前的暫時狀態,表示即將connect zk server StateConnecting State = 1 // 暫未使用 StateAuthFailed State = 4 // 暫未使用 StateConnectedReadOnly State = 5 // 暫未使用 StateSaslAuthenticated State = 6 // 在和zk server從新創建TCP鏈接以後,握手階段發現session超時 StateExpired State = -112 // 在和zk server成功創建TCP鏈接以後的狀態 StateConnected = State(100) // 和zk server成功創建TCP鏈接,而且成功握手(即成功建立session) StateHasSession = State(101) )
超時時間很大程度上影響了上述狀態的轉換,有三個超時時間值得關注:後端
func (c *Conn) setTimeouts(sessionTimeoutMs int32) { c.sessionTimeoutMs = sessionTimeoutMs sessionTimeout := time.Duration(sessionTimeoutMs) * time.Millisecond c.recvTimeout = sessionTimeout * 2 / 3 c.pingInterval = c.recvTimeout / 2 }
// Connect establishes a new connection to a pool of zookeeper // servers. The provided session timeout sets the amount of time for which // a session is considered valid after losing connection to a server. Within // the session timeout it's possible to reestablish a connection to a different // server and keep the same session. This is means any ephemeral nodes and // watches are maintained
若是client和server端鏈接發生異常,可分爲三種狀況:微信
推薦閱讀session
更多精彩內容,請掃碼關注微信公衆號:後端技術小屋。若是以爲文章對你有幫助的話,請多多分享、轉發、在看。
分佈式