resource: aws: ec2: Move waitgroup to resource struct

This commit is contained in:
Jonathan Gold
2017-11-08 16:38:17 -05:00
parent c2a5e3f5d8
commit 3634b3450d

View File

@@ -85,6 +85,7 @@ type AwsEc2Res struct {
awsChan chan *chanStruct // channel used to send events and errors to Watch()
closeChan chan struct{} // channel used to cancel context when it's time to shut down
wg *sync.WaitGroup // waitgroup for goroutines in Watch()
}
// chanStruct defines the type for a channel used to pass events and errors to watch.
@@ -167,6 +168,7 @@ func (obj *AwsEc2Res) Init() error {
obj.awsChan = make(chan *chanStruct)
obj.closeChan = make(chan struct{})
obj.wg = &sync.WaitGroup{}
return obj.BaseRes.Init() // call base init, b/c we're overriding
}
@@ -184,19 +186,18 @@ func (obj *AwsEc2Res) longpollWatch() error {
return err
}
ctx, cancel := context.WithCancel(context.TODO())
wg := &sync.WaitGroup{}
wg.Add(1)
obj.wg.Add(1)
go func() {
defer wg.Done()
defer obj.wg.Done()
select {
case <-obj.closeChan:
cancel()
}
}()
wg.Add(1)
defer wg.Wait()
obj.wg.Add(1)
defer obj.wg.Wait()
go func() {
defer wg.Done()
defer obj.wg.Done()
defer close(obj.awsChan)
for {
diInput := &ec2.DescribeInstancesInput{