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


Golang Client.TestAndSet方法代码示例

本文整理汇总了Golang中github.com/coreos/go-etcd/etcd.Client.TestAndSet方法的典型用法代码示例。如果您正苦于以下问题:Golang Client.TestAndSet方法的具体用法?Golang Client.TestAndSet怎么用?Golang Client.TestAndSet使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在github.com/coreos/go-etcd/etcd.Client的用法示例。


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

示例1: Acquire

// This will attempt to Acquire a lock for a given amount of time.
// If the lock is acquired, goChan will be unblocked
// If the lock is lost, stopChan will be unblocked
// TODO: Handle non-expected errors
func Acquire(cli *etcd.Client, lock string, timeout uint64) (goChan chan int, stopChan chan int) {
	me := uuid.NewV1().String()
	goChan = make(chan int)
	stopChan = make(chan int)

	go func() {
		log.Println("Hello, I am:", me)

		for {
			resp, acq, err := cli.TestAndSet(lock, "", me, timeout)

			if debug {
				log.Println("Lock Resp:", acq, resp, err)
			}

			if !acq {
				// We want to watch for a change in the lock, and we'll repeat
				var watcherCh = make(chan *store.Response)
				var endCh = make(chan bool)

				go cli.Watch(lock, 0, watcherCh, endCh)
				<-watcherCh

				// Now, we'll try to acquire the lock, again
			} else {

				// We got a lock, we want to keep it
				go func() {
					for {
						resp, acq, err := cli.TestAndSet(lock, me, me, timeout) // Keep the lock alive

						if debug {
							log.Println("Reset Resp:", acq, resp, err)
						}

						if !acq {

							if debug {
								log.Println("Demoted:", me)
							}
							stopChan <- 1 // Let's boot ourselves, we're no longer the leader
						}

						time.Sleep(time.Duration(timeout*500) * time.Millisecond) // We'll re-up after 50% fo the lock period
					}
				}()

				if debug {
					log.Println("King:", me)
				}
				goChan <- 1
			}
		}
	}()

	return
}
开发者ID:hayesgm,项目名称:go-etcd-lock,代码行数:61,代码来源:lock.go

示例2: lock

// A stupid spin lock
func lock(c *etcd.Client) bool {
	for {
		_, success, _ := c.TestAndSet("lock", "unlock", "lock", 0)

		if success != true {
			fmt.Println("tried lock failed!")
		} else {
			return true
		}
	}
}
开发者ID:sunatthegilddotcom,项目名称:etcd,代码行数:12,代码来源:mutex.go

示例3: inspectAndSet

func inspectAndSet(c chan string, etcdClient *etcd.Client, dockerClient *docker.Client, keyname *string, ttl *uint64) {
	for {
		id := <-c
		container, err := dockerClient.InspectContainer(id)
		if err != nil {
			log.Fatal(err)
		} else {

			var addr string

			//if the container config contains ports then set the IP as the host IP
			if len(container.Config.PortSpecs) > 0 {
				addr = localIp()
			} else {
				addr = container.NetworkSettings.IPAddress
			}

			etcdClient.TestAndSet(*keyname+"/"+container.Config.Hostname, addr, addr, *ttl)
			log.Print(container.Config.Hostname)

		}

	}
}
开发者ID:janstenpickle,项目名称:etcd-dockerhosts,代码行数:24,代码来源:etcd-docker.go


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