REST: Add 'max-body-bytes' Flag (#6167)

* Add max-body-bytes flag for REST service

* Add changelog entries

* Change types
This commit is contained in:
Alexander Bezobchuk 2020-05-07 13:49:08 -04:00 committed by GitHub
parent b3cada1001
commit fe9fe87693
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 34 additions and 22 deletions

View File

@ -99,6 +99,7 @@ information on how to implement the new `Keyring` interface.
### Features
* (rest) [\#6167](https://github.com/cosmos/cosmos-sdk/pull/6167) Support `max-body-bytes` CLI flag for the REST service.
* (x/ibc) [\#5588](https://github.com/cosmos/cosmos-sdk/pull/5588) Add [ICS 024 - Host State Machine Requirements](https://github.com/cosmos/ics/tree/master/spec/ics-024-host-requirements) subpackage to `x/ibc` module.
* (x/ibc) [\#5277](https://github.com/cosmos/cosmos-sdk/pull/5277) `x/ibc` changes from IBC alpha. For more details check the the [`x/ibc/spec`](https://github.com/cosmos/tree/master/x/ibc/spec) directory:
* [ICS 002 - Client Semantics](https://github.com/cosmos/ics/tree/master/spec/ics-002-client-semantics) subpackage

View File

@ -63,6 +63,7 @@ const (
FlagMaxOpenConnections = "max-open"
FlagRPCReadTimeout = "read-timeout"
FlagRPCWriteTimeout = "write-timeout"
FlagRPCMaxBodyBytes = "max-body-bytes"
FlagOutputDocument = "output-document" // inspired by wget -O
FlagSkipConfirmation = "yes"
FlagProve = "prove"
@ -148,6 +149,7 @@ func RegisterRestServerFlags(cmd *cobra.Command) *cobra.Command {
cmd.Flags().Uint(FlagMaxOpenConnections, 1000, "The number of maximum open connections")
cmd.Flags().Uint(FlagRPCReadTimeout, 10, "The RPC read timeout (in seconds)")
cmd.Flags().Uint(FlagRPCWriteTimeout, 10, "The RPC write timeout (in seconds)")
cmd.Flags().Uint(FlagRPCMaxBodyBytes, 1000000, "The RPC max body bytes (in MB)")
cmd.Flags().Bool(FlagUnsafeCORS, false, "Allows CORS requests from all domains. For development purposes only, use it at your own risk.")
return cmd

View File

@ -46,28 +46,23 @@ func NewRestServer(cdc *codec.Codec) *RestServer {
}
}
// Start starts the rest server
func (rs *RestServer) Start(listenAddr string, maxOpen int, readTimeout, writeTimeout uint, cors bool) (err error) {
// StartWithConfig starts the REST server that listens on the provided listenAddr.
// It will use the provided RPC configuration.
func (rs *RestServer) StartWithConfig(listenAddr string, cors bool, cfg *rpcserver.Config) error {
server.TrapSignal(func() {
err := rs.listener.Close()
rs.log.Error("error closing listener", "err", err)
})
cfg := rpcserver.DefaultConfig()
cfg.MaxOpenConnections = maxOpen
cfg.ReadTimeout = time.Duration(readTimeout) * time.Second
cfg.WriteTimeout = time.Duration(writeTimeout) * time.Second
rs.listener, err = rpcserver.Listen(listenAddr, cfg)
listener, err := rpcserver.Listen(listenAddr, cfg)
if err != nil {
return
return err
}
rs.listener = listener
rs.log.Info(
fmt.Sprintf(
"Starting application REST service (chain-id: %q)...",
viper.GetString(flags.FlagChainID),
),
fmt.Sprintf("Starting application REST service (chain-id: %q)...", viper.GetString(flags.FlagChainID)),
)
var h http.Handler = rs.Mux
@ -79,6 +74,18 @@ func (rs *RestServer) Start(listenAddr string, maxOpen int, readTimeout, writeTi
return rpcserver.StartHTTPServer(rs.listener, rs.Mux, rs.log, cfg)
}
// Start starts the REST server that listens on the provided listenAddr. The REST
// service will use Tendermint's default RPC configuration, where the R/W timeout
// and max open connections are overridden.
func (rs *RestServer) Start(listenAddr string, maxOpen int, readTimeout, writeTimeout uint, cors bool) error {
cfg := rpcserver.DefaultConfig()
cfg.MaxOpenConnections = maxOpen
cfg.ReadTimeout = time.Duration(readTimeout) * time.Second
cfg.WriteTimeout = time.Duration(writeTimeout) * time.Second
return rs.StartWithConfig(listenAddr, cors, cfg)
}
// ServeCommand will start the application REST service as a blocking process. It
// takes a codec to create a RestServer object and a function to register all
// necessary routes.
@ -92,16 +99,18 @@ func ServeCommand(cdc *codec.Codec, registerRoutesFn func(*RestServer)) *cobra.C
registerRoutesFn(rs)
rs.registerSwaggerUI()
// Start the rest server and return error if one exists
err = rs.Start(
viper.GetString(flags.FlagListenAddr),
viper.GetInt(flags.FlagMaxOpenConnections),
uint(viper.GetInt(flags.FlagRPCReadTimeout)),
uint(viper.GetInt(flags.FlagRPCWriteTimeout)),
viper.GetBool(flags.FlagUnsafeCORS),
)
cfg := rpcserver.DefaultConfig()
cfg.MaxOpenConnections = viper.GetInt(flags.FlagMaxOpenConnections)
cfg.ReadTimeout = time.Duration(viper.GetInt64(flags.FlagRPCReadTimeout)) * time.Second
cfg.WriteTimeout = time.Duration(viper.GetInt64(flags.FlagRPCWriteTimeout)) * time.Second
cfg.MaxBodyBytes = viper.GetInt64(flags.FlagRPCMaxBodyBytes)
return err
// start the rest server and return error if one exists
return rs.StartWithConfig(
viper.GetString(flags.FlagListenAddr),
viper.GetBool(flags.FlagUnsafeCORS),
cfg,
)
},
}