watch_broadcast.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 grpcproxy
  15. import (
  16. "sync"
  17. "golang.org/x/net/context"
  18. "github.com/coreos/etcd/clientv3"
  19. pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
  20. )
  21. // watchBroadcast broadcasts a server watcher to many client watchers.
  22. type watchBroadcast struct {
  23. // wbs is the backpointer to all broadcasts on this range
  24. wbs *watchBroadcasts
  25. // cancel stops the underlying etcd server watcher and closes ch.
  26. cancel context.CancelFunc
  27. donec chan struct{}
  28. // mu protects rev and receivers.
  29. mu sync.RWMutex
  30. // nextrev is the minimum expected next revision of the watcher on ch.
  31. nextrev int64
  32. // receivers contains all the client-side watchers to serve.
  33. receivers map[*watcher]struct{}
  34. // responses counts the number of responses
  35. responses int
  36. }
  37. func newWatchBroadcast(wp *watchProxy, w *watcher, update func(*watchBroadcast)) *watchBroadcast {
  38. cctx, cancel := context.WithCancel(wp.ctx)
  39. wb := &watchBroadcast{
  40. cancel: cancel,
  41. nextrev: w.nextrev,
  42. receivers: make(map[*watcher]struct{}),
  43. donec: make(chan struct{}),
  44. }
  45. wb.add(w)
  46. go func() {
  47. defer close(wb.donec)
  48. // loop because leader loss will close channel
  49. for cctx.Err() == nil {
  50. wch := wp.cw.Watch(cctx, w.wr.key,
  51. clientv3.WithRange(w.wr.end),
  52. clientv3.WithProgressNotify(),
  53. clientv3.WithCreatedNotify(),
  54. clientv3.WithRev(wb.nextrev),
  55. )
  56. for wr := range wch {
  57. wb.bcast(wr)
  58. update(wb)
  59. }
  60. wp.retryLimiter.Wait(cctx)
  61. }
  62. }()
  63. return wb
  64. }
  65. func (wb *watchBroadcast) bcast(wr clientv3.WatchResponse) {
  66. wb.mu.Lock()
  67. defer wb.mu.Unlock()
  68. wb.nextrev = wr.Header.Revision + 1
  69. wb.responses++
  70. for r := range wb.receivers {
  71. r.send(wr)
  72. }
  73. }
  74. // add puts a watcher into receiving a broadcast if its revision at least
  75. // meets the broadcast revision. Returns true if added.
  76. func (wb *watchBroadcast) add(w *watcher) bool {
  77. wb.mu.Lock()
  78. defer wb.mu.Unlock()
  79. if wb.nextrev > w.nextrev || (wb.nextrev == 0 && w.nextrev != 0) {
  80. // wb is too far ahead, w will miss events
  81. // or wb is being established with a current watcher
  82. return false
  83. }
  84. if wb.responses == 0 {
  85. // Newly created; create event will be sent by etcd.
  86. wb.receivers[w] = struct{}{}
  87. return true
  88. }
  89. // already sent by etcd; emulate create event
  90. ok := w.post(&pb.WatchResponse{
  91. Header: &pb.ResponseHeader{
  92. // todo: fill in ClusterId
  93. // todo: fill in MemberId:
  94. Revision: w.nextrev,
  95. // todo: fill in RaftTerm:
  96. },
  97. WatchId: w.id,
  98. Created: true,
  99. })
  100. if !ok {
  101. return false
  102. }
  103. wb.receivers[w] = struct{}{}
  104. return true
  105. }
  106. func (wb *watchBroadcast) delete(w *watcher) {
  107. wb.mu.Lock()
  108. defer wb.mu.Unlock()
  109. if _, ok := wb.receivers[w]; !ok {
  110. panic("deleting missing watcher from broadcast")
  111. }
  112. delete(wb.receivers, w)
  113. }
  114. func (wb *watchBroadcast) size() int {
  115. wb.mu.RLock()
  116. defer wb.mu.RUnlock()
  117. return len(wb.receivers)
  118. }
  119. func (wb *watchBroadcast) empty() bool { return wb.size() == 0 }
  120. func (wb *watchBroadcast) stop() {
  121. wb.cancel()
  122. <-wb.donec
  123. }