本文整理匯總了Golang中github.com/coreos/etcd/clientv3.Client.Put方法的典型用法代碼示例。如果您正苦於以下問題:Golang Client.Put方法的具體用法?Golang Client.Put怎麽用?Golang Client.Put使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類github.com/coreos/etcd/clientv3.Client
的用法示例。
在下文中一共展示了Client.Put方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: makeMirror
func makeMirror(ctx context.Context, c *clientv3.Client, dc *clientv3.Client) error {
total := int64(0)
go func() {
for {
time.Sleep(30 * time.Second)
fmt.Println(atomic.LoadInt64(&total))
}
}()
// TODO: remove the prefix of the destination cluster?
s := mirror.NewSyncer(c, mmprefix, 0)
rc, errc := s.SyncBase(ctx)
for r := range rc {
for _, kv := range r.Kvs {
_, err := dc.Put(ctx, string(kv.Key), string(kv.Value))
if err != nil {
return err
}
atomic.AddInt64(&total, 1)
}
}
err := <-errc
if err != nil {
return err
}
wc := s.SyncUpdates(ctx)
for wr := range wc {
if wr.CompactRevision != 0 {
return rpctypes.ErrCompacted
}
var rev int64
ops := []clientv3.Op{}
for _, ev := range wr.Events {
nrev := ev.Kv.ModRevision
if rev != 0 && nrev > rev {
_, err := dc.Txn(ctx).Then(ops...).Commit()
if err != nil {
return err
}
ops = []clientv3.Op{}
}
switch ev.Type {
case storagepb.PUT:
ops = append(ops, clientv3.OpPut(string(ev.Kv.Key), string(ev.Kv.Value)))
atomic.AddInt64(&total, 1)
case storagepb.DELETE, storagepb.EXPIRE:
ops = append(ops, clientv3.OpDelete(string(ev.Kv.Key)))
atomic.AddInt64(&total, 1)
default:
panic("unexpected event type")
}
}
if len(ops) != 0 {
_, err := dc.Txn(ctx).Then(ops...).Commit()
if err != nil {
return err
}
}
}
return nil
}
示例2: syncProcess_v3FSEvent
func syncProcess_v3FSEvent(localDir string, serverPrefix string, c3 *clientv3.Client, event fileChangeEvent) {
etcdPath, err := filepath.Rel(localDir, event.Path)
if err != nil {
log.Printf("syncProcess_v3 error get relpath '%v': %v\n", event.Path, err)
return
}
etcdPath = serverPrefix + etcdPath
etcdPath = strings.Replace(etcdPath, string(os.PathSeparator), "/", -1)
switch {
case event.IsRemoved:
_, err := c3.Delete(context.Background(), etcdPath)
if err != nil {
log.Printf("syncProcess_v3 error while delete etcdkey '%v': %v\n", etcdPath, err)
}
case event.IsDir:
files, _ := ioutil.ReadDir(event.Path)
for _, file := range files {
path := filepath.Join(event.Path, file.Name())
content := []byte(nil)
if !file.IsDir() {
content, err = ioutil.ReadFile(path)
if err != nil {
log.Println(err)
}
}
syncProcess_v3FSEvent(localDir, serverPrefix, c3, fileChangeEvent{
Path: path,
IsDir: file.IsDir(),
IsRemoved: false,
Content: content,
})
}
case !event.IsDir:
resp, err := c3.Get(context.Background(), etcdPath)
if err != nil {
log.Printf("syncProcess_v3 Can't read key '%v': %v\n", etcdPath, err)
}
if len(resp.Kvs) > 0 {
if bytes.Equal(resp.Kvs[0].Value, event.Content) {
return
}
}
_, err = c3.Put(context.Background(), etcdPath, string(event.Content))
if err != nil {
log.Printf("syncProcess_v3 error while put etcdkey '%v': %v\n", etcdPath, err)
}
}
}
示例3: makeMirror
func makeMirror(ctx context.Context, c *clientv3.Client, dc *clientv3.Client) error {
total := int64(0)
go func() {
for {
time.Sleep(30 * time.Second)
fmt.Println(atomic.LoadInt64(&total))
}
}()
s := mirror.NewSyncer(c, mmprefix, 0)
rc, errc := s.SyncBase(ctx)
// if destination prefix is specified and remove destination prefix is true return error
if mmnodestprefix && len(mmdestprefix) > 0 {
ExitWithError(ExitBadArgs, fmt.Errorf("`--dest-prefix` and `--no-dest-prefix` cannot be set at the same time, choose one."))
}
// if remove destination prefix is false and destination prefix is empty set the value of destination prefix same as prefix
if !mmnodestprefix && len(mmdestprefix) == 0 {
mmdestprefix = mmprefix
}
for r := range rc {
for _, kv := range r.Kvs {
_, err := dc.Put(ctx, modifyPrefix(string(kv.Key)), string(kv.Value))
if err != nil {
return err
}
atomic.AddInt64(&total, 1)
}
}
err := <-errc
if err != nil {
return err
}
wc := s.SyncUpdates(ctx)
for wr := range wc {
if wr.CompactRevision != 0 {
return rpctypes.ErrCompacted
}
var rev int64
ops := []clientv3.Op{}
for _, ev := range wr.Events {
nrev := ev.Kv.ModRevision
if rev != 0 && nrev > rev {
_, err := dc.Txn(ctx).Then(ops...).Commit()
if err != nil {
return err
}
ops = []clientv3.Op{}
}
switch ev.Type {
case mvccpb.PUT:
ops = append(ops, clientv3.OpPut(modifyPrefix(string(ev.Kv.Key)), string(ev.Kv.Value)))
atomic.AddInt64(&total, 1)
case mvccpb.DELETE:
ops = append(ops, clientv3.OpDelete(modifyPrefix(string(ev.Kv.Key))))
atomic.AddInt64(&total, 1)
default:
panic("unexpected event type")
}
}
if len(ops) != 0 {
_, err := dc.Txn(ctx).Then(ops...).Commit()
if err != nil {
return err
}
}
}
return nil
}