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


Golang Client.CompareAndSwap方法代码示例

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


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

示例1: acquireOrRenewLease

// acquireOrRenewLease either races to acquire a new master lease, or update the existing master's lease
// returns true if we have the lease, and an error if one occurs.
// TODO: use the master election utility once it is merged in.
func (c *Config) acquireOrRenewLease(etcdClient *etcd.Client) (bool, error) {
	result, err := etcdClient.Get(c.key, false, false)
	if err != nil {
		if tools.IsEtcdNotFound(err) {
			// there is no current master, try to become master, create will fail if the key already exists
			_, err := etcdClient.Create(c.key, c.whoami, c.ttl)
			if err != nil {
				return false, err
			}
			c.lastLease = time.Now()
			return true, nil
		}
		return false, err
	}
	if result.Node.Value == c.whoami {
		glog.Infof("key already exists, we are the master (%s)", result.Node.Value)
		// we extend our lease @ 1/2 of the existing TTL, this ensures the master doesn't flap around
		if result.Node.Expiration.Sub(time.Now()) < time.Duration(c.ttl/2)*time.Second {
			_, err := etcdClient.CompareAndSwap(c.key, c.whoami, c.ttl, c.whoami, result.Node.ModifiedIndex)
			if err != nil {
				return false, err
			}
		}
		c.lastLease = time.Now()
		return true, nil
	}
	glog.Infof("key already exists, the master is %s, sleeping.", result.Node.Value)
	return false, nil
}
开发者ID:chenzhen411,项目名称:kubernetes,代码行数:32,代码来源:podmaster.go

示例2: setCommandFunc

// setCommandFunc executes the "set" command.
func setCommandFunc(cmd *cobra.Command, args []string, client *etcd.Client) (*etcd.Response, error) {
	if len(args) == 0 {
		return nil, errors.New("Key required")
	}
	key := args[0]
	value, err := argOrStdin(args, os.Stdin, 1)
	if err != nil {
		return nil, errors.New("Value required")
	}

	ttl := ttlFlag
	prevValue := swapWithValueFlag
	prevIndex := swapWithIndexFlag

	if prevValue == "" && prevIndex == 0 {
		return client.Set(key, value, uint64(ttl))
	} else {
		return client.CompareAndSwap(key, value, uint64(ttl), prevValue, uint64(prevIndex))
	}
}
开发者ID:joshi4,项目名称:etcdctl,代码行数:21,代码来源:set_command.go

示例3: CASEpoch

func CASEpoch(client *etcd.Client, appname string, prevEpoch, epoch uint64) error {
	prevEpochStr := strconv.FormatUint(prevEpoch, 10)
	epochStr := strconv.FormatUint(epoch, 10)
	_, err := client.CompareAndSwap(EpochPath(appname), epochStr, 0, prevEpochStr, 0)
	return err
}
开发者ID:bikong2,项目名称:taskgraph,代码行数:6,代码来源:epoch.go


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