func New()

in store/engine/zookeeper/zookeeper.go [61:95]


func New(id string, cfg *Config) (*Zookeeper, error) {
	if len(id) == 0 {
		return nil, errors.New("id must NOT be a empty string")
	}
	conn, _, err := zk.Connect(cfg.Addrs, sessionTTL)
	if err != nil {
		return nil, err
	}
	electPath := defaultElectPath
	if cfg.ElectPath != "" {
		electPath = cfg.ElectPath
	}
	acl := zk.WorldACL(zk.PermAll)
	if cfg.Scheme != "" && cfg.Auth != "" {
		err := conn.AddAuth(cfg.Scheme, []byte(cfg.Auth))
		if err == nil {
			acl = []zk.ACL{{Perms: zk.PermAll, Scheme: cfg.Scheme, ID: cfg.Auth}}
		} else {
			logger.Get().Warn("Zookeeper addAuth fail: " + err.Error())
		}
	}
	e := &Zookeeper{
		myID:           id,
		acl:            acl,
		electPath:      electPath,
		conn:           conn,
		quitCh:         make(chan struct{}),
		leaderChangeCh: make(chan bool),
		wg:             sync.WaitGroup{},
	}
	e.isReady.Store(false)
	e.wg.Add(1)
	go e.electLoop(context.Background())
	return e, nil
}