From a428a0b1b3dd49f4bb5cd7f04d4b1b0017327c2d Mon Sep 17 00:00:00 2001 From: Evgeniy Kulikov Date: Fri, 12 Feb 2021 15:24:52 +0300 Subject: [PATCH] fix conflicts --- README.md | 9 +++++++++ app.go | 11 +++++++++++ settings.go | 37 +++++++++++++++++++++++++++++++------ upload.go | 2 +- 4 files changed, 52 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index f4b30d4..73aaeda 100644 --- a/README.md +++ b/README.md @@ -73,6 +73,15 @@ If false, when there are no active RPCs, Time and Timeout will be ignored and no HTTP_GW_UPLOAD_HEADER_USE_DEFAULT_TIMESTAMP=bool - enable/disable adding current timestamp attribute when object uploads +HTTP_GW_WEB_READ_BUFFER_SIZE=4096 - per-connection buffer size for requests' reading +HTTP_GW_WEB_READ_TIMEOUT=15s - an amount of time allowed to read the full request including body +HTTP_GW_WEB_WRITE_BUFFER_SIZE=4096 - per-connection buffer size for responses' writing +HTTP_GW_WEB_WRITE_TIMEOUT=1m0s - maximum duration before timing out writes of the response +HTTP_GW_WEB_STREAM_REQUEST_BODY=true - enables request body streaming, and calls the handler sooner when given + body is larger then the current limit +HTTP_GW_WEB_MAX_REQUEST_BODY_SIZE=4194304 - maximum request body size, server rejects requests with bodies exceeding + this limit + Peers preset: HTTP_GW_PEERS_[N]_ADDRESS = string diff --git a/app.go b/app.go index ca3d249..34a1492 100644 --- a/app.go +++ b/app.go @@ -97,6 +97,17 @@ func newApp(ctx context.Context, opt ...Option) App { a.web.DisableHeaderNamesNormalizing = true a.web.NoDefaultServerHeader = true a.web.NoDefaultContentType = true + a.web.MaxRequestBodySize = a.cfg.GetInt(cfgWebMaxRequestBodySize) + + // FIXME don't work with StreamRequestBody, + // some bugs with readMultipartForm + // a.web.DisablePreParseMultipartForm = true + + // body streaming + // TODO should be replaced in future with + // + // a.web.StreamRequestBody = v.GetBool(cfgWebStreamRequestBody) + checkAndEnableStreaming(a.log, a.cfg, a.web) // -- -- -- -- -- -- -- -- -- -- connections := make(map[string]float64) diff --git a/settings.go b/settings.go index 02fcde8..b2dac09 100644 --- a/settings.go +++ b/settings.go @@ -4,6 +4,7 @@ import ( "fmt" "io" "os" + "reflect" "sort" "strconv" "strings" @@ -11,6 +12,8 @@ import ( "github.com/spf13/pflag" "github.com/spf13/viper" + "github.com/valyala/fasthttp" + "go.uber.org/zap" ) type empty int @@ -33,11 +36,12 @@ const ( cfgKeepalivePermitWithoutStream = "keepalive.permit_without_stream" // Web - cfgWebReadBufferSize = "web.read_buffer_size" - cfgWebWriteBufferSize = "web.write_buffer_size" - cfgWebReadTimeout = "web.read_timeout" - cfgWebWriteTimeout = "web.write_timeout" - cfgWebConnectionPerHost = "web.connection_per_host" + cfgWebReadBufferSize = "web.read_buffer_size" + cfgWebWriteBufferSize = "web.write_buffer_size" + cfgWebReadTimeout = "web.read_timeout" + cfgWebWriteTimeout = "web.write_timeout" + cfgWebStreamRequestBody = "web.stream_request_body" + cfgWebMaxRequestBodySize = "web.max_request_body_size" // Timeouts cfgConTimeout = "connect_timeout" @@ -86,6 +90,26 @@ var ignore = map[string]struct{}{ func (empty) Read([]byte) (int, error) { return 0, io.EOF } +// checkAndEnableStreaming is temporary shim, should be used before +// `StreamRequestBody` is not merged in fasthttp master +// TODO should be removed in future +func checkAndEnableStreaming(l *zap.Logger, v *viper.Viper, i interface{}) { + vi := reflect.ValueOf(i) + + if vi.Type().Kind() != reflect.Ptr { + return + } + + field := vi.Elem().FieldByName("StreamRequestBody") + if !field.IsValid() || field.Kind() != reflect.Bool { + l.Warn("stream request body not supported") + + return + } + + field.SetBool(v.GetBool(cfgWebStreamRequestBody)) +} + func settings() *viper.Viper { v := viper.New() v.AutomaticEnv() @@ -140,7 +164,8 @@ func settings() *viper.Viper { v.SetDefault(cfgWebWriteBufferSize, 4096) v.SetDefault(cfgWebReadTimeout, time.Second*15) v.SetDefault(cfgWebWriteTimeout, time.Minute) - v.SetDefault(cfgWebConnectionPerHost, 10) + v.SetDefault(cfgWebStreamRequestBody, true) + v.SetDefault(cfgWebMaxRequestBodySize, fasthttp.DefaultMaxRequestBodySize) // upload header v.SetDefault(cfgUploaderHeaderEnableDefaultTimestamp, false) diff --git a/upload.go b/upload.go index 767ee2e..38d53e9 100644 --- a/upload.go +++ b/upload.go @@ -55,7 +55,7 @@ func (a *app) upload(c *fasthttp.RequestCtx) { defer func() { // if temporary reader can be closed - close it - if closer := tmp.(io.Closer); closer != nil { + if closer, ok := tmp.(io.Closer); ok && closer != nil { log.Debug("close temporary multipart/form file", zap.Error(closer.Close())) }