From 593fd25981a28ce9c83ec49a6a1556e9961d49fe Mon Sep 17 00:00:00 2001 From: scott lewis <33612882+dk-lockdown@users.noreply.github.com> Date: Fri, 29 Jul 2022 15:20:39 +0800 Subject: [PATCH] feat: support customize request buffer size for http request (#219) --- pkg/listener/http.go | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/pkg/listener/http.go b/pkg/listener/http.go index a2a27bf..ac9f9b0 100644 --- a/pkg/listener/http.go +++ b/pkg/listener/http.go @@ -38,6 +38,12 @@ import ( type HttpConfig struct { BackendHost string `yaml:"backend_host" json:"backend_host"` + // ReadBufferSize default 4096 byte + ReadBufferSize int `yaml:"read_buffer_size" json:"read_buffer_size"` + // WriteBufferSize default 4096 byte + WriteBufferSize int `yaml:"write_buffer_size" json:"write_buffer_size"` + // MaxRequestBodySize default 4 * 1024 * 1024 byte + MaxRequestBodySize int `yaml:"max_request_body_size" json:"max_request_body_size"` } type HttpListener struct { @@ -97,7 +103,7 @@ func NewHttpListener(conf *config.Listener) (proto.Listener, error) { func (l *HttpListener) Listen() { log.Infof("start http listener %s", l.listener.Addr()) - if err := fasthttp.Serve(l.listener, func(fastHttpCtx *fasthttp.RequestCtx) { + handler := func(fastHttpCtx *fasthttp.RequestCtx) { fastHttpCtx.SetUserValue(dt.VarHost, l.conf.BackendHost) ctx := extractTraceContext(context.Background(), &fastHttpCtx.Request) spanCtx, span := tracing.GetTraceSpan(ctx, tracing.HTTPProxyService) @@ -130,7 +136,14 @@ func (l *HttpListener) Listen() { fastHttpCtx.SetStatusCode(http.StatusInternalServerError) fastHttpCtx.SetBodyString(fmt.Sprintf(`{"success":false,"error":"%s"}`, err.Error())) } - }); err != nil { + } + server := &fasthttp.Server{ + Handler: handler, + ReadBufferSize: l.conf.ReadBufferSize, + WriteBufferSize: l.conf.WriteBufferSize, + MaxRequestBodySize: l.conf.MaxRequestBodySize, + } + if err := server.Serve(l.listener); err != nil { log.Error(err) } }