forked from mtusi2026/mtusi2026lab5
fix: enforce pipeline lifecycle limits
This commit is contained in:
+61
-51
@@ -98,14 +98,16 @@ type objectStorageConfig struct {
|
||||
}
|
||||
|
||||
func LoadServer() (ServerConfig, error) {
|
||||
values := viper.New()
|
||||
values.AutomaticEnv()
|
||||
|
||||
host, err := requiredString(values, "APP_HOST")
|
||||
values, err := loadValues()
|
||||
if err != nil {
|
||||
return ServerConfig{}, err
|
||||
}
|
||||
port, err := requiredPort(values, "APP_PORT")
|
||||
|
||||
host, err := requiredString(values, "server.host")
|
||||
if err != nil {
|
||||
return ServerConfig{}, err
|
||||
}
|
||||
port, err := requiredPort(values, "server.port")
|
||||
if err != nil {
|
||||
return ServerConfig{}, err
|
||||
}
|
||||
@@ -113,71 +115,71 @@ func LoadServer() (ServerConfig, error) {
|
||||
if err != nil {
|
||||
return ServerConfig{}, err
|
||||
}
|
||||
cacheURL, err := requiredString(values, "CACHE_URL")
|
||||
cacheURL, err := requiredString(values, "cache.url")
|
||||
if err != nil {
|
||||
return ServerConfig{}, err
|
||||
}
|
||||
store, err := requiredString(values, "SESSION_STORE")
|
||||
store, err := requiredString(values, "server.session_store")
|
||||
if err != nil {
|
||||
return ServerConfig{}, err
|
||||
}
|
||||
if store != "valkey" {
|
||||
return ServerConfig{}, errors.New("SESSION_STORE must be valkey")
|
||||
return ServerConfig{}, errors.New("server.session_store must be valkey")
|
||||
}
|
||||
|
||||
sessionTTL, err := seconds(values, "SESSION_TTL_SECONDS", 1, 86400*30)
|
||||
sessionTTL, err := duration(values, "server.session_ttl", time.Second, 30*24*time.Hour)
|
||||
if err != nil {
|
||||
return ServerConfig{}, err
|
||||
}
|
||||
idempotencyTTL, err := seconds(values, "IDEMPOTENCY_TTL_SECONDS", 86400, 86400*30)
|
||||
idempotencyTTL, err := duration(values, "server.idempotency_ttl", 24*time.Hour, 30*24*time.Hour)
|
||||
if err != nil {
|
||||
return ServerConfig{}, err
|
||||
}
|
||||
requestTimeout, err := seconds(values, "REQUEST_TIMEOUT_SECONDS", 1, 300)
|
||||
requestTimeout, err := duration(values, "server.request_timeout", time.Second, 5*time.Minute)
|
||||
if err != nil {
|
||||
return ServerConfig{}, err
|
||||
}
|
||||
readHeaderTimeout, err := seconds(values, "HTTP_READ_HEADER_TIMEOUT_SECONDS", 1, 300)
|
||||
readHeaderTimeout, err := duration(values, "server.http.read_header_timeout", time.Second, 5*time.Minute)
|
||||
if err != nil {
|
||||
return ServerConfig{}, err
|
||||
}
|
||||
readTimeout, err := seconds(values, "HTTP_READ_TIMEOUT_SECONDS", 1, 300)
|
||||
readTimeout, err := duration(values, "server.http.read_timeout", time.Second, 5*time.Minute)
|
||||
if err != nil {
|
||||
return ServerConfig{}, err
|
||||
}
|
||||
writeTimeout, err := seconds(values, "HTTP_WRITE_TIMEOUT_SECONDS", 1, 300)
|
||||
writeTimeout, err := duration(values, "server.http.write_timeout", time.Second, 5*time.Minute)
|
||||
if err != nil {
|
||||
return ServerConfig{}, err
|
||||
}
|
||||
idleTimeout, err := seconds(values, "HTTP_IDLE_TIMEOUT_SECONDS", 1, 3600)
|
||||
idleTimeout, err := duration(values, "server.http.idle_timeout", time.Second, time.Hour)
|
||||
if err != nil {
|
||||
return ServerConfig{}, err
|
||||
}
|
||||
shutdownTimeout, err := seconds(values, "HTTP_SHUTDOWN_TIMEOUT_SECONDS", 1, 300)
|
||||
shutdownTimeout, err := duration(values, "server.http.shutdown_timeout", time.Second, 5*time.Minute)
|
||||
if err != nil {
|
||||
return ServerConfig{}, err
|
||||
}
|
||||
passwordMemory, err := integer(values, "PASSWORD_MEMORY_KIB", 8*1024, 1024*1024)
|
||||
passwordMemory, err := integer(values, "server.password.memory_kib", 8*1024, 1024*1024)
|
||||
if err != nil {
|
||||
return ServerConfig{}, err
|
||||
}
|
||||
passwordIterations, err := integer(values, "PASSWORD_ITERATIONS", 1, 20)
|
||||
passwordIterations, err := integer(values, "server.password.iterations", 1, 20)
|
||||
if err != nil {
|
||||
return ServerConfig{}, err
|
||||
}
|
||||
passwordParallelism, err := integer(values, "PASSWORD_PARALLELISM", 1, 255)
|
||||
passwordParallelism, err := integer(values, "server.password.parallelism", 1, 255)
|
||||
if err != nil {
|
||||
return ServerConfig{}, err
|
||||
}
|
||||
passwordSaltLength, err := integer(values, "PASSWORD_SALT_LENGTH", 8, 1024)
|
||||
passwordSaltLength, err := integer(values, "server.password.salt_length", 8, 1024)
|
||||
if err != nil {
|
||||
return ServerConfig{}, err
|
||||
}
|
||||
passwordKeyLength, err := integer(values, "PASSWORD_KEY_LENGTH", 16, 1024)
|
||||
passwordKeyLength, err := integer(values, "server.password.key_length", 16, 1024)
|
||||
if err != nil {
|
||||
return ServerConfig{}, err
|
||||
}
|
||||
swaggerPort, err := requiredPort(values, "SWAGGER_PORT")
|
||||
swaggerPort, err := requiredPort(values, "server.swagger_port")
|
||||
if err != nil {
|
||||
return ServerConfig{}, err
|
||||
}
|
||||
@@ -185,30 +187,30 @@ func LoadServer() (ServerConfig, error) {
|
||||
if err != nil {
|
||||
return ServerConfig{}, err
|
||||
}
|
||||
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 ServerConfig{}, 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 ServerConfig{}, err
|
||||
}
|
||||
attachmentBodyMemory, err := integer(values, "ATTACHMENT_BODY_MEMORY_BYTES", 1, 1024*1024*1024)
|
||||
attachmentBodyMemory, err := integer(values, "attachments.body_memory_bytes", 1, 1024*1024*1024)
|
||||
if err != nil {
|
||||
return ServerConfig{}, err
|
||||
}
|
||||
if int64(attachmentBodyMemory) < int64(attachmentMaxBytes) {
|
||||
return ServerConfig{}, fmt.Errorf("ATTACHMENT_BODY_MEMORY_BYTES: must be at least %d", attachmentMaxBytes)
|
||||
return ServerConfig{}, fmt.Errorf("attachments.body_memory_bytes: must be at least %d", attachmentMaxBytes)
|
||||
}
|
||||
attachmentDecodeMemory, err := integer(values, "ATTACHMENT_DECODE_MEMORY_BYTES", 1, 1024*1024*1024)
|
||||
attachmentDecodeMemory, err := integer(values, "attachments.decode_memory_bytes", 1, 1024*1024*1024)
|
||||
if err != nil {
|
||||
return ServerConfig{}, err
|
||||
}
|
||||
minimumDecodeMemory := int64(attachmentMaxPixels) * 8
|
||||
if int64(attachmentDecodeMemory) < minimumDecodeMemory {
|
||||
return ServerConfig{}, fmt.Errorf("ATTACHMENT_DECODE_MEMORY_BYTES: must be at least %d", minimumDecodeMemory)
|
||||
return ServerConfig{}, fmt.Errorf("attachments.decode_memory_bytes: must be at least %d", minimumDecodeMemory)
|
||||
}
|
||||
pipelineVersion, err := integer(values, "PIPELINE_VERSION", 1, 1000)
|
||||
pipelineVersion, err := integer(values, "pipeline.version", 1, 1000)
|
||||
if err != nil {
|
||||
return ServerConfig{}, err
|
||||
}
|
||||
@@ -236,13 +238,15 @@ func LoadServer() (ServerConfig, error) {
|
||||
}
|
||||
|
||||
func LoadMigration() (MigrationConfig, error) {
|
||||
values := viper.New()
|
||||
values.AutomaticEnv()
|
||||
values, err := loadValues()
|
||||
if err != nil {
|
||||
return MigrationConfig{}, err
|
||||
}
|
||||
database, err := loadDatabaseConfig(values)
|
||||
if err != nil {
|
||||
return MigrationConfig{}, err
|
||||
}
|
||||
migrationTimeout, err := seconds(values, "MIGRATION_TIMEOUT_SECONDS", 1, 600)
|
||||
migrationTimeout, err := duration(values, "migration.timeout", time.Second, 10*time.Minute)
|
||||
if err != nil {
|
||||
return MigrationConfig{}, err
|
||||
}
|
||||
@@ -255,8 +259,10 @@ func LoadMigration() (MigrationConfig, error) {
|
||||
}
|
||||
|
||||
func LoadReconciliation() (ReconciliationConfig, error) {
|
||||
values := viper.New()
|
||||
values.AutomaticEnv()
|
||||
values, err := loadValues()
|
||||
if err != nil {
|
||||
return ReconciliationConfig{}, err
|
||||
}
|
||||
database, err := loadDatabaseConfig(values)
|
||||
if err != nil {
|
||||
return ReconciliationConfig{}, err
|
||||
@@ -276,31 +282,31 @@ func LoadReconciliation() (ReconciliationConfig, error) {
|
||||
}
|
||||
|
||||
func loadObjectStorageConfig(values *viper.Viper) (objectStorageConfig, error) {
|
||||
endpoint, err := requiredString(values, "S3_ENDPOINT")
|
||||
endpoint, err := requiredString(values, "object_storage.endpoint")
|
||||
if err != nil {
|
||||
return objectStorageConfig{}, err
|
||||
}
|
||||
region, err := requiredString(values, "S3_REGION")
|
||||
region, err := requiredString(values, "object_storage.region")
|
||||
if err != nil {
|
||||
return objectStorageConfig{}, err
|
||||
}
|
||||
bucket, err := requiredString(values, "S3_BUCKET")
|
||||
bucket, err := requiredString(values, "object_storage.bucket")
|
||||
if err != nil {
|
||||
return objectStorageConfig{}, err
|
||||
}
|
||||
accessKey, err := requiredString(values, "S3_ACCESS_KEY")
|
||||
accessKey, err := requiredString(values, "object_storage.access_key")
|
||||
if err != nil {
|
||||
return objectStorageConfig{}, err
|
||||
}
|
||||
secretKey, err := requiredString(values, "S3_SECRET_KEY")
|
||||
secretKey, err := requiredString(values, "object_storage.secret_key")
|
||||
if err != nil {
|
||||
return objectStorageConfig{}, err
|
||||
}
|
||||
useTLS, err := boolean(values, "S3_USE_TLS")
|
||||
useTLS, err := boolean(values, "object_storage.use_tls")
|
||||
if err != nil {
|
||||
return objectStorageConfig{}, err
|
||||
}
|
||||
timeout, err := seconds(values, "S3_TIMEOUT_SECONDS", 1, 300)
|
||||
timeout, err := duration(values, "object_storage.timeout", time.Second, 5*time.Minute)
|
||||
if err != nil {
|
||||
return objectStorageConfig{}, err
|
||||
}
|
||||
@@ -311,31 +317,31 @@ func loadObjectStorageConfig(values *viper.Viper) (objectStorageConfig, error) {
|
||||
}
|
||||
|
||||
func loadDatabaseConfig(values *viper.Viper) (databaseConfig, error) {
|
||||
databaseURL, err := requiredString(values, "DATABASE_URL")
|
||||
databaseURL, err := requiredString(values, "database.url")
|
||||
if err != nil {
|
||||
return databaseConfig{}, err
|
||||
}
|
||||
maxConns, err := integer(values, "DB_MAX_CONNS", 1, 100)
|
||||
maxConns, err := integer(values, "database.max_connections", 1, 100)
|
||||
if err != nil {
|
||||
return databaseConfig{}, err
|
||||
}
|
||||
minConns, err := integer(values, "DB_MIN_CONNS", 0, maxConns)
|
||||
minConns, err := integer(values, "database.min_connections", 0, maxConns)
|
||||
if err != nil {
|
||||
return databaseConfig{}, err
|
||||
}
|
||||
maxConnectionAge, err := seconds(values, "DB_MAX_CONNECTION_AGE_SECONDS", 1, 86400)
|
||||
maxConnectionAge, err := duration(values, "database.max_connection_age", time.Second, 24*time.Hour)
|
||||
if err != nil {
|
||||
return databaseConfig{}, err
|
||||
}
|
||||
maxConnectionIdle, err := seconds(values, "DB_MAX_CONNECTION_IDLE_SECONDS", 1, 86400)
|
||||
maxConnectionIdle, err := duration(values, "database.max_connection_idle", time.Second, 24*time.Hour)
|
||||
if err != nil {
|
||||
return databaseConfig{}, err
|
||||
}
|
||||
healthCheckPeriod, err := seconds(values, "DB_HEALTH_CHECK_SECONDS", 1, 3600)
|
||||
healthCheckPeriod, err := duration(values, "database.health_check_period", time.Second, time.Hour)
|
||||
if err != nil {
|
||||
return databaseConfig{}, err
|
||||
}
|
||||
dependencyTimeout, err := seconds(values, "DEPENDENCY_TIMEOUT_SECONDS", 1, 60)
|
||||
dependencyTimeout, err := duration(values, "database.dependency_timeout", time.Second, time.Minute)
|
||||
if err != nil {
|
||||
return databaseConfig{}, err
|
||||
}
|
||||
@@ -365,12 +371,16 @@ func requiredPort(values *viper.Viper, name string) (string, error) {
|
||||
return value, nil
|
||||
}
|
||||
|
||||
func seconds(values *viper.Viper, name string, minimum, maximum int) (time.Duration, error) {
|
||||
value, err := integer(values, name, minimum, maximum)
|
||||
func duration(values *viper.Viper, name string, minimum, maximum time.Duration) (time.Duration, error) {
|
||||
value, err := requiredString(values, name)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return time.Duration(value) * time.Second, nil
|
||||
parsed, err := time.ParseDuration(value)
|
||||
if err != nil || parsed < minimum || parsed > maximum {
|
||||
return 0, fmt.Errorf("%s: must be between %s and %s", name, minimum, maximum)
|
||||
}
|
||||
return parsed, nil
|
||||
}
|
||||
|
||||
func integer(values *viper.Viper, name string, minimum, maximum int) (int, error) {
|
||||
|
||||
+114
-12
@@ -1,24 +1,17 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestLoadReconciliationDoesNotRequireServerSettings(t *testing.T) {
|
||||
values := map[string]string{
|
||||
"DATABASE_URL": "postgres://user:password@postgres:5432/messenger",
|
||||
"DB_MIN_CONNS": "0", "DB_MAX_CONNS": "4",
|
||||
"DB_MAX_CONNECTION_AGE_SECONDS": "300", "DB_MAX_CONNECTION_IDLE_SECONDS": "60",
|
||||
"DB_HEALTH_CHECK_SECONDS": "30", "DEPENDENCY_TIMEOUT_SECONDS": "5",
|
||||
"S3_ENDPOINT": "s3:9000", "S3_REGION": "us-east-1", "S3_BUCKET": "attachments",
|
||||
"S3_ACCESS_KEY": "application", "S3_SECRET_KEY": "application-secret",
|
||||
"S3_USE_TLS": "false", "S3_TIMEOUT_SECONDS": "5",
|
||||
}
|
||||
for name, value := range values {
|
||||
t.Setenv(name, value)
|
||||
}
|
||||
setConfigEnvironment(t, validTestConfig)
|
||||
t.Setenv("APP_HOST", "")
|
||||
t.Setenv("CACHE_URL", "")
|
||||
t.Setenv("SWAGGER_PORT", "")
|
||||
@@ -27,3 +20,112 @@ func TestLoadReconciliationDoesNotRequireServerSettings(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, "attachments", settings.S3Bucket)
|
||||
}
|
||||
|
||||
func TestLoadWorkerUsesYAMLForRuntimeSettings(t *testing.T) {
|
||||
setConfigEnvironment(t, validTestConfig)
|
||||
t.Setenv("WORKER_CONCURRENCY", "64")
|
||||
|
||||
settings, err := LoadWorker()
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, 2, settings.Concurrency)
|
||||
require.Equal(t, 5*time.Second, settings.HeartbeatInterval)
|
||||
}
|
||||
|
||||
func TestLoadWorkerRejectsHeartbeatAtLeaseBoundary(t *testing.T) {
|
||||
content := strings.Replace(validTestConfig, "heartbeat_interval: 5s", "heartbeat_interval: 30s", 1)
|
||||
setConfigEnvironment(t, content)
|
||||
|
||||
_, err := LoadWorker()
|
||||
require.ErrorContains(t, err, "heartbeat_interval must be less than worker.lease")
|
||||
}
|
||||
|
||||
func TestLoadValuesRejectsUnknownVersion(t *testing.T) {
|
||||
setConfigEnvironment(t, "version: 2\n")
|
||||
|
||||
_, err := loadValues()
|
||||
require.ErrorContains(t, err, "version: must be 1")
|
||||
}
|
||||
|
||||
func setConfigEnvironment(t *testing.T, content string) {
|
||||
t.Helper()
|
||||
path := filepath.Join(t.TempDir(), "messenger.yaml")
|
||||
require.NoError(t, os.WriteFile(path, []byte(content), 0o600))
|
||||
t.Setenv("MESSENGER_CONFIG_PATH", path)
|
||||
for name, value := range map[string]string{
|
||||
"APP_HOST": "0.0.0.0", "APP_PORT": "8080", "SWAGGER_PORT": "8081",
|
||||
"DATABASE_URL": "postgres://user:password@postgres:5432/messenger",
|
||||
"CACHE_URL": "redis://valkey:6379/0", "BROKER_URL": "redis://redis-broker:6379/0",
|
||||
"S3_ENDPOINT": "s3:9000", "S3_REGION": "us-east-1", "S3_BUCKET": "attachments",
|
||||
"S3_ACCESS_KEY": "application", "S3_SECRET_KEY": "application-secret",
|
||||
} {
|
||||
t.Setenv(name, value)
|
||||
}
|
||||
}
|
||||
|
||||
const validTestConfig = `version: 1
|
||||
server:
|
||||
session_store: valkey
|
||||
session_ttl: 1h
|
||||
idempotency_ttl: 24h
|
||||
request_timeout: 5s
|
||||
http:
|
||||
read_header_timeout: 5s
|
||||
read_timeout: 10s
|
||||
write_timeout: 10s
|
||||
idle_timeout: 1m
|
||||
shutdown_timeout: 5s
|
||||
password:
|
||||
memory_kib: 65536
|
||||
iterations: 3
|
||||
parallelism: 2
|
||||
salt_length: 16
|
||||
key_length: 32
|
||||
database:
|
||||
min_connections: 0
|
||||
max_connections: 4
|
||||
max_connection_age: 5m
|
||||
max_connection_idle: 1m
|
||||
health_check_period: 30s
|
||||
dependency_timeout: 5s
|
||||
migration:
|
||||
timeout: 30s
|
||||
object_storage:
|
||||
use_tls: false
|
||||
timeout: 5s
|
||||
attachments:
|
||||
max_bytes: 10485760
|
||||
max_pixels: 40000000
|
||||
body_memory_bytes: 67108864
|
||||
decode_memory_bytes: 402653184
|
||||
pipeline:
|
||||
version: 1
|
||||
broker:
|
||||
stream: attachment-jobs
|
||||
consumer_group: attachment-workers
|
||||
failed_stream: attachment-jobs-failed
|
||||
failed_stream_max_length: 10000
|
||||
outbox:
|
||||
batch_size: 32
|
||||
lease: 15s
|
||||
poll_interval: 1s
|
||||
recovery_interval: 10s
|
||||
retry_delay: 2s
|
||||
retention: 168h
|
||||
cleanup_interval: 1m
|
||||
cleanup_batch_size: 1000
|
||||
worker:
|
||||
concurrency: 2
|
||||
prefetch: 4
|
||||
lease: 30s
|
||||
heartbeat_interval: 5s
|
||||
processing_timeout: 20s
|
||||
read_block: 2s
|
||||
reclaim_interval: 5s
|
||||
max_attempts: 5
|
||||
retry:
|
||||
base: 1s
|
||||
max: 30s
|
||||
memory_bytes: 738197504
|
||||
monitoring:
|
||||
metrics_address: 0.0.0.0:9091
|
||||
`
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
const supportedConfigVersion = 1
|
||||
|
||||
var environmentKeys = map[string]string{
|
||||
"server.host": "APP_HOST",
|
||||
"server.port": "APP_PORT",
|
||||
"server.swagger_port": "SWAGGER_PORT",
|
||||
"database.url": "DATABASE_URL",
|
||||
"cache.url": "CACHE_URL",
|
||||
"object_storage.endpoint": "S3_ENDPOINT",
|
||||
"object_storage.region": "S3_REGION",
|
||||
"object_storage.bucket": "S3_BUCKET",
|
||||
"object_storage.access_key": "S3_ACCESS_KEY",
|
||||
"object_storage.secret_key": "S3_SECRET_KEY",
|
||||
"broker.url": "BROKER_URL",
|
||||
}
|
||||
|
||||
func loadValues() (*viper.Viper, error) {
|
||||
path := strings.TrimSpace(os.Getenv("MESSENGER_CONFIG_PATH"))
|
||||
if path == "" {
|
||||
return nil, errors.New("MESSENGER_CONFIG_PATH is required")
|
||||
}
|
||||
values := viper.New()
|
||||
values.SetConfigFile(path)
|
||||
if err := values.ReadInConfig(); err != nil {
|
||||
return nil, fmt.Errorf("read messenger config: %w", err)
|
||||
}
|
||||
if version := values.GetInt("version"); version != supportedConfigVersion {
|
||||
return nil, fmt.Errorf("version: must be %d", supportedConfigVersion)
|
||||
}
|
||||
for key, name := range environmentKeys {
|
||||
if err := values.BindEnv(key, name); err != nil {
|
||||
return nil, fmt.Errorf("bind %s: %w", name, err)
|
||||
}
|
||||
}
|
||||
return values, nil
|
||||
}
|
||||
+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