Skip to content

Commit

Permalink
gateway: add configurable response write timeout
Browse files Browse the repository at this point in the history
Closes #812
  • Loading branch information
gammazero committed Jan 28, 2025
1 parent b0125d1 commit 689d492
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
5 changes: 5 additions & 0 deletions gateway/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ type Config struct {
// directory listings, DAG previews and errors. These will be displayed to the
// right of "About IPFS" and "Install IPFS".
Menu []assets.MenuItem

// ResponseWriteTimeout is the maximum duration the gateway will wait for a
// response to be written before returning a 503 Service Unavailable error.
// An unset or zero value uses the default timeout. -1 means no timeout.
ResponseWriteTimeout time.Duration
}

// PublicGateway is the specification of an IPFS Public Gateway.
Expand Down
14 changes: 13 additions & 1 deletion gateway/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ import (
var log = logging.Logger("boxo/gateway")

const (
defaultResponseTimeout = 30 * time.Second

ipfsPathPrefix = "/ipfs/"
ipnsPathPrefix = ipns.NamespacePrefix
immutableCacheControl = "public, max-age=29030400, immutable"
Expand Down Expand Up @@ -68,7 +70,17 @@ type handler struct {
//
// [IPFS HTTP Gateway]: https://specs.ipfs.tech/http-gateways/
func NewHandler(c Config, backend IPFSBackend) http.Handler {
return newHandlerWithMetrics(&c, backend)
handler := newHandlerWithMetrics(&c, backend)

timeout := c.ResponseWriteTimeout
switch timeout {
case 0:
timeout = defaultResponseTimeout
case -1:
return handler
}

return http.TimeoutHandler(handler, timeout, "")
}

// serveContent replies to the request using the content in the provided Reader
Expand Down

0 comments on commit 689d492

Please sign in to comment.