diff --git a/Gopkg.lock b/Gopkg.lock index 9855ecbc7..504e2c82c 100644 --- a/Gopkg.lock +++ b/Gopkg.lock @@ -1217,7 +1217,7 @@ "roundrobin", "utils" ] - revision = "7a2284ad8d6f4d362a6b38f3cdcc812291dce293" + revision = "d5b73186eed4aa34b52748699ad19e90f61d4059" [[projects]] name = "github.com/vulcand/predicate" diff --git a/server/bufferpool.go b/server/bufferpool.go new file mode 100644 index 000000000..157ea2ad7 --- /dev/null +++ b/server/bufferpool.go @@ -0,0 +1,27 @@ +package server + +import "sync" + +const bufferPoolSize int = 32 * 1024 + +func newBufferPool() *bufferPool { + return &bufferPool{ + pool: sync.Pool{ + New: func() interface{} { + return make([]byte, bufferPoolSize) + }, + }, + } +} + +type bufferPool struct { + pool sync.Pool +} + +func (b *bufferPool) Get() []byte { + return b.pool.Get().([]byte) +} + +func (b *bufferPool) Put(bytes []byte) { + b.pool.Put(bytes) +} diff --git a/server/server.go b/server/server.go index 62a7081ca..f163bfef7 100644 --- a/server/server.go +++ b/server/server.go @@ -11,6 +11,7 @@ import ( stdlog "log" "net" "net/http" + "net/http/httputil" "net/url" "os" "os/signal" @@ -75,6 +76,7 @@ type Server struct { metricsRegistry metrics.Registry provider provider.Provider configurationListeners []func(types.Configuration) + bufferPool httputil.BufferPool } type serverEntryPoints map[string]*serverEntryPoint @@ -106,6 +108,8 @@ func NewServer(globalConfiguration configuration.GlobalConfiguration, provider p server.globalConfiguration.API.CurrentConfigurations = &server.currentConfigurations } + server.bufferPool = newBufferPool() + server.routinesPool = safe.NewPool(context.Background()) server.defaultForwardingRoundTripper = createHTTPTransport(globalConfiguration) @@ -1001,6 +1005,7 @@ func (s *Server) loadConfig(configurations types.Configurations, globalConfigura forward.ErrorHandler(errorHandler), forward.Rewriter(rewriter), forward.ResponseModifier(responseModifier), + forward.BufferPool(s.bufferPool), ) if err != nil { diff --git a/vendor/github.com/vulcand/oxy/forward/fwd.go b/vendor/github.com/vulcand/oxy/forward/fwd.go index ec3c4f8d4..668cd920b 100644 --- a/vendor/github.com/vulcand/oxy/forward/fwd.go +++ b/vendor/github.com/vulcand/oxy/forward/fwd.go @@ -85,6 +85,14 @@ func ErrorHandler(h utils.ErrorHandler) optSetter { } } +// BufferPool specifies a buffer pool for httputil.ReverseProxy. +func BufferPool(pool httputil.BufferPool) optSetter { + return func(f *Forwarder) error { + f.bufferPool = pool + return nil + } +} + // Stream specifies if HTTP responses should be streamed. func Stream(stream bool) optSetter { return func(f *Forwarder) error { @@ -176,6 +184,8 @@ type httpForwarder struct { tlsClientConfig *tls.Config log OxyLogger + + bufferPool httputil.BufferPool } const ( @@ -478,6 +488,7 @@ func (f *httpForwarder) serveHTTP(w http.ResponseWriter, inReq *http.Request, ct Transport: f.roundTripper, FlushInterval: f.flushInterval, ModifyResponse: f.modifyResponse, + BufferPool: f.bufferPool, } revproxy.ServeHTTP(pw, outReq)