Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 3 commits into from
May 7, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions client/flags/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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().Int64(FlagRPCMaxBodyBytes, 1000000, "The RPC max body bytes (in MB)")
fedekunze marked this conversation as resolved.
Show resolved Hide resolved
cmd.Flags().Bool(FlagUnsafeCORS, false, "Allows CORS requests from all domains. For development purposes only, use it at your own risk.")

return cmd
Expand Down
49 changes: 29 additions & 20 deletions client/lcd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand All @@ -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(
cfg := rpcserver.DefaultConfig()
cfg.MaxOpenConnections = viper.GetInt(flags.FlagMaxOpenConnections)
cfg.ReadTimeout = time.Duration(viper.GetUint(flags.FlagRPCReadTimeout)) * time.Second
cfg.WriteTimeout = time.Duration(viper.GetUint(flags.FlagRPCWriteTimeout)) * time.Second
cfg.MaxBodyBytes = viper.GetInt64(flags.FlagRPCMaxBodyBytes)

// start the rest server and return error if one exists
return rs.StartWithConfig(
viper.GetString(flags.FlagListenAddr),
viper.GetInt(flags.FlagMaxOpenConnections),
uint(viper.GetInt(flags.FlagRPCReadTimeout)),
uint(viper.GetInt(flags.FlagRPCWriteTimeout)),
viper.GetBool(flags.FlagUnsafeCORS),
cfg,
)

return err
},
}

Expand Down