go.etcd.io/etcd@v3.3.27+incompatible/proxy/grpcproxy/watch_ranges.go (about) 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 15 package grpcproxy 16 17 import ( 18 "sync" 19 ) 20 21 // watchRanges tracks all open watches for the proxy. 22 type watchRanges struct { 23 wp *watchProxy 24 25 mu sync.Mutex 26 bcasts map[watchRange]*watchBroadcasts 27 } 28 29 func newWatchRanges(wp *watchProxy) *watchRanges { 30 return &watchRanges{ 31 wp: wp, 32 bcasts: make(map[watchRange]*watchBroadcasts), 33 } 34 } 35 36 func (wrs *watchRanges) add(w *watcher) { 37 wrs.mu.Lock() 38 defer wrs.mu.Unlock() 39 40 if wbs := wrs.bcasts[w.wr]; wbs != nil { 41 wbs.add(w) 42 return 43 } 44 wbs := newWatchBroadcasts(wrs.wp) 45 wrs.bcasts[w.wr] = wbs 46 wbs.add(w) 47 } 48 49 func (wrs *watchRanges) delete(w *watcher) { 50 wrs.mu.Lock() 51 defer wrs.mu.Unlock() 52 wbs, ok := wrs.bcasts[w.wr] 53 if !ok { 54 panic("deleting missing range") 55 } 56 if wbs.delete(w) == 0 { 57 wbs.stop() 58 delete(wrs.bcasts, w.wr) 59 } 60 } 61 62 func (wrs *watchRanges) stop() { 63 wrs.mu.Lock() 64 defer wrs.mu.Unlock() 65 for _, wb := range wrs.bcasts { 66 wb.stop() 67 } 68 wrs.bcasts = nil 69 }