Cherrypick kafka stability - #335
Merged
Merged
Conversation
There was a problem hiding this comment.
Pull request overview
This PR improves Kafka resilience and shutdown behavior to reduce failures during broker restarts, while also updating sample configuration and a small test DB override.
Changes:
- Reworks Kafka consumer lifecycle/shutdown sequencing in
main.go(drain loops, ordered cleanup). - Tunes Sarama consumer/producer retry, metadata, heartbeat, and network timeout settings for better broker-restart tolerance.
- Updates config defaults/sample config and bumps indirect Go dependencies.
Reviewed changes
Copilot reviewed 6 out of 7 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
| main.go | Moves Kafka producer result handling under context and adds coordinated shutdown/drain logic. |
| kafka/kafka_consumer_group.go | Adds configurable Sarama consumer resilience tuning (retries, refresh, timeouts, session/heartbeat). |
| http/webconfig_server.go | Adds producer-side resilience tuning and hardens producer send/result handling during shutdown. |
| db/sqlite/sqlite_client.go | Allows overriding the unit-test sqlite DB file via WEBCONFIG_TESTDB_SQLITE_FILE. |
| config/sample_webconfig.conf | Updates sample config keys (Kafka TLS flat keys, resilience knobs, new defaults). |
| go.mod | Bumps indirect dependencies (x/* and grpc). |
| go.sum | Updates checksums for bumped dependencies. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+120
to
+122
| if server.KafkaProducerEnabled() { | ||
| go server.HandleKafkaProducerResults(gCtx) | ||
| } |
Comment on lines
+178
to
+202
| // Single shutdown goroutine with ordered cleanup: | ||
| // 1. close consumer groups (signals ConsumeClaim goroutines to stop) | ||
| // 2. wait for all consume loops to drain (ensures no goroutine is mid-send) | ||
| // 3. close AsyncProducer (safe now that no one sends to its Input channel) | ||
| // 4. shut down HTTP server | ||
| g.Go( | ||
| func() error { | ||
| <-gCtx.Done() | ||
| fmt.Printf("shutdown: initiating consumer group shutdown\n") | ||
| for _, kcgroup := range kcgroups { | ||
| if err := kcgroup.Close(); err != nil { | ||
| fmt.Fprintf(os.Stderr, "kcgroup.Close() err=%v\n", err) | ||
| } | ||
| } | ||
| consumeWg.Wait() | ||
| fmt.Printf("shutdown: all consumer loops drained\n") | ||
| if server.KafkaProducerEnabled() { | ||
| if err := server.AsyncProducer.Close(); err != nil { | ||
| fmt.Fprintf(os.Stderr, "%v AsyncProducer.Close() err=%v\n", time.Now().Format(common.LoggingTimeFormat), err) | ||
| } | ||
| } | ||
| fmt.Printf("shutdown: stopping HTTP server\n") | ||
| return server.Shutdown(context.Background()) | ||
| }, | ||
| ) |
Comment on lines
+140
to
+142
| sessionTimeout := time.Duration(conf.GetInt32(prefix+".consumer.session_timeout_sec", defaultSessionTimeoutSec)) * time.Second | ||
| sconfig.Consumer.Group.Session.Timeout = sessionTimeout | ||
| sconfig.Consumer.Group.Heartbeat.Interval = sessionTimeout / 6 |
Comment on lines
+1174
to
+1183
| defer func() { | ||
| if r := recover(); r != nil { | ||
| if fmt.Sprint(r) != "send on closed channel" { | ||
| panic(r) | ||
| } | ||
| tfields["error"] = r | ||
| log.WithFields(tfields).Warn("dropped: producer closed during shutdown") | ||
| ok = false | ||
| } | ||
| }() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
No description provided.