Skip to content

Commit

Permalink
feat: support customize request buffer size for http request
Browse files Browse the repository at this point in the history
  • Loading branch information
dk-lockdown committed Jul 29, 2022
1 parent 852a0f2 commit 923b53b
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions pkg/listener/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
}
}
Expand Down

0 comments on commit 923b53b

Please sign in to comment.