@@ -41,10 +41,9 @@ type FindProvidersAsyncResponse struct {
41
41
}
42
42
43
43
type ContentRouter interface {
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 )
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 )
48
47
ProvideBitswap (ctx context.Context , req * BitswapWriteProvideRequest ) (time.Duration , error )
49
48
Provide (ctx context.Context , req * WriteProvideRequest ) (types.ProviderResponse , error )
50
49
}
@@ -72,27 +71,9 @@ func WithStreamingResultsDisabled() Option {
72
71
}
73
72
}
74
73
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
-
91
74
func Handler (svc ContentRouter , opts ... Option ) http.Handler {
92
75
server := & server {
93
- svc : svc ,
94
- recordsCount : 20 ,
95
- streamingRecordsCount : 0 ,
76
+ svc : svc ,
96
77
}
97
78
98
79
for _ , opt := range opts {
@@ -107,10 +88,8 @@ func Handler(svc ContentRouter, opts ...Option) http.Handler {
107
88
}
108
89
109
90
type server struct {
110
- svc ContentRouter
111
- disableNDJSON bool
112
- recordsCount int
113
- streamingRecordsCount int
91
+ svc ContentRouter
92
+ disableNDJSON bool
114
93
}
115
94
116
95
func (s * server ) provide (w http.ResponseWriter , httpReq * http.Request ) {
@@ -193,10 +172,11 @@ func (s *server) findProviders(w http.ResponseWriter, httpReq *http.Request) {
193
172
194
173
var supportsNDJSON bool
195
174
var supportsJSON bool
196
- var count int
175
+ var streaming bool
197
176
acceptHeaders := httpReq .Header .Values ("Accept" )
198
177
if len (acceptHeaders ) == 0 {
199
178
handlerFunc = s .findProvidersJSON
179
+ streaming = false
200
180
} else {
201
181
for _ , acceptHeader := range acceptHeaders {
202
182
for _ , accept := range strings .Split (acceptHeader , "," ) {
@@ -209,25 +189,25 @@ func (s *server) findProviders(w http.ResponseWriter, httpReq *http.Request) {
209
189
switch mediaType {
210
190
case mediaTypeJSON , mediaTypeWildcard :
211
191
supportsJSON = true
212
- count = s .recordsCount
213
192
case mediaTypeNDJSON :
214
193
supportsNDJSON = true
215
- count = s .streamingRecordsCount
216
194
}
217
195
}
218
196
}
219
197
220
198
if supportsNDJSON && ! s .disableNDJSON {
221
199
handlerFunc = s .findProvidersNDJSON
200
+ streaming = true
222
201
} else if supportsJSON {
223
202
handlerFunc = s .findProvidersJSON
203
+ streaming = false
224
204
} else {
225
205
writeErr (w , "FindProviders" , http .StatusBadRequest , errors .New ("no supported content types" ))
226
206
return
227
207
}
228
208
}
229
209
230
- provIter , err := s .svc .FindProviders (httpReq .Context (), cid , count )
210
+ provIter , err := s .svc .FindProviders (httpReq .Context (), cid , streaming )
231
211
if err != nil {
232
212
writeErr (w , "FindProviders" , http .StatusInternalServerError , fmt .Errorf ("delegate error: %w" , err ))
233
213
return
0 commit comments