resource: aws: ec2: Move waitgroup to resource struct
This commit is contained in:
@@ -85,6 +85,7 @@ type AwsEc2Res struct {
|
|||||||
|
|
||||||
awsChan chan *chanStruct // channel used to send events and errors to Watch()
|
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
|
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.
|
// 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.awsChan = make(chan *chanStruct)
|
||||||
obj.closeChan = make(chan struct{})
|
obj.closeChan = make(chan struct{})
|
||||||
|
obj.wg = &sync.WaitGroup{}
|
||||||
|
|
||||||
return obj.BaseRes.Init() // call base init, b/c we're overriding
|
return obj.BaseRes.Init() // call base init, b/c we're overriding
|
||||||
}
|
}
|
||||||
@@ -184,19 +186,18 @@ func (obj *AwsEc2Res) longpollWatch() error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
ctx, cancel := context.WithCancel(context.TODO())
|
ctx, cancel := context.WithCancel(context.TODO())
|
||||||
wg := &sync.WaitGroup{}
|
obj.wg.Add(1)
|
||||||
wg.Add(1)
|
|
||||||
go func() {
|
go func() {
|
||||||
defer wg.Done()
|
defer obj.wg.Done()
|
||||||
select {
|
select {
|
||||||
case <-obj.closeChan:
|
case <-obj.closeChan:
|
||||||
cancel()
|
cancel()
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
wg.Add(1)
|
obj.wg.Add(1)
|
||||||
defer wg.Wait()
|
defer obj.wg.Wait()
|
||||||
go func() {
|
go func() {
|
||||||
defer wg.Done()
|
defer obj.wg.Done()
|
||||||
defer close(obj.awsChan)
|
defer close(obj.awsChan)
|
||||||
for {
|
for {
|
||||||
diInput := &ec2.DescribeInstancesInput{
|
diInput := &ec2.DescribeInstancesInput{
|
||||||
|
|||||||
Reference in New Issue
Block a user