package config import ( "errors" "fmt" "strings" "time" ) const ( // The consumer runtime runs by default. It starts a consumer only for a // registered handler, and none is registered yet. defaultConsumerEnabled = true // defaultConsumerGroupPrefix is joined with a consumer name by a dot to // form the Kafka consumer group ID, for example "lincodex.". defaultConsumerGroupPrefix = "lincodex" // maxConsumerGroupPrefixLength leaves room for the consumer name in the // group ID, which the code limits to 64 characters. maxConsumerGroupPrefixLength = 128 // defaultConsumerMaxAttempts bounds the handler attempts for one event // before it is dead-lettered. defaultConsumerMaxAttempts = 5 maxConsumerMaxAttempts = 20 // The wait after a failed attempt starts at the initial delay, doubles // per consecutive failure, and stops growing at the maximum. defaultConsumerBackoffInitial = 500 * time.Millisecond defaultConsumerBackoffMax = 10 * time.Second maxConsumerBackoff = 5 * time.Minute // defaultConsumerHandlerTimeout bounds one transaction: the dedup marker, // the handler, and the commit. It keeps the worst case of one record // below the default rebalance timeout; see validateConsumerRebalance. defaultConsumerHandlerTimeout = 5 * time.Second minConsumerHandlerTimeout = 100 * time.Millisecond maxConsumerHandlerTimeout = 5 * time.Minute // defaultConsumerMaxPollRecords bounds the records of one poll, which a // consumer processes while it holds group rebalancing. defaultConsumerMaxPollRecords = 100 maxConsumerMaxPollRecords = 10000 // defaultConsumerCommitTimeout bounds one offset commit. defaultConsumerCommitTimeout = 5 * time.Second minConsumerCommitTimeout = 100 * time.Millisecond maxConsumerCommitTimeout = time.Minute // defaultConsumerShutdownGrace is how long the record in progress may // continue after shutdown begins. defaultConsumerShutdownGrace = 5 * time.Second // defaultConsumerRebalanceTimeout is the franz-go and Kafka default, // set explicitly so the budget check below uses the real value. defaultConsumerRebalanceTimeout = time.Minute minConsumerRebalanceTimeout = 10 * time.Second maxConsumerRebalanceTimeout = 30 * time.Minute // consumerRebalanceMargin covers the poll, the heartbeat that notices // the rebalance, and scheduling, on top of the worst case of one record. consumerRebalanceMargin = 5 * time.Second ) func (config *Config) loadConsumer(lookup LookupEnv) error { var err error if config.ConsumerEnabled, err = boolOrDefault(lookup, "CONSUMER_ENABLED", defaultConsumerEnabled); err != nil { return err } config.ConsumerGroupPrefix = strings.TrimSpace(valueOrDefault(lookup, "CONSUMER_GROUP_PREFIX", defaultConsumerGroupPrefix)) if config.ConsumerMaxAttempts, err = intOrDefault(lookup, "CONSUMER_MAX_ATTEMPTS", defaultConsumerMaxAttempts); err != nil { return err } if config.ConsumerBackoffInitial, err = durationOrDefault(lookup, "CONSUMER_BACKOFF_INITIAL", defaultConsumerBackoffInitial); err != nil { return err } if config.ConsumerBackoffMax, err = durationOrDefault(lookup, "CONSUMER_BACKOFF_MAX", defaultConsumerBackoffMax); err != nil { return err } if config.ConsumerHandlerTimeout, err = durationOrDefault(lookup, "CONSUMER_HANDLER_TIMEOUT", defaultConsumerHandlerTimeout); err != nil { return err } if config.ConsumerMaxPollRecords, err = intOrDefault(lookup, "CONSUMER_MAX_POLL_RECORDS", defaultConsumerMaxPollRecords); err != nil { return err } if config.ConsumerCommitTimeout, err = durationOrDefault(lookup, "CONSUMER_COMMIT_TIMEOUT", defaultConsumerCommitTimeout); err != nil { return err } if config.ConsumerShutdownGrace, err = durationOrDefault(lookup, "CONSUMER_SHUTDOWN_GRACE", defaultConsumerShutdownGrace); err != nil { return err } config.ConsumerRebalanceTimeout, err = durationOrDefault(lookup, "CONSUMER_REBALANCE_TIMEOUT", defaultConsumerRebalanceTimeout) return err } // ValidateConsumer checks the consumer settings even when consumers are // disabled, so enabling them later cannot surface an old mistake. func (config Config) ValidateConsumer() error { // A group ID allows any characters, but the prefix also appears in logs // and broker ACLs, so it follows the topic name rules. if len(config.ConsumerGroupPrefix) > maxConsumerGroupPrefixLength || !kafkaTopicNamePattern.MatchString(config.ConsumerGroupPrefix) { return fmt.Errorf("Config.ValidateConsumer - CONSUMER_GROUP_PREFIX must be 1 to %d letters, digits, dots, underscores, or hyphens", maxConsumerGroupPrefixLength) } if err := config.validateConsumerRetries(); err != nil { return err } if err := config.validateConsumerTimeouts(); err != nil { return err } return config.validateConsumerRebalance() } func (config Config) validateConsumerRetries() error { if config.ConsumerMaxAttempts < 1 || config.ConsumerMaxAttempts > maxConsumerMaxAttempts { return fmt.Errorf("Config.ValidateConsumer - CONSUMER_MAX_ATTEMPTS must be between 1 and %d", maxConsumerMaxAttempts) } if config.ConsumerBackoffInitial <= 0 { return errors.New("Config.ValidateConsumer - CONSUMER_BACKOFF_INITIAL must be positive") } if config.ConsumerBackoffMax < config.ConsumerBackoffInitial || config.ConsumerBackoffMax > maxConsumerBackoff { return fmt.Errorf("Config.ValidateConsumer - CONSUMER_BACKOFF_MAX must be between CONSUMER_BACKOFF_INITIAL and %s", maxConsumerBackoff) } if config.ConsumerMaxPollRecords < 1 || config.ConsumerMaxPollRecords > maxConsumerMaxPollRecords { return fmt.Errorf("Config.ValidateConsumer - CONSUMER_MAX_POLL_RECORDS must be between 1 and %d", maxConsumerMaxPollRecords) } return nil } func (config Config) validateConsumerTimeouts() error { if config.ConsumerHandlerTimeout < minConsumerHandlerTimeout || config.ConsumerHandlerTimeout > maxConsumerHandlerTimeout { return fmt.Errorf("Config.ValidateConsumer - CONSUMER_HANDLER_TIMEOUT must be between %s and %s", minConsumerHandlerTimeout, maxConsumerHandlerTimeout) } if config.ConsumerCommitTimeout < minConsumerCommitTimeout || config.ConsumerCommitTimeout > maxConsumerCommitTimeout { return fmt.Errorf("Config.ValidateConsumer - CONSUMER_COMMIT_TIMEOUT must be between %s and %s", minConsumerCommitTimeout, maxConsumerCommitTimeout) } // The grace runs while the HTTP servers drain, so it must fit in the // shutdown budget of the process. if config.ConsumerShutdownGrace <= 0 || config.ConsumerShutdownGrace > config.ShutdownTimeout { return errors.New("Config.ValidateConsumer - CONSUMER_SHUTDOWN_GRACE must be positive and at most SHUTDOWN_TIMEOUT") } return nil } // validateConsumerRebalance keeps the slowest record below the rebalance // timeout. A consumer holds group rebalancing while it processes a poll and // commits after every record that needed a retry, so one record is the unit // that must fit: every handler attempt, the longest wait before each retry, // the dead letter write, and the offset commit. A member that exceeds the // timeout is removed from the group, and its records are processed again. // Database outages are not part of the budget: they are retried without a // bound, and the removal they can cause is harmless because of the markers. func (config Config) validateConsumerRebalance() error { if config.ConsumerRebalanceTimeout < minConsumerRebalanceTimeout || config.ConsumerRebalanceTimeout > maxConsumerRebalanceTimeout { return fmt.Errorf("Config.ValidateConsumer - CONSUMER_REBALANCE_TIMEOUT must be between %s and %s", minConsumerRebalanceTimeout, maxConsumerRebalanceTimeout) } worstRecord := config.consumerRecordBudget() if worstRecord+consumerRebalanceMargin > config.ConsumerRebalanceTimeout { return fmt.Errorf("Config.ValidateConsumer - CONSUMER_REBALANCE_TIMEOUT must exceed the worst case of one record (%s) by %s; lower CONSUMER_MAX_ATTEMPTS, CONSUMER_HANDLER_TIMEOUT, CONSUMER_BACKOFF_MAX, or CONSUMER_COMMIT_TIMEOUT, or raise the timeout", worstRecord, consumerRebalanceMargin) } return nil } // consumerRecordBudget is the longest time one record can take without a // database outage: CONSUMER_MAX_ATTEMPTS handler attempts, the longest // backoff before each retry, one dead letter write, and one offset commit. // The backoff ceiling starts at CONSUMER_BACKOFF_INITIAL and doubles up to // CONSUMER_BACKOFF_MAX, as the workers' retry.Backoff does. func (config Config) consumerRecordBudget() time.Duration { attempts := config.ConsumerMaxAttempts budget := time.Duration(attempts+1) * config.ConsumerHandlerTimeout ceiling := min(config.ConsumerBackoffInitial, config.ConsumerBackoffMax) for retry := 1; retry < attempts; retry++ { budget += ceiling ceiling = min(ceiling*2, config.ConsumerBackoffMax) } return budget + config.ConsumerCommitTimeout }