initialize forwarder after init() to avoid crashes (#11330)

DNSCache dialer is a global value initialized in
init(), whereas `go` keeps `var =` before `init()`
, also we don't need to keep proxy routers as
global entities - register the forwarder as
necessary to avoid crashes.
master
Harshavardhana 4 years ago committed by GitHub
parent a6c146bd00
commit 1b453728a3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 10
      cmd/common-main.go
  2. 13
      cmd/generic-handlers.go
  3. 3
      cmd/globals.go

@ -41,6 +41,7 @@ import (
"github.com/minio/minio/pkg/certs" "github.com/minio/minio/pkg/certs"
"github.com/minio/minio/pkg/console" "github.com/minio/minio/pkg/console"
"github.com/minio/minio/pkg/env" "github.com/minio/minio/pkg/env"
"github.com/minio/minio/pkg/handlers"
) )
// serverDebugLog will enable debug printing // serverDebugLog will enable debug printing
@ -51,10 +52,19 @@ func init() {
logger.RegisterError(config.FmtError) logger.RegisterError(config.FmtError)
rand.Seed(time.Now().UTC().UnixNano()) rand.Seed(time.Now().UTC().UnixNano())
globalDNSCache = xhttp.NewDNSCache(10*time.Second, 10*time.Second, logger.LogOnceIf) globalDNSCache = xhttp.NewDNSCache(10*time.Second, 10*time.Second, logger.LogOnceIf)
initGlobalContext() initGlobalContext()
globalForwarder = handlers.NewForwarder(&handlers.Forwarder{
PassHost: true,
RoundTripper: newGatewayHTTPTransport(1 * time.Hour),
Logger: func(err error) {
logger.LogIf(GlobalContext, err)
},
})
globalReplicationState = newReplicationState() globalReplicationState = newReplicationState()
globalTransitionState = newTransitionState() globalTransitionState = newTransitionState()

@ -30,7 +30,6 @@ import (
xhttp "github.com/minio/minio/cmd/http" xhttp "github.com/minio/minio/cmd/http"
"github.com/minio/minio/cmd/http/stats" "github.com/minio/minio/cmd/http/stats"
"github.com/minio/minio/cmd/logger" "github.com/minio/minio/cmd/logger"
"github.com/minio/minio/pkg/handlers"
) )
// Adds limiting body size middleware // Adds limiting body size middleware
@ -532,14 +531,6 @@ func setRequestValidityHandler(h http.Handler) http.Handler {
}) })
} }
var fwd = handlers.NewForwarder(&handlers.Forwarder{
PassHost: true,
RoundTripper: newGatewayHTTPTransport(1 * time.Hour),
Logger: func(err error) {
logger.LogIf(GlobalContext, err)
},
})
// setBucketForwardingHandler middleware forwards the path style requests // setBucketForwardingHandler middleware forwards the path style requests
// on a bucket to the right bucket location, bucket to IP configuration // on a bucket to the right bucket location, bucket to IP configuration
// is obtained from centralized etcd configuration service. // is obtained from centralized etcd configuration service.
@ -589,7 +580,7 @@ func setBucketForwardingHandler(h http.Handler) http.Handler {
r.URL.Scheme = "https" r.URL.Scheme = "https"
} }
r.URL.Host = getHostFromSrv(sr) r.URL.Host = getHostFromSrv(sr)
fwd.ServeHTTP(w, r) globalForwarder.ServeHTTP(w, r)
return return
} }
h.ServeHTTP(w, r) h.ServeHTTP(w, r)
@ -639,7 +630,7 @@ func setBucketForwardingHandler(h http.Handler) http.Handler {
r.URL.Scheme = "https" r.URL.Scheme = "https"
} }
r.URL.Host = getHostFromSrv(sr) r.URL.Host = getHostFromSrv(sr)
fwd.ServeHTTP(w, r) globalForwarder.ServeHTTP(w, r)
return return
} }
h.ServeHTTP(w, r) h.ServeHTTP(w, r)

@ -25,6 +25,7 @@ import (
"github.com/minio/minio-go/v7/pkg/set" "github.com/minio/minio-go/v7/pkg/set"
"github.com/minio/minio/pkg/bucket/bandwidth" "github.com/minio/minio/pkg/bucket/bandwidth"
"github.com/minio/minio/pkg/handlers"
humanize "github.com/dustin/go-humanize" humanize "github.com/dustin/go-humanize"
"github.com/minio/minio/cmd/config/cache" "github.com/minio/minio/cmd/config/cache"
@ -285,6 +286,8 @@ var (
globalProxyTransport http.RoundTripper globalProxyTransport http.RoundTripper
globalDNSCache *xhttp.DNSCache globalDNSCache *xhttp.DNSCache
globalForwarder *handlers.Forwarder
// Add new variable global values here. // Add new variable global values here.
) )

Loading…
Cancel
Save