Skip to content

Commit cd028b4

Browse files
committed
BUG/MINOR: Fix memory leaks with time.After()
1 parent 03ca824 commit cd028b4

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

discovery/aws_service_discovery_instance.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,9 @@ func (a *awsInstance) start() {
148148
go func() {
149149
a.logDebug("discovery job starting")
150150

151+
discoveryTimer := time.NewTimer(a.timeout)
152+
defer discoveryTimer.Stop()
153+
151154
for {
152155
select {
153156
case _, ok := <-a.update:
@@ -166,7 +169,7 @@ func (a *awsInstance) start() {
166169
if err != nil {
167170
a.stop()
168171
}
169-
case <-time.After(a.timeout):
172+
case <-discoveryTimer.C:
170173
a.logDebug("discovery job update triggered")
171174

172175
var api *ec2.Client
@@ -192,6 +195,7 @@ func (a *awsInstance) start() {
192195
}
193196

194197
a.logDebug("discovery job reconciliation completed")
198+
discoveryTimer.Reset(a.timeout)
195199
case <-a.ctx.Done():
196200
a.stop()
197201
}

discovery/consul_service_discovery_instance.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,9 @@ func (c *consulInstance) setAPIClient() error {
8686
}
8787

8888
func (c *consulInstance) watch() {
89+
watchTimer := time.NewTimer(c.timeout)
90+
defer watchTimer.Stop()
91+
8992
for {
9093
select {
9194
case _, ok := <-c.update:
@@ -112,13 +115,14 @@ func (c *consulInstance) watch() {
112115
}
113116
case <-c.ctx.Done():
114117
c.stop()
115-
case <-time.After(c.timeout):
118+
case <-watchTimer.C:
116119
c.logDebug("discovery job reconciliation started")
117120
if err := c.updateServices(); err != nil {
118121
// c.log.Errorf("error while updating service: %w", err)
119122
c.stop()
120123
}
121124
c.logDebug("discovery job reconciliation completed")
125+
watchTimer.Reset(c.timeout)
122126
}
123127
}
124128
}

0 commit comments

Comments
 (0)