当前位置: 首页>>代码示例>>Golang>>正文


Golang zookeeper.Dial函数代码示例

本文整理汇总了Golang中launchpad/net/gozk/zookeeper.Dial函数的典型用法代码示例。如果您正苦于以下问题:Golang Dial函数的具体用法?Golang Dial怎么用?Golang Dial使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了Dial函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: InitWatch

// InitWatch initialize watch module
func InitWatch() error {
	// Initialize zookeeper connection
	Log.Info("Initializing zookeeper,zookeeper.Dial(\"%s\", \"%dms\")", Conf.ZKAddr, Conf.ZKTimeout/1000000)
	zkTmp, session, err := zookeeper.Dial(Conf.ZKAddr, Conf.ZKTimeout)
	if err != nil {
		return err
	}
	zk = zkTmp

	// Zookeeper client will reconnect automatically
	for {
		event := <-session
		if event.State < zookeeper.STATE_CONNECTING {
			return errors.New(fmt.Sprintf("connect zookeeper fail, event:\"%v\"", event))
		} else if event.State == zookeeper.STATE_CONNECTING {
			Log.Warn("Zookeeper connecting!")
			time.Sleep(time.Second)
			continue
		} else {
			break
		}
	}

	Log.Info("Initialize zookeeper OK")

	// Zookeeper create Public message subnode
	if err := zkCreate(); err != nil {
		return err
	}

	// Init public message mid-creater
	PubMID = timeID.NewTimeID()

	return nil
}
开发者ID:newjueqi,项目名称:gopush-cluster,代码行数:36,代码来源:watch.go

示例2: MakeZookeeperClient

func MakeZookeeperClient(server_host string, debug bool) (*ZookeeperClient, error) {
	client := &ZookeeperClient{
		debug: debug,
	}
	if server_host == "" {
		client.server_host = "localhost:2181"
	} else {
		client.server_host = server_host
	}

	conn, session, err := zookeeper.Dial(client.server_host, 5*time.Second)
	if err != nil {
		fmt.Printf("Couldn't connect: %s\n", err)
		return nil, err
	}

	client.client = conn
	client.session = session

	// Wait for connection.
	event := <-client.session
	fmt.Printf("Got event\n")
	if event.State != zookeeper.STATE_CONNECTED {
		fmt.Printf("Error with connect, %s!\n", event.State)
		return nil, errors.New("Error with connect")
	}

	return client, nil
}
开发者ID:mannesma,项目名称:bench,代码行数:29,代码来源:zookeeper.go

示例3: DialZkTimeout

func DialZkTimeout(zkAddr string, baseTimeout time.Duration, connectTimeout time.Duration) (*ZkConn, <-chan zookeeper.Event, error) {
	resolvedZkAddr, err := resolveZkAddr(zkAddr)
	if err != nil {
		return nil, nil, err
	}

	sem.Acquire()
	defer sem.Release()
	zconn, session, err := zookeeper.Dial(resolvedZkAddr, baseTimeout)
	if err == nil {
		// Wait for connection, with a timeout
		timer := time.NewTimer(connectTimeout)
		select {
		case <-timer.C:
			err = fmt.Errorf("zk connect timed out")
		case event := <-session:
			if event.State != zookeeper.STATE_CONNECTED {
				err = fmt.Errorf("zk connect failed: %v", event.State)
			}
		}

		if err == nil {
			return &ZkConn{conn: zconn}, session, nil
		} else {
			zconn.Close()
		}
	}
	return nil, nil, err
}
开发者ID:henryanand,项目名称:vitess,代码行数:29,代码来源:zkconn.go

示例4: Initzk

func Initzk() {
	ipport := utils.Cfg.GetString("zkServer")
	zk1, session, err := zookeeper.Dial(ipport, 5e9)
	zk = zk1
	if err != nil {
		//		log.Fatalf("Can't connect: %v", err)
		fmt.Println(err)
	}
	//	defer zk.Close()

	// Wait for connection.
	event := <-session
	if event.State != zookeeper.STATE_CONNECTED {
		//		log.Fatalf("Can't connect: %v", event)
		fmt.Println(event.String())
	}
	fmt.Println(event)

	_, err = zk.Create(utils.Cfg.GetString("sendslavers"), "0", 0, zookeeper.WorldACL(zookeeper.PERM_ALL)) //zookeeper.EPHEMERAL|zookeeper.SEQUENCE

	if err != nil {
		//		log.Fatalf("Can't create counter: %v", err)
		fmt.Println("Can't create counter: %v", err)
	} else {
		fmt.Println("Counter created!")
	}
	watchslavers()
}
开发者ID:houzhenggang,项目名称:ergate,代码行数:28,代码来源:zkutils.go

示例5: Dial

func Dial(serverAddrs []string, recvTimeout time.Duration) (*GlobalConn, <-chan zookeeper.Event, error) {
	zconns := make([]*zookeeper.Conn, len(serverAddrs))
	zchans := make([]<-chan zookeeper.Event, len(serverAddrs))
	for i, addr := range serverAddrs {
		conn, eventChan, err := zookeeper.Dial(addr, recvTimeout)
		if err != nil {
			// teardown
			for j := 0; j < i; j++ {
				zconns[j].Close()
			}
			return nil, nil, err
		}
		zconns[i] = conn
		zchans[i] = eventChan
	}

	eventChan := make(chan zookeeper.Event, 1)
	go func() {
		var e zookeeper.Event
		for _, c := range zchans {
			e = <-c
		}
		eventChan <- e
		close(eventChan)
	}()
	return &GlobalConn{serverAddrs, zconns, DEFAULT_MAX_RETRIES}, eventChan, nil
}
开发者ID:henryanand,项目名称:vitess,代码行数:27,代码来源:global.go

示例6: init

func (zkd *Zkd) init(preserveData bool) error {
	log.Infof("zkd.Init")
	for _, path := range zkd.config.DirectoryList() {
		if err := os.MkdirAll(path, 0775); err != nil {
			log.Errorf(err.Error())
			return err
		}
		// FIXME(msolomon) validate permissions?
	}

	configData, err := zkd.makeCfg()
	if err == nil {
		err = ioutil.WriteFile(zkd.config.ConfigFile(), []byte(configData), 0664)
	}
	if err != nil {
		log.Errorf("failed creating %v: %v", zkd.config.ConfigFile(), err)
		return err
	}

	err = zkd.config.WriteMyid()
	if err != nil {
		log.Errorf("failed creating %v: %v", zkd.config.MyidFile(), err)
		return err
	}

	if err = zkd.Start(); err != nil {
		log.Errorf("failed starting, check %v", zkd.config.LogDir())
		return err
	}

	zkAddr := fmt.Sprintf("localhost:%v", zkd.config.ClientPort)
	zk, session, err := zookeeper.Dial(zkAddr, StartWaitTime*time.Second)
	if err != nil {
		return err
	}
	event := <-session
	if event.State != zookeeper.STATE_CONNECTED {
		return err
	}
	defer zk.Close()

	if !preserveData {
		_, err = zk.Create("/zk", "", 0, zookeeper.WorldACL(zookeeper.PERM_ALL))
		if err != nil && !zookeeper.IsError(err, zookeeper.ZNODEEXISTS) {
			return err
		}

		if zkd.config.Global {
			_, err = zk.Create("/zk/global", "", 0, zookeeper.WorldACL(zookeeper.PERM_ALL))
			if err != nil && !zookeeper.IsError(err, zookeeper.ZNODEEXISTS) {
				return err
			}
		}
	}

	return nil
}
开发者ID:littleyang,项目名称:vitess,代码行数:57,代码来源:zkctl.go

示例7: Join

// Join joins the cluster *c is configured for.
func (c *Cluster) Join() error {
	if c.config.NodeId == "" {
		return errors.New("config requires a NodeId")
	}
	if len(c.config.Servers) == 0 {
		return errors.New("config requires Servers")
	}

	servers := strings.Join(c.config.Servers, ",")

	// log.Println("Join...")
	switch atomic.LoadInt32(&c.state) {
	case NewState /*, ShutdownState */ :
		zk, session, err := zookeeper.Dial(servers, c.config.Timeout)
		if err != nil {
			return err
		}
		ev := <-session
		if ev.State != zookeeper.STATE_CONNECTED {
			return errors.New("Failed to connect to Zookeeper")
		}
		log.Printf("Node %s connected to ZooKeeper", c.config.NodeId)
		c.zk, c.zkSession = zkutil.NewZooKeeper(zk), session
		c.createPaths()
		c.joinCluster()
		c.listener.OnJoin(c.zk)
		c.setupWatchers()
		if !atomic.CompareAndSwapInt32(&c.state, NewState, StartedState) {
			log.Fatalf("Could not move from NewState to StartedState: State is not NewState")
		}
	case StartedState, DrainingState:
		return errors.New("Tried to join with state StartedState or DrainingState")
	case ShutdownState:
		// TODO
	default:
		panic("Unknown state")
	}

	c.balancer.Init(c)
	go func() {
		c.rebalanceKill = make(chan byte)
		for {
			select {
			case <-c.rebalanceKill:
				return
			case <-time.After(c.config.RebalanceInterval):
				c.rebalance()
			}
		}
	}()
	c.getTasks()
	return nil
}
开发者ID:rdterner,项目名称:donut,代码行数:54,代码来源:cluster.go

示例8: connectToZK

func (c *Ctl) connectToZK() error {
	servers := strings.Join(c.cfg.Servers, ",")
	zk, zkEv, err := zookeeper.Dial(servers, c.cfg.Timeout)
	if err != nil {
		return err
	}
	ev := <-zkEv
	if ev.State != zookeeper.STATE_CONNECTED {
		errors.New("Failed to connect to Zookeeper servers: " + servers)
	}
	c.zk = zk
	return nil
}
开发者ID:rdterner,项目名称:donut,代码行数:13,代码来源:donutctl.go

示例9: NewLockServer

func NewLockServer(endpoints string) (*LockServer, error) {
	zk, session, err := zookeeper.Dial(endpoints, 5e9)
	if err != nil {
		log.Println("Can't connect: %v", err)
		return nil, err
	}

	select {
	case event := <-session:
		log.Println(event)
	case <-time.After(time.Second * 5):
		return nil, errors.New("Connection timeout")
	}
	return &LockServer{zk, session, make(chan bool)}, nil
}
开发者ID:kartvep,项目名称:Combaine,代码行数:15,代码来源:lockserver.go

示例10: DialZk

// Dial a ZK server and waits for connection event. Returns a ZkConn
// encapsulating the zookeeper.Conn, and the zookeeper session event
// channel to monitor the connection
//
// The value for baseTimeout is used as a session timeout as well, and
// will be used to negotiate a 'good' value with the server. From
// reading the zookeeper source code, it has to be between 6 and 60
// seconds (2x and 20x the tickTime by default, with default tick time
// being 3 seconds).  min session time, max session time and ticktime
// can all be overwritten on the zookeeper server side, so these
// numbers may vary.
//
// Then this baseTimeout is used to compute other related timeouts:
// - connect timeout is 1/3 of baseTimeout
// - recv timeout is 2/3 of baseTimeout minus a ping time
// - send timeout is 1/3 of baseTimeout
// - we try to send a ping a least every baseTimeout / 3
//
// Note the baseTimeout has *nothing* to do with the time between we
// call Dial and the maximum time before we receive the event on the
// session. The library will actually try to re-connect in the background
// (after each timeout), and may *never* send an event if the TCP connections
// always fail. Use DialZkTimeout to enforce a timeout for the initial connect.
func DialZk(zkAddr string, baseTimeout time.Duration) (*ZkConn, <-chan zookeeper.Event, error) {
	sem.Acquire()
	defer sem.Release()
	zconn, session, err := zookeeper.Dial(zkAddr, baseTimeout)
	if err == nil {
		// Wait for connection, possibly forever
		event := <-session
		if event.State != zookeeper.STATE_CONNECTED {
			err = fmt.Errorf("zk connect failed: %v", event.State)
		}
		if err == nil {
			return &ZkConn{zconn}, session, nil
		} else {
			zconn.Close()
		}
	}
	return nil, nil, err
}
开发者ID:shrutip,项目名称:vitess,代码行数:41,代码来源:zkconn.go

示例11: TestInitErrorThroughErrno

// This error will be delivered via C errno, since ZK unfortunately
// only provides the handler back from zookeeper_init().
func (s *S) TestInitErrorThroughErrno(c *C) {
	conn, watch, err := zk.Dial("bad-domain-without-port", 5e9)
	if conn != nil {
		conn.Close()
	}
	if watch != nil {
		go func() {
			for {
				_, ok := <-watch
				if !ok {
					break
				}
			}
		}()
	}
	c.Assert(conn, IsNil)
	c.Assert(watch, IsNil)
	c.Assert(err, ErrorMatches, "zookeeper: dial: invalid argument")
}
开发者ID:CowLeo,项目名称:vitess,代码行数:21,代码来源:zk_test.go

示例12: main

func main() {
	zk, session, err := zookeeper.Dial("localhost:2181", 5e9)
	if err != nil {
		log.Fatalf("Can't connect: %v", err)
	}
	defer zk.Close()

	// Wait for connection.
	event := <-session
	if event.State != zookeeper.STATE_CONNECTED {
		log.Fatalf("Can't connect: %v", event)
	}

	_, err = zk.Create("/counter", "0", 0, zookeeper.WorldACL(zookeeper.PERM_ALL))
	if err != nil {
		log.Fatalf("Can't create counter: %v", err)
	} else {
		fmt.Println("Counter created!")
	}
}
开发者ID:yoCruzer,项目名称:go-snippets,代码行数:20,代码来源:main.go

示例13: newZookeeper

// newZookeeper dial zookeeper cluster.
func newZookeeper() (*ZK, error) {
	zk, session, err := zookeeper.Dial(Conf.ZookeeperAddr, Conf.ZookeeperTimeout)
	if err != nil {
		Log.Error("zookeeper.Dial(\"%s\", %d) error(%v)", Conf.ZookeeperAddr, Conf.ZookeeperTimeout, err)
		return nil, err
	}
	go func() {
		for {
			event := <-session
			if event.State < zookeeper.STATE_CONNECTING {
				Log.Error("can't connect zookeeper, event: %v", event)
			} else if event.State == zookeeper.STATE_CONNECTING {
				Log.Warn("retry connect zookeeper, event: %v", event)
			} else {
				Log.Debug("succeed connect zookeeper, event: %v", event)
			}
		}
	}()
	return &ZK{conn: zk}, nil
}
开发者ID:Brant-Zhang,项目名称:gopush-cluster,代码行数:21,代码来源:zk.go

示例14: NewZookeeper

func NewZookeeper(addr string, timeout int) (*ZK, error) {
	zk, session, err := zookeeper.Dial(addr, time.Duration(timeout)*1e9)
	if err != nil {
		return nil, err
	}

	go func() {
		for {
			event := <-session
			if event.State < zookeeper.STATE_CONNECTING {
				Log.Error("can't connect zookeeper, event: %v", event)
			} else if event.State == zookeeper.STATE_CONNECTING {
				Log.Warn("retry connect zookeeper, event: %v", event)
			} else {
				Log.Debug("succeed connect zookeeper, event: %v", event)
			}
		}
	}()

	return &ZK{conn: zk}, nil
}
开发者ID:Ehuos,项目名称:gopush-cluster,代码行数:21,代码来源:zk.go

示例15: TestRecvTimeoutInitParameter

func (s *S) TestRecvTimeoutInitParameter(c *C) {
	conn, watch, err := zk.Dial(s.zkAddr, 0)
	c.Assert(err, IsNil)
	defer conn.Close()

	select {
	case <-watch:
		c.Fatal("Watch fired")
	default:
	}

	for i := 0; i != 1000; i++ {
		_, _, err := conn.Get("/zookeeper")
		if err != nil {
			c.Check(zk.IsError(err, zk.ZOPERATIONTIMEOUT), Equals, true, Commentf("%v", err))
			c.SucceedNow()
		}
	}

	c.Fatal("Operation didn't timeout")
}
开发者ID:CowLeo,项目名称:vitess,代码行数:21,代码来源:zk_test.go


注:本文中的launchpad/net/gozk/zookeeper.Dial函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。