example_lease_test.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. // Copyright 2016 CoreOS, Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package clientv3_test
  15. import (
  16. "fmt"
  17. "log"
  18. "github.com/coreos/etcd/Godeps/_workspace/src/golang.org/x/net/context"
  19. "github.com/coreos/etcd/clientv3"
  20. )
  21. func ExampleLease_create() {
  22. cli, err := clientv3.New(clientv3.Config{
  23. Endpoints: endpoints,
  24. DialTimeout: dialTimeout,
  25. })
  26. if err != nil {
  27. log.Fatal(err)
  28. }
  29. defer cli.Close()
  30. // minimum lease TTL is 5-second
  31. resp, err := cli.Create(context.TODO(), 5)
  32. if err != nil {
  33. log.Fatal(err)
  34. }
  35. // after 5 seconds, the key 'foo' will be removed
  36. _, err = cli.Put(context.TODO(), "foo", "bar", clientv3.WithLease(clientv3.LeaseID(resp.ID)))
  37. if err != nil {
  38. log.Fatal(err)
  39. }
  40. }
  41. func ExampleLease_revoke() {
  42. cli, err := clientv3.New(clientv3.Config{
  43. Endpoints: endpoints,
  44. DialTimeout: dialTimeout,
  45. })
  46. if err != nil {
  47. log.Fatal(err)
  48. }
  49. defer cli.Close()
  50. resp, err := cli.Create(context.TODO(), 5)
  51. if err != nil {
  52. log.Fatal(err)
  53. }
  54. _, err = cli.Put(context.TODO(), "foo", "bar", clientv3.WithLease(clientv3.LeaseID(resp.ID)))
  55. if err != nil {
  56. log.Fatal(err)
  57. }
  58. // revoking lease expires the key attached to its lease ID
  59. _, err = cli.Revoke(context.TODO(), clientv3.LeaseID(resp.ID))
  60. if err != nil {
  61. log.Fatal(err)
  62. }
  63. gresp, err := cli.Get(context.TODO(), "foo")
  64. if err != nil {
  65. log.Fatal(err)
  66. }
  67. fmt.Println("number of keys:", len(gresp.Kvs))
  68. // number of keys: 0
  69. }
  70. func ExampleLease_keepAlive() {
  71. cli, err := clientv3.New(clientv3.Config{
  72. Endpoints: endpoints,
  73. DialTimeout: dialTimeout,
  74. })
  75. if err != nil {
  76. log.Fatal(err)
  77. }
  78. defer cli.Close()
  79. resp, err := cli.Create(context.TODO(), 5)
  80. if err != nil {
  81. log.Fatal(err)
  82. }
  83. _, err = cli.Put(context.TODO(), "foo", "bar", clientv3.WithLease(clientv3.LeaseID(resp.ID)))
  84. if err != nil {
  85. log.Fatal(err)
  86. }
  87. // the key 'foo' will be kept forever
  88. _, err = cli.KeepAlive(context.TODO(), clientv3.LeaseID(resp.ID))
  89. if err != nil {
  90. log.Fatal(err)
  91. }
  92. }
  93. func ExampleLease_keepAliveOnce() {
  94. cli, err := clientv3.New(clientv3.Config{
  95. Endpoints: endpoints,
  96. DialTimeout: dialTimeout,
  97. })
  98. if err != nil {
  99. log.Fatal(err)
  100. }
  101. defer cli.Close()
  102. resp, err := cli.Create(context.TODO(), 5)
  103. if err != nil {
  104. log.Fatal(err)
  105. }
  106. _, err = cli.Put(context.TODO(), "foo", "bar", clientv3.WithLease(clientv3.LeaseID(resp.ID)))
  107. if err != nil {
  108. log.Fatal(err)
  109. }
  110. // to renew the lease only once
  111. _, err = cli.KeepAliveOnce(context.TODO(), clientv3.LeaseID(resp.ID))
  112. if err != nil {
  113. log.Fatal(err)
  114. }
  115. }