vitess.io/vitess@v0.16.2/go/vt/vttablet/tabletmanager/shard_watcher.go (about) 1 /* 2 Copyright 2019 The Vitess Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package tabletmanager 18 19 import ( 20 "context" 21 22 "vitess.io/vitess/go/vt/log" 23 "vitess.io/vitess/go/vt/topo" 24 ) 25 26 type shardWatcher struct { 27 watchChan <-chan *topo.WatchShardData 28 watchCancel context.CancelFunc 29 } 30 31 func (sw *shardWatcher) active() bool { 32 return sw.watchChan != nil 33 } 34 35 func (sw *shardWatcher) start(ts *topo.Server, keyspace, shard string) error { 36 log.Infof("Starting shard watch of %v/%v", keyspace, shard) 37 38 ctx, cancel := context.WithCancel(context.Background()) 39 _, c, err := ts.WatchShard(ctx, keyspace, shard) 40 if err != nil { 41 cancel() 42 return err 43 } 44 45 sw.watchChan = c 46 sw.watchCancel = cancel 47 return nil 48 } 49 50 func (sw *shardWatcher) stop() { 51 if !sw.active() { 52 return 53 } 54 55 log.Infof("Stopping shard watch...") 56 sw.watchCancel() 57 58 // Drain all remaining watch events. 59 for range sw.watchChan { 60 } 61 log.Infof("Shard watch stopped.") 62 63 sw.watchChan = nil 64 sw.watchCancel = nil 65 }