Skip to content

Commit

Permalink
Update server timeouts
Browse files Browse the repository at this point in the history
  • Loading branch information
m-bo-one committed Sep 24, 2021
1 parent 9b5ced7 commit 2ff18ba
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
2 changes: 2 additions & 0 deletions config/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,7 @@ type HTTPConfig struct {
Addr string `toml:"addr" desc:"HTTP-RPC server listening interface (default: \"localhost\")"`
Port int `toml:"port" desc:"HTTP-RPC server listening port (default: 8545)"`

KeepAlive bool `toml:"keepalive" desc:"API's keep alive. Note: only very resource-constrained environments or servers in the process of shutting down should disable them (default: true)"`
API []string `toml:"api" desc:"API's offered over the HTTP-RPC interface"`
CORSDomain []string `toml:"corsdomain" desc:"Comma separated list of domains from which to accept cross origin requests (browser enforced)"`
VHosts []string `toml:"vhosts" desc:"Comma separated list of virtual hostnames from which to accept requests (server enforced). Accepts '*' wildcard. (default: \"localhost\")"`
Expand All @@ -297,6 +298,7 @@ func DefaultAPIConfig() *APIConfig {
HTTPConfig: &HTTPConfig{
Addr: "localhost",
Port: 8545,
KeepAlive: true,
API: []string{"eth", "web3", "net"},
CORSDomain: make([]string, 0),
VHosts: []string{"localhost"},
Expand Down
22 changes: 16 additions & 6 deletions web3/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,28 +51,38 @@ func (s *Server) start(rpcInfo interface{}, apis map[string]rpctypes.Web3Service
name string
handler http.Handler
)
srv := rpc.NewServer()
rpcSrv := rpc.NewServer()
channel := make(chan error)
timeout := make(chan error)
keepAlive := true

switch rpcCfg := rpcInfo.(type) {
case *config.HTTPConfig:
name = "HTTP"
uri = fmt.Sprintf("%s:%d", rpcCfg.Addr, rpcCfg.Port)
enabled = rpcCfg.Enabled
availableAPINames = rpcCfg.API
handler = node.NewHTTPHandlerStack(srv, s.cfg.API.HTTPConfig.CORSDomain, s.cfg.API.HTTPConfig.VHosts)
keepAlive = rpcCfg.KeepAlive
handler = node.NewHTTPHandlerStack(rpcSrv, s.cfg.API.HTTPConfig.CORSDomain, s.cfg.API.HTTPConfig.VHosts)
case *config.WSConfig:
name = "WS"
uri = fmt.Sprintf("%s:%d", rpcCfg.Addr, rpcCfg.Port)
enabled = rpcCfg.Enabled
availableAPINames = rpcCfg.API
handler = srv.WebsocketHandler(s.cfg.API.WSConfig.Origins)
handler = rpcSrv.WebsocketHandler(s.cfg.API.WSConfig.Origins)
default:
s.logger.Info("Config for Web3 RPC not properly configured, skipping")
return nil
}

srv := &http.Server{
Addr: uri,
Handler: handler,
ReadTimeout: 5 * time.Second,
WriteTimeout: 10 * time.Second,
}
srv.SetKeepAlivesEnabled(keepAlive)

if !enabled {
s.logger.Info("Web3 " + name + " RPC server not enabled, skipping")
return nil
Expand All @@ -85,7 +95,7 @@ func (s *Server) start(rpcInfo interface{}, apis map[string]rpctypes.Web3Service
}
}

if err := RegisterApis(srv, availableAPIs); err != nil {
if err := RegisterApis(rpcSrv, availableAPIs); err != nil {
return err
}

Expand All @@ -96,10 +106,10 @@ func (s *Server) start(rpcInfo interface{}, apis map[string]rpctypes.Web3Service
}()

go func(ch chan error) {
defer srv.Stop()
defer rpcSrv.Stop()

s.logger.Info("starting Web3 " + name + " RPC server on " + uri)
err := http.ListenAndServe(uri, handler)
err := srv.ListenAndServe()
if err != nil {
s.logger.Fatalf("server: %s", err)
}
Expand Down

0 comments on commit 2ff18ba

Please sign in to comment.