Skip to content

Commit e38f236

Browse files
2colorlidel
andauthored
feat(routing/http/server): add routing timeout (#720)
* feat: add routing timeouts to delegated server * chore: update changelog * docs: CHANGELOG.md --------- Co-authored-by: Daniel N <[email protected]> Co-authored-by: Marcin Rataj <[email protected]>
1 parent 1bf1488 commit e38f236

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ The following emojis are used to highlight certain changes:
1717
### Added
1818

1919
- `routing/http/server`: added Prometheus instrumentation to http delegated routing endpoints.
20+
- `routing/http/server`: added configurable routing timeout (`DefaultRoutingTimeout` being 30s) to prevent indefinite hangs during content/peer routing. Set custom duration via `WithRoutingTimeout`.
2021

2122
### Changed
2223

routing/http/server/server.go

+26-4
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ const (
4141

4242
DefaultRecordsLimit = 20
4343
DefaultStreamingRecordsLimit = 0
44+
DefaultRoutingTimeout = 30 * time.Second
4445
)
4546

4647
var logger = logging.Logger("routing/http/server")
@@ -132,11 +133,18 @@ func WithPrometheusRegistry(reg prometheus.Registerer) Option {
132133
}
133134
}
134135

136+
func WithRoutingTimeout(timeout time.Duration) Option {
137+
return func(s *server) {
138+
s.routingTimeout = timeout
139+
}
140+
}
141+
135142
func Handler(svc ContentRouter, opts ...Option) http.Handler {
136143
server := &server{
137144
svc: svc,
138145
recordsLimit: DefaultRecordsLimit,
139146
streamingRecordsLimit: DefaultStreamingRecordsLimit,
147+
routingTimeout: DefaultRoutingTimeout,
140148
}
141149

142150
for _, opt := range opts {
@@ -174,6 +182,7 @@ type server struct {
174182
recordsLimit int
175183
streamingRecordsLimit int
176184
promRegistry prometheus.Registerer
185+
routingTimeout time.Duration
177186
}
178187

179188
func (s *server) detectResponseType(r *http.Request) (string, error) {
@@ -246,7 +255,10 @@ func (s *server) findProviders(w http.ResponseWriter, httpReq *http.Request) {
246255
recordsLimit = s.recordsLimit
247256
}
248257

249-
provIter, err := s.svc.FindProviders(httpReq.Context(), cid, recordsLimit)
258+
ctx, cancel := context.WithTimeout(httpReq.Context(), s.routingTimeout)
259+
defer cancel()
260+
261+
provIter, err := s.svc.FindProviders(ctx, cid, recordsLimit)
250262
if err != nil {
251263
if errors.Is(err, routing.ErrNotFound) {
252264
// handlerFunc takes care of setting the 404 and necessary headers
@@ -335,7 +347,11 @@ func (s *server) findPeers(w http.ResponseWriter, r *http.Request) {
335347
recordsLimit = s.recordsLimit
336348
}
337349

338-
provIter, err := s.svc.FindPeers(r.Context(), pid, recordsLimit)
350+
// Add timeout to the routing operation
351+
ctx, cancel := context.WithTimeout(r.Context(), s.routingTimeout)
352+
defer cancel()
353+
354+
provIter, err := s.svc.FindPeers(ctx, pid, recordsLimit)
339355
if err != nil {
340356
if errors.Is(err, routing.ErrNotFound) {
341357
// handlerFunc takes care of setting the 404 and necessary headers
@@ -466,7 +482,10 @@ func (s *server) GetIPNS(w http.ResponseWriter, r *http.Request) {
466482
return
467483
}
468484

469-
record, err := s.svc.GetIPNS(r.Context(), name)
485+
ctx, cancel := context.WithTimeout(r.Context(), s.routingTimeout)
486+
defer cancel()
487+
488+
record, err := s.svc.GetIPNS(ctx, name)
470489
if err != nil {
471490
if errors.Is(err, routing.ErrNotFound) {
472491
writeErr(w, "GetIPNS", http.StatusNotFound, fmt.Errorf("delegate error: %w", err))
@@ -550,7 +569,10 @@ func (s *server) PutIPNS(w http.ResponseWriter, r *http.Request) {
550569
return
551570
}
552571

553-
err = s.svc.PutIPNS(r.Context(), name, record)
572+
ctx, cancel := context.WithTimeout(r.Context(), s.routingTimeout)
573+
defer cancel()
574+
575+
err = s.svc.PutIPNS(ctx, name, record)
554576
if err != nil {
555577
writeErr(w, "PutIPNS", http.StatusInternalServerError, fmt.Errorf("delegate error: %w", err))
556578
return

0 commit comments

Comments
 (0)