Skip to content

Commit eb3f041

Browse files
authored
Use WaitGroup to block extension loop iteration until apm server request is complete (#137)
1 parent a3f5c80 commit eb3f041

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

apm-lambda-extension/main.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,19 +135,19 @@ func main() {
135135
// Receive agent data as it comes in and post it to the APM server.
136136
// Stop checking for, and sending agent data when the function invocation
137137
// has completed, signaled via a channel.
138+
backgroundDataSendWg.Add(1)
138139
go func() {
140+
defer backgroundDataSendWg.Done()
139141
for {
140142
select {
141143
case <-funcDone:
142144
log.Println("funcDone signal received, not processing any more agent data")
143145
return
144146
case agentData := <-agentDataChannel:
145-
backgroundDataSendWg.Add(1)
146147
err := extension.PostToApmServer(client, agentData, config)
147148
if err != nil {
148149
log.Printf("Error sending to APM server, skipping: %v", err)
149150
}
150-
backgroundDataSendWg.Done()
151151
}
152152
}
153153
}()
@@ -194,13 +194,13 @@ func main() {
194194
log.Println("Time expired waiting for agent signal or runtimeDone event")
195195
}
196196

197+
close(funcDone)
197198
backgroundDataSendWg.Wait()
198199
if config.SendStrategy == extension.SyncFlush {
199200
// Flush APM data now that the function invocation has completed
200201
extension.FlushAPMData(client, agentDataChannel, config)
201202
}
202203

203-
close(funcDone)
204204
close(runtimeDoneSignal)
205205
close(extension.AgentDoneSignal)
206206
}

0 commit comments

Comments
 (0)