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

Update REST Client to Support Custom R/W Timeouts #4285

Merged
merged 2 commits into from
May 7, 2019
Merged
Show file tree
Hide file tree
Changes from all 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 .pending/breaking/sdk/4263-RestServer-Star
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#4263 RestServer#Start now takes read and write timeout arguments.
2 changes: 2 additions & 0 deletions .pending/features/sdk/4263-Add---read-time
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#4263 Add `--read-timeout` and `--write-timeout` args to the `rest-server` command
to support custom RPC R/W timeouts.
6 changes: 5 additions & 1 deletion client/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ const (
FlagListenAddr = "laddr"
FlagCORS = "cors"
FlagMaxOpenConnections = "max-open"
FlagRPCReadTimeout = "read-timeout"
FlagRPCWriteTimeout = "write-timeout"
FlagOutputDocument = "output-document" // inspired by wget -O
FlagSkipConfirmation = "yes"
)
Expand Down Expand Up @@ -116,7 +118,9 @@ func RegisterRestServerFlags(cmd *cobra.Command) *cobra.Command {
cmd = GetCommands(cmd)[0]
cmd.Flags().String(FlagListenAddr, "tcp://localhost:1317", "The address for the server to listen on")
cmd.Flags().String(FlagCORS, "", "Set the domains that can make CORS requests (* for all)")
cmd.Flags().Int(FlagMaxOpenConnections, 1000, "The number of maximum open connections")
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)")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this the timeout in order to receive conformation that your write request was successful? Like what is a rest write?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rigelrozanski yeah, the write timeout, from the REST client pov, is mainly for broadcasting.


return cmd
}
Expand Down
21 changes: 14 additions & 7 deletions client/lcd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"net"
"net/http"
"os"
"time"

"github.com/gorilla/mux"
"github.com/rakyll/statik/fs"
Expand Down Expand Up @@ -45,20 +46,22 @@ func NewRestServer(cdc *codec.Codec) *RestServer {
Mux: r,
CliCtx: cliCtx,
Cdc: cdc,

log: logger,
log: logger,
}
}

// Start starts the rest server
func (rs *RestServer) Start(listenAddr string, maxOpen int) (err error) {
func (rs *RestServer) Start(listenAddr string, maxOpen int, readTimeout, writeTimeout uint) (err error) {
server.TrapSignal(func() {
err := rs.listener.Close()
rs.log.Error("error closing listener", "err", err)
})

cfg := rpcserver.DefaultConfig()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are you dropping DefaultConfig()? Couldn't we override only the values that we want to override and fallback to the defaults?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because there is no longer a need for DefaultConfig(). Config is defined by three values which we each set manually. ie. the call would be redundant.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see - fair enough

cfg.MaxOpenConnections = maxOpen
cfg := &rpcserver.Config{
MaxOpenConnections: maxOpen,
ReadTimeout: time.Duration(readTimeout) * time.Second,
WriteTimeout: time.Duration(writeTimeout) * time.Second,
}

rs.listener, err = rpcserver.Listen(listenAddr, cfg)
if err != nil {
Expand Down Expand Up @@ -87,8 +90,12 @@ func ServeCommand(cdc *codec.Codec, registerRoutesFn func(*RestServer)) *cobra.C
registerRoutesFn(rs)

// Start the rest server and return error if one exists
err = rs.Start(viper.GetString(client.FlagListenAddr),
viper.GetInt(client.FlagMaxOpenConnections))
err = rs.Start(
viper.GetString(client.FlagListenAddr),
viper.GetInt(client.FlagMaxOpenConnections),
uint(viper.GetInt(client.FlagRPCReadTimeout)),
uint(viper.GetInt(client.FlagRPCWriteTimeout)),
)

return err
},
Expand Down