Skip to content

Commit

Permalink
fix(pubsub): fix defer call in for loop
Browse files Browse the repository at this point in the history
Signed-off-by: Koichi Shiraishi <[email protected]>
  • Loading branch information
zchee committed Nov 22, 2024
1 parent 9fe6199 commit bc6f17c
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions pubsub/topic.go
Original file line number Diff line number Diff line change
Expand Up @@ -1083,12 +1083,21 @@ func (t *Topic) initBundler() {
defer cancel()
}
bmsgs := bundle.([]*bundledMessage)
var deferFns []func()
if t.enableTracing {
for _, m := range bmsgs {
deferFns = make([]func(), len(bmsgs))
for i, m := range bmsgs {
m.batcherSpan.End()
m.createSpan.AddEvent(eventPublishStart, trace.WithAttributes(semconv.MessagingBatchMessageCount(len(bmsgs))))
defer m.createSpan.End()
defer m.createSpan.AddEvent(eventPublishEnd)
deferFns[i] = func() {
m.createSpan.AddEvent(eventPublishEnd)
m.createSpan.End()
}
}
}
if len(deferFns) > 0 {
for _, fn := range deferFns {
defer fn()
}
}
t.publishMessageBundle(ctx, bmsgs)
Expand Down

0 comments on commit bc6f17c

Please sign in to comment.