watch_broadcasts.go 3.1 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. )
  18. type watchBroadcasts struct {
  19. wp *watchProxy
  20. // mu protects bcasts and watchers from the coalesce loop.
  21. mu sync.Mutex
  22. bcasts map[*watchBroadcast]struct{}
  23. watchers map[*watcher]*watchBroadcast
  24. updatec chan *watchBroadcast
  25. donec chan struct{}
  26. }
  27. // maxCoalesceRecievers prevents a popular watchBroadcast from being coalseced.
  28. const maxCoalesceReceivers = 5
  29. func newWatchBroadcasts(wp *watchProxy) *watchBroadcasts {
  30. wbs := &watchBroadcasts{
  31. wp: wp,
  32. bcasts: make(map[*watchBroadcast]struct{}),
  33. watchers: make(map[*watcher]*watchBroadcast),
  34. updatec: make(chan *watchBroadcast, 1),
  35. donec: make(chan struct{}),
  36. }
  37. go func() {
  38. defer close(wbs.donec)
  39. for wb := range wbs.updatec {
  40. wbs.coalesce(wb)
  41. }
  42. }()
  43. return wbs
  44. }
  45. func (wbs *watchBroadcasts) coalesce(wb *watchBroadcast) {
  46. if wb.size() >= maxCoalesceReceivers {
  47. return
  48. }
  49. wbs.mu.Lock()
  50. for wbswb := range wbs.bcasts {
  51. if wbswb == wb {
  52. continue
  53. }
  54. wb.mu.Lock()
  55. wbswb.mu.Lock()
  56. // 1. check if wbswb is behind wb so it won't skip any events in wb
  57. // 2. ensure wbswb started; nextrev == 0 may mean wbswb is waiting
  58. // for a current watcher and expects a create event from the server.
  59. if wb.nextrev >= wbswb.nextrev && wbswb.responses > 0 {
  60. for w := range wb.receivers {
  61. wbswb.receivers[w] = struct{}{}
  62. wbs.watchers[w] = wbswb
  63. }
  64. wb.receivers = nil
  65. }
  66. wbswb.mu.Unlock()
  67. wb.mu.Unlock()
  68. if wb.empty() {
  69. delete(wbs.bcasts, wb)
  70. wb.stop()
  71. break
  72. }
  73. }
  74. wbs.mu.Unlock()
  75. }
  76. func (wbs *watchBroadcasts) add(w *watcher) {
  77. wbs.mu.Lock()
  78. defer wbs.mu.Unlock()
  79. // find fitting bcast
  80. for wb := range wbs.bcasts {
  81. if wb.add(w) {
  82. wbs.watchers[w] = wb
  83. return
  84. }
  85. }
  86. // no fit; create a bcast
  87. wb := newWatchBroadcast(wbs.wp, w, wbs.update)
  88. wbs.watchers[w] = wb
  89. wbs.bcasts[wb] = struct{}{}
  90. }
  91. // delete removes a watcher and returns the number of remaining watchers.
  92. func (wbs *watchBroadcasts) delete(w *watcher) int {
  93. wbs.mu.Lock()
  94. defer wbs.mu.Unlock()
  95. wb, ok := wbs.watchers[w]
  96. if !ok {
  97. panic("deleting missing watcher from broadcasts")
  98. }
  99. delete(wbs.watchers, w)
  100. wb.delete(w)
  101. if wb.empty() {
  102. delete(wbs.bcasts, wb)
  103. wb.stop()
  104. }
  105. return len(wbs.bcasts)
  106. }
  107. func (wbs *watchBroadcasts) stop() {
  108. wbs.mu.Lock()
  109. for wb := range wbs.bcasts {
  110. wb.stop()
  111. }
  112. wbs.bcasts = nil
  113. close(wbs.updatec)
  114. wbs.mu.Unlock()
  115. <-wbs.donec
  116. }
  117. func (wbs *watchBroadcasts) update(wb *watchBroadcast) {
  118. select {
  119. case wbs.updatec <- wb:
  120. default:
  121. }
  122. }