Skip to content

Commit

Permalink
Changes to http server settings
Browse files Browse the repository at this point in the history
  • Loading branch information
Marko Boben committed Mar 30, 2024
1 parent fdee67e commit cd951df
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 5 deletions.
48 changes: 43 additions & 5 deletions avalanchego/api/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"time"

"github.com/NYTimes/gziphandler"
"golang.org/x/net/http2"

"github.com/gorilla/handlers"

Expand All @@ -29,7 +30,10 @@ import (
"github.com/flare-foundation/flare/utils/logging"
)

const baseURL = "/ext"
const (
baseURL = "/ext"
maxConcurrentStreams = 64
)

var (
errUnknownLockOption = errors.New("invalid lock options")
Expand All @@ -41,6 +45,13 @@ type RouteAdder interface {
AddRoute(handler *common.HTTPHandler, lock *sync.RWMutex, base, endpoint string, loggingWriter io.Writer) error
}

type HTTPConfig struct {
ReadTimeout time.Duration `json:"readTimeout"`
ReadHeaderTimeout time.Duration `json:"readHeaderTimeout"`
WriteTimeout time.Duration `json:"writeHeaderTimeout"`
IdleTimeout time.Duration `json:"idleTimeout"`
}

// Server maintains the HTTP router
type Server struct {
// log this server writes to
Expand All @@ -59,12 +70,15 @@ type Server struct {
router *router

srv *http.Server

httpConfig *HTTPConfig
}

// Initialize creates the API server at the provided host and port
func (s *Server) Initialize(
log logging.Logger,
factory logging.Factory,
httpConfig *HTTPConfig,
host string,
port uint16,
allowedOrigins []string,
Expand All @@ -74,13 +88,12 @@ func (s *Server) Initialize(
) {
s.log = log
s.factory = factory
s.httpConfig = httpConfig
s.listenHost = host
s.listenPort = port
s.shutdownTimeout = shutdownTimeout
s.router = newRouter()

s.log.Info("API created with allowed origins: %v", allowedOrigins)

corsHandler := cors.New(cors.Options{
AllowedOrigins: allowedOrigins,
AllowCredentials: true,
Expand All @@ -97,6 +110,25 @@ func (s *Server) Initialize(
for _, wrapper := range wrappers {
s.handler = wrapper.WrapHandler(s.handler)
}

}

func (s *Server) newHttpServer(listenAddress string) (*http.Server, error) {
httpServer := &http.Server{
Addr: listenAddress,
ReadTimeout: s.httpConfig.ReadTimeout,
ReadHeaderTimeout: s.httpConfig.ReadHeaderTimeout,
WriteTimeout: s.httpConfig.WriteTimeout,
IdleTimeout: s.httpConfig.IdleTimeout,
Handler: s.handler,
}
err := http2.ConfigureServer(httpServer, &http2.Server{
MaxConcurrentStreams: maxConcurrentStreams,
})
if err != nil {
return nil, err
}
return httpServer, nil
}

// Dispatch starts the API server
Expand All @@ -114,7 +146,10 @@ func (s *Server) Dispatch() error {
s.log.Info("HTTP API server listening on \"%s:%d\"", s.listenHost, ipDesc.Port)
}

s.srv = &http.Server{Handler: s.handler}
s.srv, err = s.newHttpServer("")
if err != nil {
return err
}
return s.srv.Serve(listener)
}

Expand Down Expand Up @@ -142,7 +177,10 @@ func (s *Server) DispatchTLS(certBytes, keyBytes []byte) error {
s.log.Info("HTTPS API server listening on \"%s:%d\"", s.listenHost, ipDesc.Port)
}

s.srv = &http.Server{Addr: listenAddress, Handler: s.handler}
s.srv, err = s.newHttpServer(listenAddress)
if err != nil {
return err
}
return s.srv.Serve(listener)
}

Expand Down
7 changes: 7 additions & 0 deletions avalanchego/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (

"github.com/spf13/viper"

"github.com/flare-foundation/flare/api/server"
"github.com/flare-foundation/flare/app/runner"
"github.com/flare-foundation/flare/chains"
"github.com/flare-foundation/flare/genesis"
Expand Down Expand Up @@ -232,6 +233,12 @@ func getHTTPConfig(v *viper.Viper) (node.HTTPConfig, error) {
}

config := node.HTTPConfig{
HTTPConfig: server.HTTPConfig{
ReadTimeout: v.GetDuration(HTTPReadTimeoutKey),
ReadHeaderTimeout: v.GetDuration(HTTPReadHeaderTimeoutKey),
WriteTimeout: v.GetDuration(HTTPWriteTimeoutKey),
IdleTimeout: v.GetDuration(HTTPIdleTimeoutKey),
},
APIConfig: node.APIConfig{
APIIndexerConfig: node.APIIndexerConfig{
IndexAPIEnabled: v.GetBool(IndexEnabledKey),
Expand Down
4 changes: 4 additions & 0 deletions avalanchego/config/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,10 @@ func addNodeFlags(fs *flag.FlagSet) {
fs.String(HTTPAllowedOrigins, "*", "Origins to allow on the HTTP port. Defaults to * which allows all origins. Example: https://*.avax.network https://*.avax-test.network")
fs.Duration(HTTPShutdownWaitKey, 0, "Duration to wait after receiving SIGTERM or SIGINT before initiating shutdown. The /health endpoint will return unhealthy during this duration")
fs.Duration(HTTPShutdownTimeoutKey, 10*time.Second, "Maximum duration to wait for existing connections to complete during node shutdown")
fs.Duration(HTTPReadTimeoutKey, 30*time.Second, "Maximum duration for reading the entire request, including the body. A zero or negative value means there will be no timeout")
fs.Duration(HTTPReadHeaderTimeoutKey, 30*time.Second, fmt.Sprintf("Maximum duration to read request headers. The connection's read deadline is reset after reading the headers. If %s is zero, the value of %s is used. If both are zero, there is no timeout.", HTTPReadHeaderTimeoutKey, HTTPReadTimeoutKey))
fs.Duration(HTTPWriteTimeoutKey, 30*time.Second, "Maximum duration before timing out writes of the response. It is reset whenever a new request's header is read. A zero or negative value means there will be no timeout.")
fs.Duration(HTTPIdleTimeoutKey, 120*time.Second, fmt.Sprintf("Maximum duration to wait for the next request when keep-alives are enabled. If %s is zero, the value of %s is used. If both are zero, there is no timeout.", HTTPIdleTimeoutKey, HTTPReadTimeoutKey))
fs.Bool(APIAuthRequiredKey, false, "Require authorization token to call HTTP APIs")
fs.String(APIAuthPasswordFileKey, "",
fmt.Sprintf("Password file used to initially create/validate API authorization tokens. Ignored if %s is specified. Leading and trailing whitespace is removed from the password. Can be changed via API call",
Expand Down
4 changes: 4 additions & 0 deletions avalanchego/config/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ const (
HTTPAllowedOrigins = "http-allowed-origins"
HTTPShutdownTimeoutKey = "http-shutdown-timeout"
HTTPShutdownWaitKey = "http-shutdown-wait"
HTTPReadTimeoutKey = "http-read-timeout"
HTTPReadHeaderTimeoutKey = "http-read-header-timeout"
HTTPWriteTimeoutKey = "http-write-timeout"
HTTPIdleTimeoutKey = "http-idle-timeout"
APIAuthRequiredKey = "api-auth-required"
APIAuthPasswordKey = "api-auth-password"
APIAuthPasswordFileKey = "api-auth-password-file"
Expand Down
2 changes: 2 additions & 0 deletions avalanchego/node/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"crypto/tls"
"time"

"github.com/flare-foundation/flare/api/server"
"github.com/flare-foundation/flare/chains"
"github.com/flare-foundation/flare/genesis"
"github.com/flare-foundation/flare/ids"
Expand Down Expand Up @@ -40,6 +41,7 @@ type APIIndexerConfig struct {
}

type HTTPConfig struct {
server.HTTPConfig
APIConfig `json:"apiConfig"`
HTTPHost string `json:"httpHost"`
HTTPPort uint16 `json:"httpPort"`
Expand Down
2 changes: 2 additions & 0 deletions avalanchego/node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,7 @@ func (n *Node) initAPIServer() error {
n.APIServer.Initialize(
n.Log,
n.LogFactory,
&n.Config.HTTPConfig.HTTPConfig,
n.Config.HTTPHost,
n.Config.HTTPPort,
n.Config.APIAllowedOrigins,
Expand All @@ -512,6 +513,7 @@ func (n *Node) initAPIServer() error {
n.APIServer.Initialize(
n.Log,
n.LogFactory,
&n.Config.HTTPConfig.HTTPConfig,
n.Config.HTTPHost,
n.Config.HTTPPort,
n.Config.APIAllowedOrigins,
Expand Down

0 comments on commit cd951df

Please sign in to comment.