func InitZK() (*zk.Conn, error) { config := GetConfig() if config == nil { log.Fatal("get config error") } conn, err := myzk.Connect(config.Zk.ZookeeperAddr, config.Zk.ZookeeperTimeout) if err != nil { log.Error("myzk.Connect() error(%v)", err) return nil, err } fpath := path.Join(ZOOKEEPER_GATE_PATH, config.Zk.ZookeeperNode) err := myzk.DeleteNode(conn, fpath) if err != nil { log.Error("myzk delete node error:%v", err) } data := []byte{} log.Debug("myzk node:\"%s\" registe data: \"%s\"", fpath, string(data)) if err = myzk.RegisterTemp(conn, fpath, data); err != nil { log.Error("myzk.RegisterTemp() error(%v)", err) return conn, err } go WatchConnChange(conn) return conn, nil }
func InitZK() (*zk.Conn, error) { config := GetConfig() if config == nil { log.Fatal("get config error") } conn, err := myzk.Connect(config.Zk.ZookeeperAddr, config.Zk.ZookeeperTimeout) if err != nil { log.Error("myzk.Connect() error(%v)", err) return nil, err } fpath := path.Join(ZOOKEEPER_CONN_PATH, config.Zk.ZookeeperNode) nodeInfo := ConnectorConf{ Hostname: config.Zk.ZookeeperNode, ServeAddr: config.ServeAddr, RpcAddr: config.RpcAddr, } data, err := json.Marshal(nodeInfo) if err != nil { log.Error("json.Marshal() error(%v)", err) return conn, err } log.Debug("myzk node:\"%s\" registe data: \"%s\"", fpath, string(data)) err = myzk.DeleteNode(conn, fpath) if err != nil { log.Error("myzk delete node error:%v", err) } if err = myzk.RegisterTemp(conn, fpath, data); err != nil { log.Error("myzk.RegisterTemp() error(%v)", err) return conn, err } return conn, nil }
// RegisterTmp create a ephemeral node, and watch it, if node droped then send a SIGQUIT to self. func RegisterTemp(conn *zk.Conn, fpath string, data []byte) error { tpath, err := conn.Create(path.Join(fpath), data, zk.FlagEphemeral, zk.WorldACL(zk.PermAll)) if err != nil { log.Error("conn.Create(\"%s\", \"%s\", zk.FlagEphemeral) error(%v)", fpath, string(data), err) return err } log.Debug("create a zookeeper node:%s", tpath) return nil }
func ZkDeleteRoomConn(conn *zk.Conn, roomid string) error { config := GetConfig() fpath := path.Join(ZOOKEEPER_ROOM_PATH, roomid, config.RpcAddr) log.Debug("myzk delete znode:%s", fpath) err := myzk.DeleteNode(conn, fpath) if err != nil { log.Error("myzk delete node error:%v", err) } return err }
func ZkAddRoomConn(conn *zk.Conn, roomid string) error { config := GetConfig() fpath := path.Join(ZOOKEEPER_ROOM_PATH, roomid, config.RpcAddr) data := []byte(config.RpcAddr) log.Debug("myzk node:\"%s\" registe data: \"%s\"", fpath, string(data)) err := myzk.RegisterTemp(conn, fpath, data) if err != nil { log.Error("myzk.RegisterTemp() error(%v)", err) } return err }
// Create create zookeeper path, if path exists ignore error func Create(conn *zk.Conn, fpath string) error { // create zk root path tpath := "" for _, str := range strings.Split(fpath, "/")[1:] { tpath = path.Join(tpath, "/", str) log.Debug("create zookeeper path: \"%s\"", tpath) _, err := conn.Create(tpath, []byte(""), 0, zk.WorldACL(zk.PermAll)) if err != nil { if err == zk.ErrNodeExists { log.Warning("zk.create(\"%s\") exists", tpath) } else { log.Error("zk.create(\"%s\") error(%v)", tpath, err) return err } } } return nil }
// Connect connect to zookeeper, and start a goroutine log the event. func Connect(addr []string, timeout string) (*zk.Conn, error) { timeoutDuration, err := time.ParseDuration(timeout) if err != nil { log.Error("zk timeout error:%v", err) return nil, err } conn, session, err := zk.Connect(addr, timeoutDuration) if err != nil { log.Error("zk.Connect(\"%v\", %d) error(%v)", addr, timeoutDuration, err) return nil, err } go func() { for { event := <-session log.Debug("zookeeper get a event: %s", event.State.String()) } }() return conn, nil }
func DeleteNode(conn *zk.Conn, fpath string) error { log.Debug("delete znode %s", fpath) err := conn.Delete(fpath, -1) return err }