session.go 2.9 KB

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