session.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 concurrency
  15. import (
  16. "time"
  17. v3 "github.com/coreos/etcd/clientv3"
  18. "golang.org/x/net/context"
  19. )
  20. const defaultSessionTTL = 60
  21. // Session represents a lease kept alive for the lifetime of a client.
  22. // Fault-tolerant applications may use sessions to reason about liveness.
  23. type Session struct {
  24. client *v3.Client
  25. opts *sessionOptions
  26. id v3.LeaseID
  27. cancel context.CancelFunc
  28. donec <-chan struct{}
  29. }
  30. // NewSession gets the leased session for a client.
  31. func NewSession(client *v3.Client, opts ...SessionOption) (*Session, error) {
  32. ops := &sessionOptions{ttl: defaultSessionTTL}
  33. for _, opt := range opts {
  34. opt(ops)
  35. }
  36. resp, err := client.Grant(client.Ctx(), int64(ops.ttl))
  37. if err != nil {
  38. return nil, err
  39. }
  40. id := v3.LeaseID(resp.ID)
  41. ctx, cancel := context.WithCancel(client.Ctx())
  42. keepAlive, err := client.KeepAlive(ctx, id)
  43. if err != nil || keepAlive == nil {
  44. return nil, err
  45. }
  46. donec := make(chan struct{})
  47. s := &Session{client: client, opts: ops, id: id, cancel: cancel, donec: donec}
  48. // keep the lease alive until client error or cancelled context
  49. go func() {
  50. defer close(donec)
  51. for range keepAlive {
  52. // eat messages until keep alive channel closes
  53. }
  54. }()
  55. return s, nil
  56. }
  57. // Client is the etcd client that is attached to the session.
  58. func (s *Session) Client() *v3.Client {
  59. return s.client
  60. }
  61. // Lease is the lease ID for keys bound to the session.
  62. func (s *Session) Lease() v3.LeaseID { return s.id }
  63. // Done returns a channel that closes when the lease is orphaned, expires, or
  64. // is otherwise no longer being refreshed.
  65. func (s *Session) Done() <-chan struct{} { return s.donec }
  66. // Orphan ends the refresh for the session lease. This is useful
  67. // in case the state of the client connection is indeterminate (revoke
  68. // would fail) or when transferring lease ownership.
  69. func (s *Session) Orphan() {
  70. s.cancel()
  71. <-s.donec
  72. }
  73. // Close orphans the session and revokes the session lease.
  74. func (s *Session) Close() error {
  75. s.Orphan()
  76. // if revoke takes longer than the ttl, lease is expired anyway
  77. ctx, cancel := context.WithTimeout(s.client.Ctx(), time.Duration(s.opts.ttl)*time.Second)
  78. _, err := s.client.Revoke(ctx, s.id)
  79. cancel()
  80. return err
  81. }
  82. type sessionOptions struct {
  83. ttl int
  84. }
  85. // SessionOption configures Session.
  86. type SessionOption func(*sessionOptions)
  87. // WithTTL configures the session's TTL in seconds.
  88. // If TTL is <= 0, the default 60 seconds TTL will be used.
  89. func WithTTL(ttl int) SessionOption {
  90. return func(so *sessionOptions) {
  91. if ttl > 0 {
  92. so.ttl = ttl
  93. }
  94. }
  95. }