diff options
Diffstat (limited to 'internal/config/config.go')
| -rw-r--r-- | internal/config/config.go | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/internal/config/config.go b/internal/config/config.go index 5e6e467..609bde5 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -34,6 +34,18 @@ type Config struct { ListenAddr string `yaml:"listen_addr"` MirrorPrivateKeyPath string `yaml:"mirror_private_key_path"` PollInterval time.Duration `yaml:"poll_interval"` + RequestTimeout time.Duration `yaml:"request_timeout"` + RateLimit RateLimit `yaml:"rate_limit"` + HTTPRetryMaxAttempts int `yaml:"http_retry_max_attempts"` + HTTPRetryBaseDelay time.Duration `yaml:"http_retry_base_delay"` + HTTPRetryMaxDelay time.Duration `yaml:"http_retry_max_delay"` +} + +type RateLimit struct { + ResolveRPS float64 `yaml:"resolve_rps"` + ResolveBurst int `yaml:"resolve_burst"` + ProofRPS float64 `yaml:"proof_rps"` + ProofBurst int `yaml:"proof_burst"` } func Default() Config { @@ -50,6 +62,16 @@ func Default() Config { ListenAddr: ":8080", MirrorPrivateKeyPath: "./mirror.key", PollInterval: 5 * time.Second, + RequestTimeout: 10 * time.Second, + RateLimit: RateLimit{ + ResolveRPS: 30, + ResolveBurst: 60, + ProofRPS: 10, + ProofBurst: 20, + }, + HTTPRetryMaxAttempts: 8, + HTTPRetryBaseDelay: 250 * time.Millisecond, + HTTPRetryMaxDelay: 10 * time.Second, } } @@ -77,6 +99,13 @@ func applyEnv(cfg *Config) { *dst = v } } + setFloat64 := func(key string, dst *float64) { + if v := strings.TrimSpace(os.Getenv(key)); v != "" { + if n, err := strconv.ParseFloat(v, 64); err == nil { + *dst = n + } + } + } setInt := func(key string, dst *int) { if v := strings.TrimSpace(os.Getenv(key)); v != "" { if n, err := strconv.Atoi(v); err == nil { @@ -111,6 +140,14 @@ func applyEnv(cfg *Config) { setString("PLUTIA_LISTEN_ADDR", &cfg.ListenAddr) setString("PLUTIA_MIRROR_PRIVATE_KEY_PATH", &cfg.MirrorPrivateKeyPath) setDuration("PLUTIA_POLL_INTERVAL", &cfg.PollInterval) + setDuration("PLUTIA_REQUEST_TIMEOUT", &cfg.RequestTimeout) + setFloat64("PLUTIA_RATE_LIMIT_RESOLVE_RPS", &cfg.RateLimit.ResolveRPS) + setInt("PLUTIA_RATE_LIMIT_RESOLVE_BURST", &cfg.RateLimit.ResolveBurst) + setFloat64("PLUTIA_RATE_LIMIT_PROOF_RPS", &cfg.RateLimit.ProofRPS) + setInt("PLUTIA_RATE_LIMIT_PROOF_BURST", &cfg.RateLimit.ProofBurst) + setInt("PLUTIA_HTTP_RETRY_MAX_ATTEMPTS", &cfg.HTTPRetryMaxAttempts) + setDuration("PLUTIA_HTTP_RETRY_BASE_DELAY", &cfg.HTTPRetryBaseDelay) + setDuration("PLUTIA_HTTP_RETRY_MAX_DELAY", &cfg.HTTPRetryMaxDelay) } func (c Config) Validate() error { @@ -149,5 +186,32 @@ func (c Config) Validate() error { if c.PollInterval <= 0 { return errors.New("poll_interval must be > 0") } + if c.RequestTimeout <= 0 { + return errors.New("request_timeout must be > 0") + } + if c.RateLimit.ResolveRPS <= 0 { + return errors.New("rate_limit.resolve_rps must be > 0") + } + if c.RateLimit.ResolveBurst <= 0 { + return errors.New("rate_limit.resolve_burst must be > 0") + } + if c.RateLimit.ProofRPS <= 0 { + return errors.New("rate_limit.proof_rps must be > 0") + } + if c.RateLimit.ProofBurst <= 0 { + return errors.New("rate_limit.proof_burst must be > 0") + } + if c.HTTPRetryMaxAttempts < 1 || c.HTTPRetryMaxAttempts > 32 { + return fmt.Errorf("http_retry_max_attempts must be between 1 and 32, got %d", c.HTTPRetryMaxAttempts) + } + if c.HTTPRetryBaseDelay <= 0 { + return errors.New("http_retry_base_delay must be > 0") + } + if c.HTTPRetryMaxDelay <= 0 { + return errors.New("http_retry_max_delay must be > 0") + } + if c.HTTPRetryBaseDelay > c.HTTPRetryMaxDelay { + return errors.New("http_retry_base_delay must be <= http_retry_max_delay") + } return nil } |