Files
lincodex/backend/config/relay.go
T

91 lines
4.1 KiB
Go

package config
import (
"errors"
"fmt"
"time"
)
const (
// The relay runs by default so committed events reach Kafka without an
// extra switch. It still needs DATABASE_URL; without it bootstrap skips
// the relay with a warning.
defaultRelayEnabled = true
// defaultRelayBatchSize bounds the events one pass locks and publishes in
// one transaction.
defaultRelayBatchSize = 100
// maxRelayBatchSize keeps a pass, and the time its transaction holds row
// locks, short.
maxRelayBatchSize = 1000
// defaultRelayPollInterval is the wait after a pass that found nothing.
defaultRelayPollInterval = time.Second
minRelayPollInterval = 10 * time.Millisecond
maxRelayPollInterval = 5 * time.Minute
// defaultRelayProduceTimeout bounds one publish, during which the pass
// holds its transaction open.
defaultRelayProduceTimeout = 10 * time.Second
// minRelayProduceTimeout is the smallest record delivery timeout that
// franz-go accepts.
minRelayProduceTimeout = time.Second
maxRelayProduceTimeout = 2 * time.Minute
// defaultRelayStatementTimeout bounds each SQL statement of a pass. The
// claim reads one batch through the partial indexes and the mark updates
// at most one batch, so a healthy database answers well below it.
defaultRelayStatementTimeout = 5 * time.Second
minRelayStatementTimeout = 100 * time.Millisecond
maxRelayStatementTimeout = time.Minute
// The backoff after a failed pass starts at the initial delay, doubles
// per consecutive failure, and stops growing at the maximum.
defaultRelayBackoffInitial = 500 * time.Millisecond
defaultRelayBackoffMax = 30 * time.Second
maxRelayBackoff = 10 * time.Minute
)
func (config *Config) loadRelay(lookup LookupEnv) error {
var err error
if config.RelayEnabled, err = boolOrDefault(lookup, "RELAY_ENABLED", defaultRelayEnabled); err != nil {
return err
}
if config.RelayBatchSize, err = intOrDefault(lookup, "RELAY_BATCH_SIZE", defaultRelayBatchSize); err != nil {
return err
}
if config.RelayPollInterval, err = durationOrDefault(lookup, "RELAY_POLL_INTERVAL", defaultRelayPollInterval); err != nil {
return err
}
if config.RelayProduceTimeout, err = durationOrDefault(lookup, "RELAY_PRODUCE_TIMEOUT", defaultRelayProduceTimeout); err != nil {
return err
}
if config.RelayStatementTimeout, err = durationOrDefault(lookup, "RELAY_STATEMENT_TIMEOUT", defaultRelayStatementTimeout); err != nil {
return err
}
if config.RelayBackoffInitial, err = durationOrDefault(lookup, "RELAY_BACKOFF_INITIAL", defaultRelayBackoffInitial); err != nil {
return err
}
config.RelayBackoffMax, err = durationOrDefault(lookup, "RELAY_BACKOFF_MAX", defaultRelayBackoffMax)
return err
}
// ValidateRelay checks the relay settings even when the relay is disabled, so
// enabling it later cannot surface an old mistake.
func (config Config) ValidateRelay() error {
if config.RelayBatchSize < 1 || config.RelayBatchSize > maxRelayBatchSize {
return fmt.Errorf("Config.ValidateRelay - RELAY_BATCH_SIZE must be between 1 and %d", maxRelayBatchSize)
}
if config.RelayPollInterval < minRelayPollInterval || config.RelayPollInterval > maxRelayPollInterval {
return fmt.Errorf("Config.ValidateRelay - RELAY_POLL_INTERVAL must be between %s and %s", minRelayPollInterval, maxRelayPollInterval)
}
if config.RelayProduceTimeout < minRelayProduceTimeout || config.RelayProduceTimeout > maxRelayProduceTimeout {
return fmt.Errorf("Config.ValidateRelay - RELAY_PRODUCE_TIMEOUT must be between %s and %s", minRelayProduceTimeout, maxRelayProduceTimeout)
}
if config.RelayStatementTimeout < minRelayStatementTimeout || config.RelayStatementTimeout > maxRelayStatementTimeout {
return fmt.Errorf("Config.ValidateRelay - RELAY_STATEMENT_TIMEOUT must be between %s and %s", minRelayStatementTimeout, maxRelayStatementTimeout)
}
if config.RelayBackoffInitial <= 0 {
return errors.New("Config.ValidateRelay - RELAY_BACKOFF_INITIAL must be positive")
}
if config.RelayBackoffMax < config.RelayBackoffInitial || config.RelayBackoffMax > maxRelayBackoff {
return fmt.Errorf("Config.ValidateRelay - RELAY_BACKOFF_MAX must be between RELAY_BACKOFF_INITIAL and %s", maxRelayBackoff)
}
return nil
}