2016-05-13 06:50:58 +03:00
|
|
|
// Copyright 2016 The etcd Authors
|
2016-02-23 03:39:05 +03:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package clientv3_test
|
|
|
|
|
|
|
|
import (
|
2017-09-07 00:57:25 +03:00
|
|
|
"context"
|
2016-02-23 03:39:05 +03:00
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
|
2019-04-27 00:17:16 +03:00
|
|
|
"go.etcd.io/etcd/v3/clientv3"
|
2016-02-23 03:39:05 +03:00
|
|
|
)
|
|
|
|
|
2016-04-07 20:27:36 +03:00
|
|
|
func ExampleLease_grant() {
|
2016-02-23 03:39:05 +03:00
|
|
|
cli, err := clientv3.New(clientv3.Config{
|
|
|
|
Endpoints: endpoints,
|
|
|
|
DialTimeout: dialTimeout,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
defer cli.Close()
|
|
|
|
|
|
|
|
// minimum lease TTL is 5-second
|
2016-04-07 20:27:36 +03:00
|
|
|
resp, err := cli.Grant(context.TODO(), 5)
|
2016-02-23 03:39:05 +03:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// after 5 seconds, the key 'foo' will be removed
|
2016-04-15 03:37:25 +03:00
|
|
|
_, err = cli.Put(context.TODO(), "foo", "bar", clientv3.WithLease(resp.ID))
|
2016-02-23 03:39:05 +03:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func ExampleLease_revoke() {
|
|
|
|
cli, err := clientv3.New(clientv3.Config{
|
|
|
|
Endpoints: endpoints,
|
|
|
|
DialTimeout: dialTimeout,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
defer cli.Close()
|
|
|
|
|
2016-04-07 20:27:36 +03:00
|
|
|
resp, err := cli.Grant(context.TODO(), 5)
|
2016-02-23 03:39:05 +03:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2016-04-15 03:37:25 +03:00
|
|
|
_, err = cli.Put(context.TODO(), "foo", "bar", clientv3.WithLease(resp.ID))
|
2016-02-23 03:39:05 +03:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// revoking lease expires the key attached to its lease ID
|
2016-04-15 03:37:25 +03:00
|
|
|
_, err = cli.Revoke(context.TODO(), resp.ID)
|
2016-02-23 03:39:05 +03:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2016-02-25 09:49:24 +03:00
|
|
|
gresp, err := cli.Get(context.TODO(), "foo")
|
2016-02-23 03:39:05 +03:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
fmt.Println("number of keys:", len(gresp.Kvs))
|
2016-04-15 10:18:24 +03:00
|
|
|
// Output: number of keys: 0
|
2016-02-23 03:39:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func ExampleLease_keepAlive() {
|
|
|
|
cli, err := clientv3.New(clientv3.Config{
|
|
|
|
Endpoints: endpoints,
|
|
|
|
DialTimeout: dialTimeout,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
defer cli.Close()
|
|
|
|
|
2016-04-07 20:27:36 +03:00
|
|
|
resp, err := cli.Grant(context.TODO(), 5)
|
2016-02-23 03:39:05 +03:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2016-04-15 03:37:25 +03:00
|
|
|
_, err = cli.Put(context.TODO(), "foo", "bar", clientv3.WithLease(resp.ID))
|
2016-02-23 03:39:05 +03:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// the key 'foo' will be kept forever
|
2017-05-02 19:34:54 +03:00
|
|
|
ch, kaerr := cli.KeepAlive(context.TODO(), resp.ID)
|
|
|
|
if kaerr != nil {
|
|
|
|
log.Fatal(kaerr)
|
2017-04-04 21:05:19 +03:00
|
|
|
}
|
|
|
|
|
2017-05-02 19:34:54 +03:00
|
|
|
ka := <-ch
|
2016-04-15 10:18:24 +03:00
|
|
|
fmt.Println("ttl:", ka.TTL)
|
|
|
|
// Output: ttl: 5
|
2016-02-23 03:39:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func ExampleLease_keepAliveOnce() {
|
|
|
|
cli, err := clientv3.New(clientv3.Config{
|
|
|
|
Endpoints: endpoints,
|
|
|
|
DialTimeout: dialTimeout,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
defer cli.Close()
|
|
|
|
|
2016-04-07 20:27:36 +03:00
|
|
|
resp, err := cli.Grant(context.TODO(), 5)
|
2016-02-23 03:39:05 +03:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2016-04-15 03:37:25 +03:00
|
|
|
_, err = cli.Put(context.TODO(), "foo", "bar", clientv3.WithLease(resp.ID))
|
2016-02-23 03:39:05 +03:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// to renew the lease only once
|
2017-05-02 19:34:54 +03:00
|
|
|
ka, kaerr := cli.KeepAliveOnce(context.TODO(), resp.ID)
|
|
|
|
if kaerr != nil {
|
|
|
|
log.Fatal(kaerr)
|
2016-02-23 03:39:05 +03:00
|
|
|
}
|
2016-04-15 10:18:24 +03:00
|
|
|
|
|
|
|
fmt.Println("ttl:", ka.TTL)
|
|
|
|
// Output: ttl: 5
|
2016-02-23 03:39:05 +03:00
|
|
|
}
|