-
Notifications
You must be signed in to change notification settings - Fork 159
fix(routing/http): cap records after filtering #1157
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
Open
lidel
wants to merge
1
commit into
main
Choose a base branch
from
fix/post-filter-records-limit
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -69,6 +69,11 @@ type FindProvidersAsyncResponse struct { | |||||||||||||||||||||||||
| type DelegatedRouter interface { | ||||||||||||||||||||||||||
| // FindProviders searches for peers who are able to provide the given [cid.Cid]. | ||||||||||||||||||||||||||
| // Limit indicates the maximum amount of results to return; 0 means unbounded. | ||||||||||||||||||||||||||
| // | ||||||||||||||||||||||||||
| // The HTTP server in this package always calls FindProviders with a | ||||||||||||||||||||||||||
| // limit of 0 and caps the response itself, after filtering. It consumes | ||||||||||||||||||||||||||
| // the iterator lazily and Closes it once enough records are collected, | ||||||||||||||||||||||||||
| // so implementations should return results lazily and stop work on Close. | ||||||||||||||||||||||||||
| FindProviders(ctx context.Context, cid cid.Cid, limit int) (iter.ResultIter[types.Record], error) | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| // Deprecated: historic API from [IPIP-526], may be removed in a future version. | ||||||||||||||||||||||||||
|
|
@@ -78,6 +83,9 @@ type DelegatedRouter interface { | |||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| // FindPeers searches for peers who have the provided [peer.ID]. | ||||||||||||||||||||||||||
| // Limit indicates the maximum amount of results to return; 0 means unbounded. | ||||||||||||||||||||||||||
| // | ||||||||||||||||||||||||||
| // As with FindProviders, the HTTP server always calls FindPeers with a | ||||||||||||||||||||||||||
| // limit of 0 and caps the response itself, after filtering. | ||||||||||||||||||||||||||
| FindPeers(ctx context.Context, pid peer.ID, limit int) (iter.ResultIter[*types.PeerRecord], error) | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| // GetIPNS searches for an [ipns.Record] for the given [ipns.Name]. | ||||||||||||||||||||||||||
|
|
@@ -124,18 +132,20 @@ func WithStreamingResultsDisabled() Option { | |||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| // WithRecordsLimit sets a limit that will be passed to [ContentRouter.FindProviders] | ||||||||||||||||||||||||||
| // and [ContentRouter.FindPeers] for non-streaming requests (application/json). | ||||||||||||||||||||||||||
| // Default is [DefaultRecordsLimit]. | ||||||||||||||||||||||||||
| // WithRecordsLimit caps the number of records returned for non-streaming | ||||||||||||||||||||||||||
| // requests (application/json). The server applies the cap after filtering, | ||||||||||||||||||||||||||
| // so filtered-out records do not shrink the response. The delegate | ||||||||||||||||||||||||||
| // [ContentRouter] is always called with a limit of 0 (unbounded). | ||||||||||||||||||||||||||
| // A limit of 0 disables the cap. Default is [DefaultRecordsLimit]. | ||||||||||||||||||||||||||
| func WithRecordsLimit(limit int) Option { | ||||||||||||||||||||||||||
| return func(s *server) { | ||||||||||||||||||||||||||
| s.recordsLimit = limit | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| // WithStreamingRecordsLimit sets a limit that will be passed to [ContentRouter.FindProviders] | ||||||||||||||||||||||||||
| // and [ContentRouter.FindPeers] for streaming requests (application/x-ndjson). | ||||||||||||||||||||||||||
| // Default is [DefaultStreamingRecordsLimit]. | ||||||||||||||||||||||||||
| // WithStreamingRecordsLimit caps the number of records returned for | ||||||||||||||||||||||||||
| // streaming requests (application/x-ndjson). See [WithRecordsLimit] for | ||||||||||||||||||||||||||
| // how the cap is applied. Default is [DefaultStreamingRecordsLimit]. | ||||||||||||||||||||||||||
| func WithStreamingRecordsLimit(limit int) Option { | ||||||||||||||||||||||||||
| return func(s *server) { | ||||||||||||||||||||||||||
| s.streamingRecordsLimit = limit | ||||||||||||||||||||||||||
|
|
@@ -272,7 +282,7 @@ func (s *server) findProviders(w http.ResponseWriter, httpReq *http.Request) { | |||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| var ( | ||||||||||||||||||||||||||
| handlerFunc func(w http.ResponseWriter, provIter iter.ResultIter[types.Record], filterAddrs, filterProtocols []string) | ||||||||||||||||||||||||||
| handlerFunc func(w http.ResponseWriter, provIter iter.ResultIter[types.Record], recordsLimit int, filterAddrs, filterProtocols []string) | ||||||||||||||||||||||||||
| recordsLimit int | ||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
|
|
@@ -287,7 +297,13 @@ func (s *server) findProviders(w http.ResponseWriter, httpReq *http.Request) { | |||||||||||||||||||||||||
| ctx, cancel := context.WithTimeout(httpReq.Context(), s.routingTimeout) | ||||||||||||||||||||||||||
| defer cancel() | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| provIter, err := s.svc.FindProviders(ctx, cid, recordsLimit) | ||||||||||||||||||||||||||
| // Pass 0 (unbounded) to the delegate and enforce recordsLimit here, | ||||||||||||||||||||||||||
| // after filtering. Passing recordsLimit would let the delegate stop | ||||||||||||||||||||||||||
| // early, before filters run, so records dropped by filters would | ||||||||||||||||||||||||||
| // shrink the response below recordsLimit. The delegate returns | ||||||||||||||||||||||||||
| // results lazily; the limiting iterator closes it once the cap is | ||||||||||||||||||||||||||
| // reached. | ||||||||||||||||||||||||||
| provIter, err := s.svc.FindProviders(ctx, cid, 0) | ||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||
| if errors.Is(err, routing.ErrNotFound) { | ||||||||||||||||||||||||||
| // handlerFunc takes care of setting the 404 and necessary headers | ||||||||||||||||||||||||||
|
|
@@ -298,14 +314,15 @@ func (s *server) findProviders(w http.ResponseWriter, httpReq *http.Request) { | |||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| handlerFunc(w, provIter, filterAddrs, filterProtocols) | ||||||||||||||||||||||||||
| handlerFunc(w, provIter, recordsLimit, filterAddrs, filterProtocols) | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| func (s *server) findProvidersJSON(w http.ResponseWriter, provIter iter.ResultIter[types.Record], filterAddrs, filterProtocols []string) { | ||||||||||||||||||||||||||
| func (s *server) findProvidersJSON(w http.ResponseWriter, provIter iter.ResultIter[types.Record], recordsLimit int, filterAddrs, filterProtocols []string) { | ||||||||||||||||||||||||||
| defer provIter.Close() | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| filteredIter := filters.ApplyFiltersToIter(provIter, filterAddrs, filterProtocols) | ||||||||||||||||||||||||||
| providers, err := iter.ReadAllResults(filteredIter) | ||||||||||||||||||||||||||
| var limitedIter iter.ResultIter[types.Record] = iter.Limit(filteredIter, recordsLimit) | ||||||||||||||||||||||||||
| providers, err := iter.ReadAllResults(limitedIter) | ||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||
| writeErr(w, "FindProviders", http.StatusInternalServerError, fmt.Errorf("delegate error: %w", err)) | ||||||||||||||||||||||||||
| return | ||||||||||||||||||||||||||
|
|
@@ -321,10 +338,10 @@ func (s *server) findProvidersJSON(w http.ResponseWriter, provIter iter.ResultIt | |||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| func (s *server) findProvidersNDJSON(w http.ResponseWriter, provIter iter.ResultIter[types.Record], filterAddrs, filterProtocols []string) { | ||||||||||||||||||||||||||
| func (s *server) findProvidersNDJSON(w http.ResponseWriter, provIter iter.ResultIter[types.Record], recordsLimit int, filterAddrs, filterProtocols []string) { | ||||||||||||||||||||||||||
| filteredIter := filters.ApplyFiltersToIter(provIter, filterAddrs, filterProtocols) | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| writeResultsIterNDJSON(w, filteredIter) | ||||||||||||||||||||||||||
| var limitedIter iter.ResultIter[types.Record] = iter.Limit(filteredIter, recordsLimit) | ||||||||||||||||||||||||||
| writeResultsIterNDJSON(w, limitedIter) | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| func (s *server) findPeers(w http.ResponseWriter, r *http.Request) { | ||||||||||||||||||||||||||
|
|
@@ -346,7 +363,7 @@ func (s *server) findPeers(w http.ResponseWriter, r *http.Request) { | |||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| var ( | ||||||||||||||||||||||||||
| handlerFunc func(w http.ResponseWriter, provIter iter.ResultIter[*types.PeerRecord], filterAddrs, filterProtocols []string) | ||||||||||||||||||||||||||
| handlerFunc func(w http.ResponseWriter, provIter iter.ResultIter[*types.PeerRecord], recordsLimit int, filterAddrs, filterProtocols []string) | ||||||||||||||||||||||||||
| recordsLimit int | ||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
|
|
@@ -362,7 +379,9 @@ func (s *server) findPeers(w http.ResponseWriter, r *http.Request) { | |||||||||||||||||||||||||
| ctx, cancel := context.WithTimeout(r.Context(), s.routingTimeout) | ||||||||||||||||||||||||||
| defer cancel() | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| provIter, err := s.svc.FindPeers(ctx, pid, recordsLimit) | ||||||||||||||||||||||||||
| // Pass 0 (unbounded) to the delegate and enforce recordsLimit here, | ||||||||||||||||||||||||||
| // after filtering. See findProviders for the rationale. | ||||||||||||||||||||||||||
| provIter, err := s.svc.FindPeers(ctx, pid, 0) | ||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||
| if errors.Is(err, routing.ErrNotFound) { | ||||||||||||||||||||||||||
| // handlerFunc takes care of setting the 404 and necessary headers | ||||||||||||||||||||||||||
|
|
@@ -373,7 +392,7 @@ func (s *server) findPeers(w http.ResponseWriter, r *http.Request) { | |||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| handlerFunc(w, provIter, filterAddrs, filterProtocols) | ||||||||||||||||||||||||||
| handlerFunc(w, provIter, recordsLimit, filterAddrs, filterProtocols) | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| func (s *server) provide(w http.ResponseWriter, httpReq *http.Request) { | ||||||||||||||||||||||||||
|
|
@@ -438,12 +457,13 @@ func (s *server) provide(w http.ResponseWriter, httpReq *http.Request) { | |||||||||||||||||||||||||
| writeJSONResult(w, "Provide", resp) | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| func (s *server) findPeersJSON(w http.ResponseWriter, peersIter iter.ResultIter[*types.PeerRecord], filterAddrs, filterProtocols []string) { | ||||||||||||||||||||||||||
| func (s *server) findPeersJSON(w http.ResponseWriter, peersIter iter.ResultIter[*types.PeerRecord], recordsLimit int, filterAddrs, filterProtocols []string) { | ||||||||||||||||||||||||||
| defer peersIter.Close() | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| peersIter = filters.ApplyFiltersToPeerRecordIter(peersIter, filterAddrs, filterProtocols) | ||||||||||||||||||||||||||
| filteredIter := filters.ApplyFiltersToPeerRecordIter(peersIter, filterAddrs, filterProtocols) | ||||||||||||||||||||||||||
| var limitedIter iter.ResultIter[*types.PeerRecord] = iter.Limit(filteredIter, recordsLimit) | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| peers, err := iter.ReadAllResults(peersIter) | ||||||||||||||||||||||||||
| peers, err := iter.ReadAllResults(limitedIter) | ||||||||||||||||||||||||||
|
Comment on lines
461
to
+466
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same reasoning as above, no need to keep query longer than required.
Suggested change
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Or even better to close |
||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||
| writeErr(w, "FindPeers", http.StatusInternalServerError, fmt.Errorf("delegate error: %w", err)) | ||||||||||||||||||||||||||
| return | ||||||||||||||||||||||||||
|
|
@@ -459,7 +479,7 @@ func (s *server) findPeersJSON(w http.ResponseWriter, peersIter iter.ResultIter[ | |||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| func (s *server) findPeersNDJSON(w http.ResponseWriter, peersIter iter.ResultIter[*types.PeerRecord], filterAddrs, filterProtocols []string) { | ||||||||||||||||||||||||||
| func (s *server) findPeersNDJSON(w http.ResponseWriter, peersIter iter.ResultIter[*types.PeerRecord], recordsLimit int, filterAddrs, filterProtocols []string) { | ||||||||||||||||||||||||||
| // Convert PeerRecord to Record so that we can reuse the filtering logic from findProviders | ||||||||||||||||||||||||||
| mappedIter := iter.Map(peersIter, func(v iter.Result[*types.PeerRecord]) iter.Result[types.Record] { | ||||||||||||||||||||||||||
| if v.Err != nil || v.Val == nil { | ||||||||||||||||||||||||||
|
|
@@ -471,7 +491,8 @@ func (s *server) findPeersNDJSON(w http.ResponseWriter, peersIter iter.ResultIte | |||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| filteredIter := filters.ApplyFiltersToIter(mappedIter, filterAddrs, filterProtocols) | ||||||||||||||||||||||||||
| writeResultsIterNDJSON(w, filteredIter) | ||||||||||||||||||||||||||
| var limitedIter iter.ResultIter[types.Record] = iter.Limit(filteredIter, recordsLimit) | ||||||||||||||||||||||||||
| writeResultsIterNDJSON(w, limitedIter) | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| func (s *server) GetIPNS(w http.ResponseWriter, r *http.Request) { | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
provItercould be unconditionally closed afteriter.ReadAllResults(), which would cancel request on router faster. No need to wait for response to be sent.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or even better to close
limitedIter?