Skip to content

Commit 12d4752

Browse files
committed
code review
1 parent b33279f commit 12d4752

3 files changed

Lines changed: 6 additions & 7 deletions

File tree

cmd/watcher/main.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,18 @@ func handleExitSignal(cancel context.CancelFunc, container *service.Container) f
4646

4747
// Cancel the main context immediately so all goroutines start shutting down.
4848
cancel()
49-
container.GetKafkaClient().Close()
5049

5150
// Use independent background contexts so these calls succeed even after cancel().
51+
// Disconnect MongoDB before Kafka close: Kafka.Close() busy-waits with no timeout
52+
// and could block indefinitely, preventing Disconnect() from running.
5253
disconnectCtx, disconnectCancel := context.WithTimeout(context.Background(), 10*time.Second)
5354
defer disconnectCancel()
5455
if err := container.GetMongoConnection().Client().Disconnect(disconnectCtx); err != nil {
5556
log.Error("Failed to disconnect MongoDB client", logger.Error("error", err))
5657
}
5758

59+
container.GetKafkaClient().Close()
60+
5861
httpShutdownCtx, httpShutdownCancel := context.WithTimeout(context.Background(), 10*time.Second)
5962
defer httpShutdownCancel()
6063
if err := container.GetHttpServer().Close(httpShutdownCtx); err != nil {

internal/mongo/replay_producer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ func (r *ReplayProducer) Produce(ctx context.Context) (chan *ChangeEvent, error)
5757
var events = make(chan *ChangeEvent)
5858

5959
go func() {
60-
defer func() { closeCursor(cursor) }()
60+
defer closeCursor(cursor)
6161
defer close(events)
6262

6363
r.sendEvents(ctx, cursor, events)

internal/mongo/watch_producer.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,7 @@ func (w *WatchProducer) GetProducer(o ...WatchOption) ChangeEventProducer {
4242

4343
go func() {
4444
defer close(events)
45-
defer func() {
46-
if cursor != nil {
47-
closeCursor(cursor)
48-
}
49-
}()
45+
defer func() { closeCursor(cursor) }()
5046
for {
5147
select {
5248
case <-ctx.Done():

0 commit comments

Comments
 (0)