feat(events): add idempotent Kafka event consumer runtime
This commit is contained in:
1 parent
565ef8d4f5
commit
3adb0ef9e4
47 files changed
+5018
-105
No files matched your search
@@ -9,6 +9,7 @@ import (
|
||||
"net/http"
|
||||
"os"
|
||||
"os/signal"
|
||||
"sync"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
@@ -36,6 +37,9 @@ type Application struct {
|
||||
// relay publishes outbox events while the servers run. It is nil when
|
||||
// the relay is disabled or the database is not configured.
|
||||
relay *outboxrelay.Relay
|
||||
// consumers apply events from the event bus while the servers run. The
|
||||
// list is empty until a handler is registered in eventHandlers.
|
||||
consumers eventConsumers
|
||||
}
|
||||
|
||||
func NewApplication(
|
||||
@@ -45,6 +49,7 @@ func NewApplication(
|
||||
logger *slog.Logger,
|
||||
mailSender outbound.MailSender,
|
||||
relay *outboxrelay.Relay,
|
||||
consumers eventConsumers,
|
||||
) *Application {
|
||||
return &Application{
|
||||
config: config,
|
||||
@@ -53,6 +58,7 @@ func NewApplication(
|
||||
logger: logger,
|
||||
mailSender: mailSender,
|
||||
relay: relay,
|
||||
consumers: consumers,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -149,11 +155,11 @@ func (application *Application) run(ctx context.Context) error {
|
||||
slog.String("metrics_addr", application.config.MetricsAddr),
|
||||
)
|
||||
|
||||
// The relay stops when the servers stop, including after a server
|
||||
// The workers stop when the servers stop, including after a server
|
||||
// failure that leaves ctx running.
|
||||
runCtx, stopRun := context.WithCancel(ctx)
|
||||
defer stopRun()
|
||||
relayStopped := application.startRelay(runCtx)
|
||||
workersStopped := application.startWorkers(runCtx)
|
||||
|
||||
apiServer := newHTTPServer(application.apiHandler)
|
||||
metricsServer := newHTTPServer(application.metrics.Handler())
|
||||
@@ -167,23 +173,30 @@ func (application *Application) run(ctx context.Context) error {
|
||||
)
|
||||
application.logger.Info("servers stopped")
|
||||
stopRun()
|
||||
// The relay finishes its pass in progress within SHUTDOWN_TIMEOUT, so
|
||||
// the cleanup that closes its pool and producer runs after it.
|
||||
<-relayStopped
|
||||
// The relay finishes its pass in progress within SHUTDOWN_TIMEOUT and
|
||||
// each consumer its record in progress within CONSUMER_SHUTDOWN_GRACE
|
||||
// plus one offset commit, so the cleanup that closes their pools,
|
||||
// producer, and group members runs after all of them stopped.
|
||||
<-workersStopped
|
||||
application.logger.Info("workers stopped")
|
||||
return err
|
||||
}
|
||||
|
||||
// startRelay runs the relay until ctx ends and returns a channel that closes
|
||||
// when it has stopped.
|
||||
func (application *Application) startRelay(ctx context.Context) <-chan struct{} {
|
||||
stopped := make(chan struct{})
|
||||
if application.relay == nil {
|
||||
close(stopped)
|
||||
return stopped
|
||||
// startWorkers runs the relay and every consumer until ctx ends and returns
|
||||
// a channel that closes when all of them have stopped. The workers stop
|
||||
// together with the servers; none of them depends on another.
|
||||
func (application *Application) startWorkers(ctx context.Context) <-chan struct{} {
|
||||
var workers sync.WaitGroup
|
||||
if application.relay != nil {
|
||||
workers.Go(func() { application.relay.Run(ctx) })
|
||||
}
|
||||
for _, consumer := range application.consumers {
|
||||
workers.Go(func() { consumer.Run(ctx) })
|
||||
}
|
||||
stopped := make(chan struct{})
|
||||
go func() {
|
||||
defer close(stopped)
|
||||
application.relay.Run(ctx)
|
||||
workers.Wait()
|
||||
}()
|
||||
return stopped
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user