session.go 2.8 KB

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