Skip to content

Commit

Permalink
Clean up flag parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
Kevin Deems committed Dec 30, 2024
1 parent 0f2f97c commit 46f6bf3
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 250 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ Build the docker-compose stack
```
make all
docker compose down
docker-compose up --build
docker compose up --build
```

- Generate fake traffic with `./scripts/traffic_generator.py`
Expand Down
30 changes: 5 additions & 25 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,18 +65,8 @@ func main() {
}

func setupInsecureServer(ctx context.Context, cfg proxyutil.Config) (*http.Server, error) {
readTimeout, err := time.ParseDuration(cfg.ReadTimeout)
if err != nil {
return nil, fmt.Errorf("error parsing read timeout: %v", err)
}

writeTimeout, err := time.ParseDuration(cfg.WriteTimeout)
if err != nil {
return nil, fmt.Errorf("error parsing write timeout: %v", err)
}

if cfg.ProxyConfig.ClientTimeout == 0 {
cfg.ProxyConfig.ClientTimeout = 2 * readTimeout
cfg.ProxyConfig.ClientTimeout = 2 * cfg.ReadTimeout
}

routes, err := proxyhttp.NewRoutes(ctx, cfg)
Expand All @@ -94,8 +84,8 @@ func setupInsecureServer(ctx context.Context, cfg proxyutil.Config) (*http.Serve

srv := &http.Server{
Handler: mux,
ReadTimeout: readTimeout,
WriteTimeout: writeTimeout,
ReadTimeout: cfg.ReadTimeout,
WriteTimeout: cfg.WriteTimeout,
}

go func() {
Expand Down Expand Up @@ -129,20 +119,10 @@ func setupInternalServer(cfg proxyutil.Config) (*http.Server, error) {
return nil, fmt.Errorf("failed to listen on internal address: %v", err)
}

readTimeout, err := time.ParseDuration(cfg.ReadTimeout)
if err != nil {
return nil, fmt.Errorf("error parsing read timeout: %v", err)
}

writeTimeout, err := time.ParseDuration(cfg.WriteTimeout)
if err != nil {
return nil, fmt.Errorf("error parsing write timeout: %v", err)
}

srv := &http.Server{
Handler: h,
ReadTimeout: readTimeout,
WriteTimeout: writeTimeout,
ReadTimeout: cfg.ReadTimeout,
WriteTimeout: cfg.WriteTimeout,
}

go func() {
Expand Down
34 changes: 34 additions & 0 deletions proxymw/backpressure.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,40 @@ type BackpressureConfig struct {
CongestionWindowMax int `yaml:"congestion_window_max"`
}

func ParseBackpressureQueries(
bpQueries, bpQueryNames []string, bpWarnThresholds, bpEmergencyThresholds []float64,
) ([]BackpressureQuery, error) {
n := len(bpQueries)
queries := make([]BackpressureQuery, n)
if len(bpQueryNames) != n && len(bpQueryNames) != 0 {
return nil, fmt.Errorf("number of backpressure query names should be 0 or %d", n)
}

if len(bpWarnThresholds) != n {
return nil, fmt.Errorf("expected %d warn thresholds for %d backpressure queries", n, n)
}

if len(bpEmergencyThresholds) != n {
return nil, fmt.Errorf(
"expected %d emergency thresholds for %d backpressure queries", n, n,
)
}

for i, query := range bpQueries {
queryName := ""
if len(bpQueryNames) > 0 {
queryName = bpQueryNames[i]
}
queries[i] = BackpressureQuery{
Name: queryName,
Query: query,
WarningThreshold: bpWarnThresholds[i],
EmergencyThreshold: bpEmergencyThresholds[i],
}
}
return queries, nil
}

func (c BackpressureConfig) Validate() error {
if !c.EnableBackpressure {
return nil
Expand Down
Loading

0 comments on commit 46f6bf3

Please sign in to comment.