From aed43a54da328b3f4d85660598818f79c82bcaa3 Mon Sep 17 00:00:00 2001 From: Joel Speed Date: Thu, 19 Nov 2020 10:35:04 +0000 Subject: [PATCH] Add DefaultUpstreamFlushInterval to replace magic time.Second value --- pkg/apis/options/legacy_options.go | 4 ++-- pkg/apis/options/upstreams.go | 7 +++++++ pkg/upstream/http.go | 3 +-- pkg/validation/upstreams.go | 3 +-- 4 files changed, 11 insertions(+), 6 deletions(-) diff --git a/pkg/apis/options/legacy_options.go b/pkg/apis/options/legacy_options.go index 030dbe83..6fb2596b 100644 --- a/pkg/apis/options/legacy_options.go +++ b/pkg/apis/options/legacy_options.go @@ -27,7 +27,7 @@ func NewLegacyOptions() *LegacyOptions { LegacyUpstreams: LegacyUpstreams{ PassHostHeader: true, ProxyWebSockets: true, - FlushInterval: time.Duration(1) * time.Second, + FlushInterval: DefaultUpstreamFlushInterval, }, LegacyHeaders: LegacyHeaders{ @@ -62,7 +62,7 @@ type LegacyUpstreams struct { func legacyUpstreamsFlagSet() *pflag.FlagSet { flagSet := pflag.NewFlagSet("upstreams", pflag.ExitOnError) - flagSet.Duration("flush-interval", time.Duration(1)*time.Second, "period between response flushing when streaming responses") + flagSet.Duration("flush-interval", DefaultUpstreamFlushInterval, "period between response flushing when streaming responses") flagSet.Bool("pass-host-header", true, "pass the request Host Header to upstream") flagSet.Bool("proxy-websockets", true, "enables WebSocket proxying") flagSet.Bool("ssl-upstream-insecure-skip-verify", false, "skip validation of certificates presented when using HTTPS upstreams") diff --git a/pkg/apis/options/upstreams.go b/pkg/apis/options/upstreams.go index 6536498d..6ae87487 100644 --- a/pkg/apis/options/upstreams.go +++ b/pkg/apis/options/upstreams.go @@ -1,5 +1,12 @@ package options +import "time" + +const ( + // DefaultUpstreamFlushInterval is the default value for the Upstream FlushInterval. + DefaultUpstreamFlushInterval = 1 * time.Second +) + // Upstreams is a collection of definitions for upstream servers. type Upstreams []Upstream diff --git a/pkg/upstream/http.go b/pkg/upstream/http.go index 741e9a97..a6e948c3 100644 --- a/pkg/upstream/http.go +++ b/pkg/upstream/http.go @@ -6,7 +6,6 @@ import ( "net/http/httputil" "net/url" "strings" - "time" "github.com/mbland/hmacauth" "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/apis/options" @@ -100,7 +99,7 @@ func newReverseProxy(target *url.URL, upstream options.Upstream, errorHandler Pr if upstream.FlushInterval != nil { proxy.FlushInterval = upstream.FlushInterval.Duration() } else { - proxy.FlushInterval = 1 * time.Second + proxy.FlushInterval = options.DefaultUpstreamFlushInterval } // InsecureSkipVerify is a configurable option we allow diff --git a/pkg/validation/upstreams.go b/pkg/validation/upstreams.go index fbff122c..7bd6b2d5 100644 --- a/pkg/validation/upstreams.go +++ b/pkg/validation/upstreams.go @@ -3,7 +3,6 @@ package validation import ( "fmt" "net/url" - "time" "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/apis/options" ) @@ -70,7 +69,7 @@ func validateStaticUpstream(upstream options.Upstream) []string { if upstream.InsecureSkipTLSVerify { msgs = append(msgs, fmt.Sprintf("upstream %q has insecureSkipTLSVerify, but is a static upstream, this will have no effect.", upstream.ID)) } - if upstream.FlushInterval != nil && upstream.FlushInterval.Duration() != time.Second { + if upstream.FlushInterval != nil && upstream.FlushInterval.Duration() != options.DefaultUpstreamFlushInterval { msgs = append(msgs, fmt.Sprintf("upstream %q has flushInterval, but is a static upstream, this will have no effect.", upstream.ID)) } if upstream.PassHostHeader != nil {