Files
lincodex/backend/internal/bootstrap/relay.go
T

94 lines
3.3 KiB
Go

package bootstrap
import (
"context"
"fmt"
"log/slog"
"time"
"lincodex/backend/config"
"lincodex/backend/internal/adapter/secondary/kafka"
"lincodex/backend/internal/adapter/secondary/postgres"
"lincodex/backend/internal/adapter/secondary/prometheus"
"lincodex/backend/internal/worker/outboxrelay"
"lincodex/backend/internal/worker/retry"
)
const (
// relayPoolMaxConns covers the claim transaction and the backlog read,
// the only two statements the relay can have open at once.
relayPoolMaxConns = 2
// relayBacklogInterval matches the usual Prometheus scrape interval, so
// the backlog gauges stay current without a count on every busy pass.
relayBacklogInterval = 15 * time.Second
)
// newOutboxRelay builds the outbox relay and the resources it owns: a
// PostgreSQL pool and a Kafka producer. It returns a nil relay when
// RELAY_ENABLED is false or DATABASE_URL is unset; the service then starts
// without publishing events and the outbox keeps them. Neither the pool nor
// the producer connects here, so an unavailable Kafka never blocks startup:
// the relay retries in the background.
func newOutboxRelay(
ctx context.Context,
settings config.Config,
logger *slog.Logger,
metrics *prometheus.Metrics,
) (*outboxrelay.Relay, func(), error) {
if !settings.RelayEnabled {
logger.Info("outbox relay is disabled; events stay in the outbox")
return nil, func() {}, nil
}
if settings.DatabaseURL == "" {
logger.Warn("outbox relay skipped because DATABASE_URL is not set")
return nil, func() {}, nil
}
pool, err := postgres.NewSizedPool(ctx, settings.DatabaseURL, relayPoolMaxConns)
if err != nil {
return nil, nil, fmt.Errorf("bootstrap.newOutboxRelay - create pool: %w", err)
}
relayLogger := logger.With(slog.String("component", "outbox_relay"), slog.String("topic", settings.KafkaEventsTopic))
publisher, closePublisher, err := kafka.NewPublisher(settings, relayLogger)
if err != nil {
pool.Close()
return nil, nil, fmt.Errorf("bootstrap.newOutboxRelay - create Kafka publisher: %w", err)
}
relay := outboxrelay.New(
postgres.NewOutboxRelayStore(pool, relayTransactionTimeouts(settings)),
publisher,
metrics,
relayLogger,
relaySettings(settings),
)
// The application stops the relay before this cleanup runs, so no pass
// uses the producer or the pool while they close.
return relay, func() {
closePublisher()
pool.Close()
}, nil
}
func relaySettings(settings config.Config) outboxrelay.Settings {
return outboxrelay.Settings{
BatchSize: settings.RelayBatchSize,
PollInterval: settings.RelayPollInterval,
Backoff: retry.Backoff{
Initial: settings.RelayBackoffInitial,
Max: settings.RelayBackoffMax,
},
ShutdownTimeout: settings.ShutdownTimeout,
BacklogInterval: relayBacklogInterval,
}
}
// relayTransactionTimeouts derives the server-side bounds of a claim. Between
// its statements the transaction waits only for the publish, which ends by
// RELAY_PRODUCE_TIMEOUT; one statement timeout on top covers the work around
// it, so only a client that stopped responding hits the idle limit.
func relayTransactionTimeouts(settings config.Config) postgres.OutboxRelayTimeouts {
return postgres.OutboxRelayTimeouts{
Statement: settings.RelayStatementTimeout,
IdleInTransaction: settings.RelayProduceTimeout + settings.RelayStatementTimeout,
}
}