watch_broadcasts.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. // 1. check if wbswb is behind wb so it won't skip any events in wb
  56. // 2. ensure wbswb started; nextrev == 0 may mean wbswb is waiting
  57. // for a current watcher and expects a create event from the server.
  58. if wb.nextrev >= wbswb.nextrev && wbswb.responses > 0 {
  59. for w := range wb.receivers {
  60. wbswb.receivers[w] = struct{}{}
  61. wbs.watchers[w] = wbswb
  62. }
  63. wb.receivers = nil
  64. }
  65. wbswb.mu.Unlock()
  66. if wb.empty() {
  67. delete(wbs.bcasts, wb)
  68. wb.stop()
  69. break
  70. }
  71. }
  72. wbs.mu.Unlock()
  73. }
  74. func (wbs *watchBroadcasts) add(w *watcher) {
  75. wbs.mu.Lock()
  76. defer wbs.mu.Unlock()
  77. // find fitting bcast
  78. for wb := range wbs.bcasts {
  79. if wb.add(w) {
  80. wbs.watchers[w] = wb
  81. return
  82. }
  83. }
  84. // no fit; create a bcast
  85. wb := newWatchBroadcast(wbs.wp, w, wbs.update)
  86. wbs.watchers[w] = wb
  87. wbs.bcasts[wb] = struct{}{}
  88. }
  89. func (wbs *watchBroadcasts) delete(w *watcher) {
  90. wbs.mu.Lock()
  91. defer wbs.mu.Unlock()
  92. wb, ok := wbs.watchers[w]
  93. if !ok {
  94. panic("deleting missing watcher from broadcasts")
  95. }
  96. delete(wbs.watchers, w)
  97. wb.delete(w)
  98. if wb.empty() {
  99. delete(wbs.bcasts, wb)
  100. wb.stop()
  101. }
  102. }
  103. func (wbs *watchBroadcasts) empty() bool { return len(wbs.bcasts) == 0 }
  104. func (wbs *watchBroadcasts) stop() {
  105. wbs.mu.Lock()
  106. defer wbs.mu.Unlock()
  107. for wb := range wbs.bcasts {
  108. wb.stop()
  109. }
  110. wbs.bcasts = nil
  111. close(wbs.updatec)
  112. <-wbs.donec
  113. }
  114. func (wbs *watchBroadcasts) update(wb *watchBroadcast) {
  115. select {
  116. case wbs.updatec <- wb:
  117. default:
  118. }
  119. }