forked from mtusi2026/mtusi2026lab6
49 lines
1.3 KiB
Go
49 lines
1.3 KiB
Go
package config
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
type Pipeline struct {
|
|
Concurrency, Prefetch, MaxAttempts int
|
|
Timeout, Lease, Retention time.Duration
|
|
BrokerURL string
|
|
}
|
|
|
|
func LoadPipeline() (*Pipeline, error) {
|
|
c := &Pipeline{Concurrency: 2, Prefetch: 2, MaxAttempts: 5, Timeout: 60 * time.Second, Lease: 90 * time.Second, Retention: 24 * time.Hour}
|
|
for k, p := range map[string]*int{"WORKER_CONCURRENCY": &c.Concurrency, "WORKER_PREFETCH": &c.Prefetch, "JOB_MAX_ATTEMPTS": &c.MaxAttempts} {
|
|
if v := os.Getenv(k); v != "" {
|
|
n, e := strconv.Atoi(v)
|
|
if e != nil || n < 1 || n > 64 {
|
|
return nil, fmt.Errorf("invalid %s", k)
|
|
}
|
|
*p = n
|
|
}
|
|
}
|
|
for k, p := range map[string]*time.Duration{"JOB_TIMEOUT": &c.Timeout, "JOB_LEASE": &c.Lease, "STAGING_RETENTION": &c.Retention} {
|
|
if v := os.Getenv(k); v != "" {
|
|
d, e := time.ParseDuration(v)
|
|
if e != nil || d <= 0 {
|
|
return nil, fmt.Errorf("invalid %s", k)
|
|
}
|
|
*p = d
|
|
}
|
|
}
|
|
if c.Lease <= c.Timeout || c.Prefetch > c.Concurrency {
|
|
return nil, fmt.Errorf("lease must exceed timeout; prefetch must not exceed concurrency")
|
|
}
|
|
if p := os.Getenv("BROKER_URL_FILE"); p != "" {
|
|
b, e := os.ReadFile(p)
|
|
if e != nil {
|
|
return nil, fmt.Errorf("read broker secret: %w", e)
|
|
}
|
|
c.BrokerURL = strings.TrimSpace(string(b))
|
|
}
|
|
return c, nil
|
|
}
|