Skip to content

gateway: add configurable response write timeout #818

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

Closed
wants to merge 2 commits into from
Closed
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ The following emojis are used to highlight certain changes:

### Added

- `gateway`: Configurable write timeout [#818](https://github.com/ipfs/boxo/pull/818)

### Changed

### Removed
Expand Down
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 @@
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 @@
//
// [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

Check warning on line 80 in gateway/handler.go

View check run for this annotation

Codecov / codecov/patch

gateway/handler.go#L79-L80

Added lines #L79 - L80 were not covered by tests
}

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

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