@@ -41,7 +41,10 @@ type FindProvidersAsyncResponse struct {
41
41
}
42
42
43
43
type ContentRouter interface {
44
- FindProviders (ctx context.Context , key cid.Cid ) (iter.ResultIter [types.ProviderResponse ], error )
44
+ // FindProviders searches for peers who are able to provide a given key. Count
45
+ // indicates the maximum amount of providers we are looking for. If count is 0,
46
+ // the implementer can return an unbounded number of results.
47
+ FindProviders (ctx context.Context , key cid.Cid , count int ) (iter.ResultIter [types.ProviderResponse ], error )
45
48
ProvideBitswap (ctx context.Context , req * BitswapWriteProvideRequest ) (time.Duration , error )
46
49
Provide (ctx context.Context , req * WriteProvideRequest ) (types.ProviderResponse , error )
47
50
}
@@ -69,9 +72,27 @@ func WithStreamingResultsDisabled() Option {
69
72
}
70
73
}
71
74
75
+ // WithRecordsCount changes the amount of records asked for non-streaming requests.
76
+ // Default is 20.
77
+ func WithRecordsCount (count int ) Option {
78
+ return func (s * server ) {
79
+ s .recordsCount = count
80
+ }
81
+ }
82
+
83
+ // WithStreamingRecordsCount changes the amount of records asked for streaming requests.
84
+ // Default is 0 (unbounded).
85
+ func WithStreamingRecordsCount (count int ) Option {
86
+ return func (s * server ) {
87
+ s .streamingRecordsCount = count
88
+ }
89
+ }
90
+
72
91
func Handler (svc ContentRouter , opts ... Option ) http.Handler {
73
92
server := & server {
74
- svc : svc ,
93
+ svc : svc ,
94
+ recordsCount : 20 ,
95
+ streamingRecordsCount : 0 ,
75
96
}
76
97
77
98
for _ , opt := range opts {
@@ -86,8 +107,10 @@ func Handler(svc ContentRouter, opts ...Option) http.Handler {
86
107
}
87
108
88
109
type server struct {
89
- svc ContentRouter
90
- disableNDJSON bool
110
+ svc ContentRouter
111
+ disableNDJSON bool
112
+ recordsCount int
113
+ streamingRecordsCount int
91
114
}
92
115
93
116
func (s * server ) provide (w http.ResponseWriter , httpReq * http.Request ) {
@@ -170,6 +193,7 @@ func (s *server) findProviders(w http.ResponseWriter, httpReq *http.Request) {
170
193
171
194
var supportsNDJSON bool
172
195
var supportsJSON bool
196
+ var count int
173
197
acceptHeaders := httpReq .Header .Values ("Accept" )
174
198
if len (acceptHeaders ) == 0 {
175
199
handlerFunc = s .findProvidersJSON
@@ -185,8 +209,10 @@ func (s *server) findProviders(w http.ResponseWriter, httpReq *http.Request) {
185
209
switch mediaType {
186
210
case mediaTypeJSON , mediaTypeWildcard :
187
211
supportsJSON = true
212
+ count = s .recordsCount
188
213
case mediaTypeNDJSON :
189
214
supportsNDJSON = true
215
+ count = s .streamingRecordsCount
190
216
}
191
217
}
192
218
}
@@ -201,7 +227,7 @@ func (s *server) findProviders(w http.ResponseWriter, httpReq *http.Request) {
201
227
}
202
228
}
203
229
204
- provIter , err := s .svc .FindProviders (httpReq .Context (), cid )
230
+ provIter , err := s .svc .FindProviders (httpReq .Context (), cid , count )
205
231
if err != nil {
206
232
writeErr (w , "FindProviders" , http .StatusInternalServerError , fmt .Errorf ("delegate error: %w" , err ))
207
233
return
0 commit comments