fix: enforce pipeline lifecycle limits
This commit is contained in:
+72
-38
@@ -2,6 +2,7 @@ package config
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net"
|
||||
"time"
|
||||
|
||||
@@ -26,6 +27,9 @@ type DispatcherConfig struct {
|
||||
PollInterval time.Duration
|
||||
RecoveryInterval time.Duration
|
||||
RetryDelay time.Duration
|
||||
Retention time.Duration
|
||||
CleanupInterval time.Duration
|
||||
CleanupBatchSize int
|
||||
MetricsAddress string
|
||||
}
|
||||
|
||||
@@ -52,6 +56,7 @@ type WorkerConfig struct {
|
||||
Concurrency int
|
||||
Prefetch int
|
||||
LeaseDuration time.Duration
|
||||
HeartbeatInterval time.Duration
|
||||
ProcessingTimeout time.Duration
|
||||
ReadBlock time.Duration
|
||||
ReclaimInterval time.Duration
|
||||
@@ -83,8 +88,10 @@ type brokerConfig struct {
|
||||
}
|
||||
|
||||
func LoadJobAdmin() (JobAdminConfig, error) {
|
||||
values := viper.New()
|
||||
values.AutomaticEnv()
|
||||
values, err := loadValues()
|
||||
if err != nil {
|
||||
return JobAdminConfig{}, err
|
||||
}
|
||||
database, err := loadDatabaseConfig(values)
|
||||
if err != nil {
|
||||
return JobAdminConfig{}, err
|
||||
@@ -97,8 +104,10 @@ func LoadJobAdmin() (JobAdminConfig, error) {
|
||||
}
|
||||
|
||||
func LoadDispatcher() (DispatcherConfig, error) {
|
||||
values := viper.New()
|
||||
values.AutomaticEnv()
|
||||
values, err := loadValues()
|
||||
if err != nil {
|
||||
return DispatcherConfig{}, err
|
||||
}
|
||||
database, err := loadDatabaseConfig(values)
|
||||
if err != nil {
|
||||
return DispatcherConfig{}, err
|
||||
@@ -107,23 +116,35 @@ func LoadDispatcher() (DispatcherConfig, error) {
|
||||
if err != nil {
|
||||
return DispatcherConfig{}, err
|
||||
}
|
||||
batchSize, err := integer(values, "OUTBOX_BATCH_SIZE", 1, 1000)
|
||||
batchSize, err := integer(values, "outbox.batch_size", 1, 1000)
|
||||
if err != nil {
|
||||
return DispatcherConfig{}, err
|
||||
}
|
||||
leaseDuration, err := seconds(values, "OUTBOX_LEASE_SECONDS", 1, 600)
|
||||
leaseDuration, err := duration(values, "outbox.lease", time.Second, 10*time.Minute)
|
||||
if err != nil {
|
||||
return DispatcherConfig{}, err
|
||||
}
|
||||
pollInterval, err := seconds(values, "OUTBOX_POLL_SECONDS", 1, 60)
|
||||
pollInterval, err := duration(values, "outbox.poll_interval", time.Millisecond, time.Minute)
|
||||
if err != nil {
|
||||
return DispatcherConfig{}, err
|
||||
}
|
||||
recoveryInterval, err := seconds(values, "OUTBOX_RECOVERY_SECONDS", 1, 3600)
|
||||
recoveryInterval, err := duration(values, "outbox.recovery_interval", time.Second, time.Hour)
|
||||
if err != nil {
|
||||
return DispatcherConfig{}, err
|
||||
}
|
||||
retryDelay, err := seconds(values, "OUTBOX_RETRY_SECONDS", 1, 3600)
|
||||
retryDelay, err := duration(values, "outbox.retry_delay", time.Second, time.Hour)
|
||||
if err != nil {
|
||||
return DispatcherConfig{}, err
|
||||
}
|
||||
retention, err := duration(values, "outbox.retention", time.Hour, 365*24*time.Hour)
|
||||
if err != nil {
|
||||
return DispatcherConfig{}, err
|
||||
}
|
||||
cleanupInterval, err := duration(values, "outbox.cleanup_interval", time.Minute, 24*time.Hour)
|
||||
if err != nil {
|
||||
return DispatcherConfig{}, err
|
||||
}
|
||||
cleanupBatchSize, err := integer(values, "outbox.cleanup_batch_size", 1, 1000)
|
||||
if err != nil {
|
||||
return DispatcherConfig{}, err
|
||||
}
|
||||
@@ -138,13 +159,17 @@ func LoadDispatcher() (DispatcherConfig, error) {
|
||||
BrokerURL: broker.url, Stream: broker.stream, ConsumerGroup: broker.consumerGroup,
|
||||
FailedStream: broker.failedStream, FailedStreamMaxLength: int64(broker.failedStreamMaxLength),
|
||||
BatchSize: batchSize, LeaseDuration: leaseDuration, PollInterval: pollInterval,
|
||||
RecoveryInterval: recoveryInterval, RetryDelay: retryDelay, MetricsAddress: metricsAddress,
|
||||
RecoveryInterval: recoveryInterval, RetryDelay: retryDelay,
|
||||
Retention: retention, CleanupInterval: cleanupInterval, CleanupBatchSize: cleanupBatchSize,
|
||||
MetricsAddress: metricsAddress,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func LoadWorker() (WorkerConfig, error) {
|
||||
values := viper.New()
|
||||
values.AutomaticEnv()
|
||||
values, err := loadValues()
|
||||
if err != nil {
|
||||
return WorkerConfig{}, err
|
||||
}
|
||||
database, err := loadDatabaseConfig(values)
|
||||
if err != nil {
|
||||
return WorkerConfig{}, err
|
||||
@@ -157,57 +182,64 @@ func LoadWorker() (WorkerConfig, error) {
|
||||
if err != nil {
|
||||
return WorkerConfig{}, err
|
||||
}
|
||||
concurrency, err := integer(values, "WORKER_CONCURRENCY", 1, 64)
|
||||
concurrency, err := integer(values, "worker.concurrency", 1, 64)
|
||||
if err != nil {
|
||||
return WorkerConfig{}, err
|
||||
}
|
||||
prefetch, err := integer(values, "WORKER_PREFETCH", concurrency, 1024)
|
||||
prefetch, err := integer(values, "worker.prefetch", concurrency, 1024)
|
||||
if err != nil {
|
||||
return WorkerConfig{}, err
|
||||
}
|
||||
leaseDuration, err := seconds(values, "WORKER_LEASE_SECONDS", 1, 3600)
|
||||
leaseDuration, err := duration(values, "worker.lease", time.Second, time.Hour)
|
||||
if err != nil {
|
||||
return WorkerConfig{}, err
|
||||
}
|
||||
processingTimeout, err := seconds(values, "WORKER_PROCESSING_TIMEOUT_SECONDS", 1, 3600)
|
||||
heartbeatInterval, err := duration(values, "worker.heartbeat_interval", 100*time.Millisecond, time.Hour)
|
||||
if err != nil {
|
||||
return WorkerConfig{}, err
|
||||
}
|
||||
if heartbeatInterval >= leaseDuration {
|
||||
return WorkerConfig{}, errors.New("worker.heartbeat_interval must be less than worker.lease")
|
||||
}
|
||||
processingTimeout, err := duration(values, "worker.processing_timeout", time.Second, time.Hour)
|
||||
if err != nil {
|
||||
return WorkerConfig{}, err
|
||||
}
|
||||
if processingTimeout >= leaseDuration {
|
||||
return WorkerConfig{}, errors.New("WORKER_PROCESSING_TIMEOUT_SECONDS must be less than WORKER_LEASE_SECONDS")
|
||||
return WorkerConfig{}, errors.New("worker.processing_timeout must be less than worker.lease")
|
||||
}
|
||||
readBlock, err := seconds(values, "WORKER_READ_BLOCK_SECONDS", 1, 60)
|
||||
readBlock, err := duration(values, "worker.read_block", 100*time.Millisecond, time.Minute)
|
||||
if err != nil {
|
||||
return WorkerConfig{}, err
|
||||
}
|
||||
reclaimInterval, err := seconds(values, "WORKER_RECLAIM_SECONDS", 1, 3600)
|
||||
reclaimInterval, err := duration(values, "worker.reclaim_interval", time.Second, time.Hour)
|
||||
if err != nil {
|
||||
return WorkerConfig{}, err
|
||||
}
|
||||
maxAttempts, err := integer(values, "WORKER_MAX_ATTEMPTS", 1, 100)
|
||||
maxAttempts, err := integer(values, "worker.max_attempts", 1, 100)
|
||||
if err != nil {
|
||||
return WorkerConfig{}, err
|
||||
}
|
||||
retryBase, err := seconds(values, "WORKER_RETRY_BASE_SECONDS", 1, 3600)
|
||||
retryBase, err := duration(values, "worker.retry.base", time.Second, time.Hour)
|
||||
if err != nil {
|
||||
return WorkerConfig{}, err
|
||||
}
|
||||
retryMax, err := seconds(values, "WORKER_RETRY_MAX_SECONDS", 1, 86400)
|
||||
retryMax, err := duration(values, "worker.retry.max", time.Second, 24*time.Hour)
|
||||
if err != nil {
|
||||
return WorkerConfig{}, err
|
||||
}
|
||||
if retryMax < retryBase {
|
||||
return WorkerConfig{}, errors.New("WORKER_RETRY_MAX_SECONDS must be at least WORKER_RETRY_BASE_SECONDS")
|
||||
return WorkerConfig{}, errors.New("worker.retry.max must be at least worker.retry.base")
|
||||
}
|
||||
attachmentMaxBytes, err := integer(values, "ATTACHMENT_MAX_BYTES", 1, 10*1024*1024)
|
||||
attachmentMaxBytes, err := integer(values, "attachments.max_bytes", 1, 10*1024*1024)
|
||||
if err != nil {
|
||||
return WorkerConfig{}, err
|
||||
}
|
||||
attachmentMaxPixels, err := integer(values, "ATTACHMENT_MAX_PIXELS", 1, 40_000_000)
|
||||
attachmentMaxPixels, err := integer(values, "attachments.max_pixels", 1, 40_000_000)
|
||||
if err != nil {
|
||||
return WorkerConfig{}, err
|
||||
}
|
||||
memoryBytes, err := integer(values, "WORKER_MEMORY_BYTES", attachmentMaxBytes, 2*1024*1024*1024)
|
||||
memoryBytes, err := integer(values, "worker.memory_bytes", attachmentMaxBytes, 2*1024*1024*1024)
|
||||
if err != nil {
|
||||
return WorkerConfig{}, err
|
||||
}
|
||||
@@ -225,44 +257,46 @@ func LoadWorker() (WorkerConfig, error) {
|
||||
ConsumerGroup: broker.consumerGroup, FailedStream: broker.failedStream,
|
||||
FailedStreamMaxLength: int64(broker.failedStreamMaxLength),
|
||||
Concurrency: concurrency, Prefetch: prefetch, LeaseDuration: leaseDuration,
|
||||
ProcessingTimeout: processingTimeout, ReadBlock: readBlock,
|
||||
HeartbeatInterval: heartbeatInterval, ProcessingTimeout: processingTimeout, ReadBlock: readBlock,
|
||||
ReclaimInterval: reclaimInterval, MaxAttempts: maxAttempts, RetryBase: retryBase, RetryMax: retryMax,
|
||||
AttachmentMaxBytes: int64(attachmentMaxBytes), AttachmentMaxPixels: int64(attachmentMaxPixels),
|
||||
MemoryBytes: int64(memoryBytes),
|
||||
MetricsAddress: metricsAddress,
|
||||
MemoryBytes: int64(memoryBytes), MetricsAddress: metricsAddress,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func metricsAddress(values *viper.Viper) (string, error) {
|
||||
host, err := requiredString(values, "PIPELINE_METRICS_HOST")
|
||||
address, err := requiredString(values, "monitoring.metrics_address")
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
port, err := requiredPort(values, "PIPELINE_METRICS_PORT")
|
||||
_, port, err := net.SplitHostPort(address)
|
||||
if err != nil {
|
||||
return "", err
|
||||
return "", fmt.Errorf("monitoring.metrics_address: %w", err)
|
||||
}
|
||||
return net.JoinHostPort(host, port), nil
|
||||
if _, err := integerValue(port, 1, 65535); err != nil {
|
||||
return "", fmt.Errorf("monitoring.metrics_address: %w", err)
|
||||
}
|
||||
return address, nil
|
||||
}
|
||||
|
||||
func loadBrokerConfig(values *viper.Viper) (brokerConfig, error) {
|
||||
url, err := requiredString(values, "BROKER_URL")
|
||||
url, err := requiredString(values, "broker.url")
|
||||
if err != nil {
|
||||
return brokerConfig{}, err
|
||||
}
|
||||
stream, err := requiredString(values, "BROKER_STREAM")
|
||||
stream, err := requiredString(values, "broker.stream")
|
||||
if err != nil {
|
||||
return brokerConfig{}, err
|
||||
}
|
||||
consumerGroup, err := requiredString(values, "BROKER_CONSUMER_GROUP")
|
||||
consumerGroup, err := requiredString(values, "broker.consumer_group")
|
||||
if err != nil {
|
||||
return brokerConfig{}, err
|
||||
}
|
||||
failedStream, err := requiredString(values, "BROKER_FAILED_STREAM")
|
||||
failedStream, err := requiredString(values, "broker.failed_stream")
|
||||
if err != nil {
|
||||
return brokerConfig{}, err
|
||||
}
|
||||
failedStreamMaxLength, err := integer(values, "BROKER_FAILED_MAX_LENGTH", 100, 10_000_000)
|
||||
failedStreamMaxLength, err := integer(values, "broker.failed_stream_max_length", 100, 10_000_000)
|
||||
if err != nil {
|
||||
return brokerConfig{}, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user