session.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. lc := v3.NewLease(client)
  44. resp, err := lc.Create(context.TODO(), sessionTTL)
  45. if err != nil {
  46. return nil, err
  47. }
  48. id := lease.LeaseID(resp.ID)
  49. ctx, cancel := context.WithCancel(context.Background())
  50. keepAlive, err := lc.KeepAlive(ctx, id)
  51. if err != nil || keepAlive == nil {
  52. return nil, err
  53. }
  54. donec := make(chan struct{})
  55. s := &Session{client: client, id: id, cancel: cancel, donec: donec}
  56. clientSessions.sessions[client] = s
  57. // keep the lease alive until client error or cancelled context
  58. go func() {
  59. defer func() {
  60. clientSessions.mu.Lock()
  61. delete(clientSessions.sessions, client)
  62. clientSessions.mu.Unlock()
  63. lc.Close()
  64. close(donec)
  65. }()
  66. for range keepAlive {
  67. // eat messages until keep alive channel closes
  68. }
  69. }()
  70. return s, nil
  71. }
  72. // Lease is the lease ID for keys bound to the session.
  73. func (s *Session) Lease() lease.LeaseID { return s.id }
  74. // Done returns a channel that closes when the lease is orphaned, expires, or
  75. // is otherwise no longer being refreshed.
  76. func (s *Session) Done() <-chan struct{} { return s.donec }
  77. // Orphan ends the refresh for the session lease. This is useful
  78. // in case the state of the client connection is indeterminate (revoke
  79. // would fail) or when transferring lease ownership.
  80. func (s *Session) Orphan() {
  81. s.cancel()
  82. <-s.donec
  83. }
  84. // Close orphans the session and revokes the session lease.
  85. func (s *Session) Close() error {
  86. s.Orphan()
  87. _, err := v3.NewLease(s.client).Revoke(context.TODO(), s.id)
  88. return err
  89. }