session.go 2.9 KB

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