watch_broadcasts.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. wbswb.mu.Lock()
  55. // NB: victim lock already held
  56. if wb.nextrev >= wbswb.nextrev && wbswb.nextrev != 0 {
  57. for w := range wb.receivers {
  58. wbswb.receivers[w] = struct{}{}
  59. wbs.watchers[w] = wbswb
  60. }
  61. wb.receivers = nil
  62. }
  63. wbswb.mu.Unlock()
  64. if wb.empty() {
  65. delete(wbs.bcasts, wb)
  66. wb.stop()
  67. break
  68. }
  69. }
  70. wbs.mu.Unlock()
  71. }
  72. func (wbs *watchBroadcasts) add(w *watcher) {
  73. wbs.mu.Lock()
  74. defer wbs.mu.Unlock()
  75. // find fitting bcast
  76. for wb := range wbs.bcasts {
  77. if wb.add(w) {
  78. wbs.watchers[w] = wb
  79. return
  80. }
  81. }
  82. // no fit; create a bcast
  83. wb := newWatchBroadcast(wbs.wp, w, wbs.update)
  84. wbs.watchers[w] = wb
  85. wbs.bcasts[wb] = struct{}{}
  86. }
  87. func (wbs *watchBroadcasts) delete(w *watcher) {
  88. wbs.mu.Lock()
  89. defer wbs.mu.Unlock()
  90. wb, ok := wbs.watchers[w]
  91. if !ok {
  92. panic("deleting missing watcher from broadcasts")
  93. }
  94. delete(wbs.watchers, w)
  95. wb.delete(w)
  96. if wb.empty() {
  97. delete(wbs.bcasts, wb)
  98. wb.stop()
  99. }
  100. }
  101. func (wbs *watchBroadcasts) empty() bool { return len(wbs.bcasts) == 0 }
  102. func (wbs *watchBroadcasts) stop() {
  103. wbs.mu.Lock()
  104. defer wbs.mu.Unlock()
  105. for wb := range wbs.bcasts {
  106. wb.stop()
  107. }
  108. wbs.bcasts = nil
  109. close(wbs.updatec)
  110. <-wbs.donec
  111. }
  112. func (wbs *watchBroadcasts) update(wb *watchBroadcast) {
  113. select {
  114. case wbs.updatec <- wb:
  115. default:
  116. }
  117. }