@@ -28,6 +28,9 @@ const (
28
28
mediaTypeJSON = "application/json"
29
29
mediaTypeNDJSON = "application/x-ndjson"
30
30
mediaTypeWildcard = "*/*"
31
+
32
+ DefaultRecordsLimit = 20
33
+ DefaultStreamingRecordsLimit = 0
31
34
)
32
35
33
36
var logger = logging .Logger ("service/server/delegatedrouting" )
@@ -41,9 +44,9 @@ type FindProvidersAsyncResponse struct {
41
44
}
42
45
43
46
type ContentRouter interface {
44
- // FindProviders searches for peers who are able to provide a given key. Stream
45
- // indicates whether or not this request will be responded as a stream .
46
- FindProviders (ctx context.Context , key cid.Cid , stream bool ) (iter.ResultIter [types.ProviderResponse ], error )
47
+ // FindProviders searches for peers who are able to provide a given key. Limit
48
+ // indicates the maximum amount of results to return. 0 means unbounded .
49
+ FindProviders (ctx context.Context , key cid.Cid , limit int ) (iter.ResultIter [types.ProviderResponse ], error )
47
50
ProvideBitswap (ctx context.Context , req * BitswapWriteProvideRequest ) (time.Duration , error )
48
51
Provide (ctx context.Context , req * WriteProvideRequest ) (types.ProviderResponse , error )
49
52
}
@@ -71,9 +74,27 @@ func WithStreamingResultsDisabled() Option {
71
74
}
72
75
}
73
76
77
+ // WithRecordsLimit sets a limit that will be passed to ContentRouter.FindProviders
78
+ // for non-streaming requests (application/json). Default is DefaultRecordsLimit.
79
+ func WithRecordsLimit (limit int ) Option {
80
+ return func (s * server ) {
81
+ s .recordsLimit = limit
82
+ }
83
+ }
84
+
85
+ // WithStreamingRecordsLimit sets a limit that will be passed to ContentRouter.FindProviders
86
+ // for streaming requests (application/x-ndjson). Default is DefaultStreamingRecordsLimit.
87
+ func WithStreamingRecordsLimit (limit int ) Option {
88
+ return func (s * server ) {
89
+ s .streamingRecordsLimit = limit
90
+ }
91
+ }
92
+
74
93
func Handler (svc ContentRouter , opts ... Option ) http.Handler {
75
94
server := & server {
76
- svc : svc ,
95
+ svc : svc ,
96
+ recordsLimit : DefaultRecordsLimit ,
97
+ streamingRecordsLimit : DefaultStreamingRecordsLimit ,
77
98
}
78
99
79
100
for _ , opt := range opts {
@@ -88,8 +109,10 @@ func Handler(svc ContentRouter, opts ...Option) http.Handler {
88
109
}
89
110
90
111
type server struct {
91
- svc ContentRouter
92
- disableNDJSON bool
112
+ svc ContentRouter
113
+ disableNDJSON bool
114
+ recordsLimit int
115
+ streamingRecordsLimit int
93
116
}
94
117
95
118
func (s * server ) provide (w http.ResponseWriter , httpReq * http.Request ) {
@@ -172,11 +195,11 @@ func (s *server) findProviders(w http.ResponseWriter, httpReq *http.Request) {
172
195
173
196
var supportsNDJSON bool
174
197
var supportsJSON bool
175
- var streaming bool
198
+ var recordsLimit int
176
199
acceptHeaders := httpReq .Header .Values ("Accept" )
177
200
if len (acceptHeaders ) == 0 {
178
201
handlerFunc = s .findProvidersJSON
179
- streaming = false
202
+ recordsLimit = s . recordsLimit
180
203
} else {
181
204
for _ , acceptHeader := range acceptHeaders {
182
205
for _ , accept := range strings .Split (acceptHeader , "," ) {
@@ -197,17 +220,17 @@ func (s *server) findProviders(w http.ResponseWriter, httpReq *http.Request) {
197
220
198
221
if supportsNDJSON && ! s .disableNDJSON {
199
222
handlerFunc = s .findProvidersNDJSON
200
- streaming = true
223
+ recordsLimit = s . streamingRecordsLimit
201
224
} else if supportsJSON {
202
225
handlerFunc = s .findProvidersJSON
203
- streaming = false
226
+ recordsLimit = s . recordsLimit
204
227
} else {
205
228
writeErr (w , "FindProviders" , http .StatusBadRequest , errors .New ("no supported content types" ))
206
229
return
207
230
}
208
231
}
209
232
210
- provIter , err := s .svc .FindProviders (httpReq .Context (), cid , streaming )
233
+ provIter , err := s .svc .FindProviders (httpReq .Context (), cid , recordsLimit )
211
234
if err != nil {
212
235
writeErr (w , "FindProviders" , http .StatusInternalServerError , fmt .Errorf ("delegate error: %w" , err ))
213
236
return
0 commit comments