示例#1
0
// a health check for zookeeper
func zkHealthCheck(halt <-chan struct{}) error {
	lastError := time.Now()
	minUptime := time.Second * 2
	zookeepers := []string{"127.0.0.1:2181"}

	for {
		if conn, _, err := zk.Connect(zookeepers, time.Second*10); err == nil {
			conn.Close()
		} else {
			conn.Close()
			glog.V(1).Infof("Could not connect to zookeeper: %s", err)
			lastError = time.Now()
		}
		// make sure that service has been good for at least minUptime
		if time.Since(lastError) > minUptime {
			break
		}

		select {
		case <-halt:
			glog.V(1).Infof("Quit healthcheck for zookeeper")
			return nil
		default:
			time.Sleep(time.Second)
		}
	}
	glog.V(1).Info("zookeeper running, browser at http://localhost:12181/exhibitor/v1/ui/index.html")
	return nil
}
示例#2
0
// GetConnection returns a Zookeeper connection given the dsn. The caller is
// responsible for closing the returned connection.
func (driver *Driver) GetConnection(dsn, basePath string) (client.Connection, error) {

	dsnVal, err := ParseDSN(dsn)
	if err != nil {
		return nil, err
	}

	conn, event, err := zklib.Connect(dsnVal.Servers, dsnVal.Timeout)
	if err != nil {
		return nil, err
	}
	<-event // wait for connected event, or anything really
	return &Connection{
		basePath: basePath,
		conn:     conn,
		servers:  dsnVal.Servers,
		timeout:  dsnVal.Timeout,
	}, nil
}