當前位置: 首頁>>代碼示例>>Golang>>正文


Golang Client.Close方法代碼示例

本文整理匯總了Golang中github.com/coreos/go-etcd/etcd.Client.Close方法的典型用法代碼示例。如果您正苦於以下問題:Golang Client.Close方法的具體用法?Golang Client.Close怎麽用?Golang Client.Close使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在github.com/coreos/go-etcd/etcd.Client的用法示例。


在下文中一共展示了Client.Close方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: NewNameWatch

// NameWatch watches for specific member names to join. It can be used
// to discover when a known group of members is present.
//
//     w := condition.NewNameWatch(client, "path", "to", "watch")
//     defer w.Stop()
//
// The path must be to an etcd directory, otherwise the watch will not
// function.
//
//     for {
//         select {
//         case <- w.WatchUntil("member1", "member2"):
//             ... 2 partners are present ...
//         case err := <- w.WatchError():
//             ... error code ...
//         }
//     }
//
// Or
//     for {
//         select {
//         case names := <- w.WatchNames():
//             ... names of partners currently present ...
//         case err := <- w.WatchError():
//             ... error code ...
//         }
//     }
//
// It is not correct to select from both WatchUntil and WatchCount at
// the same time.
func NewNameWatch(e *etcd.Client, path ...string) NameWatch {
	key := strings.Join(path, "/")
	stopc := make(chan bool)
	namesc := make(chan []string, 1)
	errorc := make(chan error, 1)
	go func() {
		defer e.Close()

		var res *etcd.Response
		var err error
		var exists bool
		for !exists {
			res, err = e.Get(key, false, false)
			if err != nil {
				switch err := err.(type) {
				case *etcd.EtcdError:
					if err.ErrorCode == 100 {
						ticker := time.NewTicker(5 * time.Second)
						select {
						case <-stopc:
							ticker.Stop()
							return
						case <-ticker.C:
							ticker.Stop()
						}
					}
				default:
					errorc <- err
					return
				}
			} else {
				exists = true
			}
		}

		index := uint64(0)
		if res != nil && res.Node != nil && res.Node.Dir {
			names := make([]string, 0, len(res.Node.Nodes))
			for _, n := range res.Node.Nodes {
				names = append(names, n.Key)
			}
			select {
			case namesc <- names:
			default:
			}
			index = res.EtcdIndex
		} else {
			select {
			case namesc <- nil:
			default:
			}
		}

		watch := make(chan *etcd.Response)
		go e.Watch(key, index, true, watch, stopc)

		for {
			select {
			case <-stopc:
				return
			case res, open := <-watch:
				if !open {
					select {
					case errorc <- fmt.Errorf("name watch closed unexpectedly for: %v", key):
					default:
					}
					return
				}
				res, err := e.Get(key, false, false)
				if err != nil {
//.........這裏部分代碼省略.........
開發者ID:mdmarek,項目名稱:grid,代碼行數:101,代碼來源:condition.go

示例2: NewCountWatch

// CountWatch watches for some number of joins. It can be used to discover
// when a known number of group memebers are present.
//
//     w := condition.NewCountWatch(client, "path", "to", "watch")
//     defer w.Stop()
//
// The path must be to an etcd directory, otherwise the watch will not
// function.
//
//     for {
//         select {
//         case <- w.WatchUntil(10):
//             ... 10 partners are present ...
//         case err := <- w.WatchError():
//             ... error code ...
//         }
//     }
//
// Or
//     for {
//         select {
//         case n := <- w.WatchCount():
//             ... n partners are present ...
//         case err := <- w.WatchError():
//             ... error code ...
//         }
//     }
//
// It is not correct to select from both WatchUntil and WatchCount at
// the same time.
func NewCountWatch(e *etcd.Client, path ...string) CountWatch {
	key := strings.Join(path, "/")
	stopc := make(chan bool)
	countc := make(chan int, 1)
	errorc := make(chan error, 1)
	go func() {
		defer e.Close()

		var res *etcd.Response
		var err error
		var exists bool
		for !exists {
			res, err = e.Get(key, false, false)
			if err != nil {
				switch err := err.(type) {
				case *etcd.EtcdError:
					if err.ErrorCode == 100 {
						ticker := time.NewTicker(5 * time.Second)
						select {
						case <-stopc:
							ticker.Stop()
							return
						case <-ticker.C:
							ticker.Stop()
						}
					}
				default:
					errorc <- err
					return
				}
			} else {
				exists = true
			}
		}

		index := uint64(0)
		if res != nil && res.Node != nil && res.Node.Dir {
			select {
			case countc <- len(res.Node.Nodes):
			default:
			}
			index = res.EtcdIndex
		} else {
			select {
			case countc <- 0:
			default:
			}
		}

		watch := make(chan *etcd.Response)
		go e.Watch(key, index, true, watch, stopc)

		for {
			select {
			case <-stopc:
				return
			case res, open := <-watch:
				if !open {
					select {
					case errorc <- fmt.Errorf("count watch closed unexpectedly: %v", key):
					default:
					}
					return
				}
				res, err := e.Get(key, false, false)
				if err != nil {
					switch err := err.(type) {
					case *etcd.EtcdError:
						if err.ErrorCode == 100 {
							// Do nothing.
//.........這裏部分代碼省略.........
開發者ID:mdmarek,項目名稱:grid,代碼行數:101,代碼來源:condition.go


注:本文中的github.com/coreos/go-etcd/etcd.Client.Close方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。