example_lease_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. // Copyright 2016 The etcd Authors
  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. "context"
  17. "fmt"
  18. "log"
  19. "go.etcd.io/etcd/clientv3"
  20. )
  21. func ExampleLease_grant() {
  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.Grant(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(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.Grant(context.TODO(), 5)
  51. if err != nil {
  52. log.Fatal(err)
  53. }
  54. _, err = cli.Put(context.TODO(), "foo", "bar", clientv3.WithLease(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(), 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. // Output: 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.Grant(context.TODO(), 5)
  80. if err != nil {
  81. log.Fatal(err)
  82. }
  83. _, err = cli.Put(context.TODO(), "foo", "bar", clientv3.WithLease(resp.ID))
  84. if err != nil {
  85. log.Fatal(err)
  86. }
  87. // the key 'foo' will be kept forever
  88. ch, kaerr := cli.KeepAlive(context.TODO(), resp.ID)
  89. if kaerr != nil {
  90. log.Fatal(kaerr)
  91. }
  92. ka := <-ch
  93. fmt.Println("ttl:", ka.TTL)
  94. // Output: ttl: 5
  95. }
  96. func ExampleLease_keepAliveOnce() {
  97. cli, err := clientv3.New(clientv3.Config{
  98. Endpoints: endpoints,
  99. DialTimeout: dialTimeout,
  100. })
  101. if err != nil {
  102. log.Fatal(err)
  103. }
  104. defer cli.Close()
  105. resp, err := cli.Grant(context.TODO(), 5)
  106. if err != nil {
  107. log.Fatal(err)
  108. }
  109. _, err = cli.Put(context.TODO(), "foo", "bar", clientv3.WithLease(resp.ID))
  110. if err != nil {
  111. log.Fatal(err)
  112. }
  113. // to renew the lease only once
  114. ka, kaerr := cli.KeepAliveOnce(context.TODO(), resp.ID)
  115. if kaerr != nil {
  116. log.Fatal(kaerr)
  117. }
  118. fmt.Println("ttl:", ka.TTL)
  119. // Output: ttl: 5
  120. }