Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean up flag parsing #43

Merged
merged 1 commit into from
Dec 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading