vitess.io/vitess@v0.16.2/go/vt/vtorc/inst/postponed_functions.go (about) 1 /* 2 Copyright 2015 Shlomi Noach, courtesy Booking.com 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 inst 18 19 import ( 20 "sync" 21 22 "vitess.io/vitess/go/vt/log" 23 ) 24 25 type PostponedFunctionsContainer struct { 26 waitGroup sync.WaitGroup 27 mutex sync.Mutex 28 descriptions []string 29 } 30 31 func NewPostponedFunctionsContainer() *PostponedFunctionsContainer { 32 postponedFunctionsContainer := &PostponedFunctionsContainer{ 33 descriptions: []string{}, 34 } 35 return postponedFunctionsContainer 36 } 37 38 func (postponedFuncsContainer *PostponedFunctionsContainer) AddPostponedFunction(postponedFunction func() error, description string) { 39 postponedFuncsContainer.mutex.Lock() 40 defer postponedFuncsContainer.mutex.Unlock() 41 42 postponedFuncsContainer.descriptions = append(postponedFuncsContainer.descriptions, description) 43 44 postponedFuncsContainer.waitGroup.Add(1) 45 go func() { 46 defer postponedFuncsContainer.waitGroup.Done() 47 _ = postponedFunction() 48 }() 49 } 50 51 func (postponedFuncsContainer *PostponedFunctionsContainer) Wait() { 52 log.Infof("PostponedFunctionsContainer: waiting on %+v postponed functions", postponedFuncsContainer.Len()) 53 postponedFuncsContainer.waitGroup.Wait() 54 log.Infof("PostponedFunctionsContainer: done waiting") 55 } 56 57 func (postponedFuncsContainer *PostponedFunctionsContainer) Len() int { 58 postponedFuncsContainer.mutex.Lock() 59 defer postponedFuncsContainer.mutex.Unlock() 60 61 return len(postponedFuncsContainer.descriptions) 62 } 63 64 func (postponedFuncsContainer *PostponedFunctionsContainer) Descriptions() []string { 65 postponedFuncsContainer.mutex.Lock() 66 defer postponedFuncsContainer.mutex.Unlock() 67 68 return postponedFuncsContainer.descriptions 69 }