Skip to content

Commit 710f972

Browse files
committed
refactor: use streaming bool instead of count int
1 parent d4b75cf commit 710f972

File tree

3 files changed

+17
-37
lines changed

3 files changed

+17
-37
lines changed

routing/http/client/client_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ import (
2727

2828
type mockContentRouter struct{ mock.Mock }
2929

30-
func (m *mockContentRouter) FindProviders(ctx context.Context, key cid.Cid, count int) (iter.ResultIter[types.ProviderResponse], error) {
31-
args := m.Called(ctx, key, count)
30+
func (m *mockContentRouter) FindProviders(ctx context.Context, key cid.Cid, stream bool) (iter.ResultIter[types.ProviderResponse], error) {
31+
args := m.Called(ctx, key, stream)
3232
return args.Get(0).(iter.ResultIter[types.ProviderResponse]), args.Error(1)
3333
}
3434
func (m *mockContentRouter) ProvideBitswap(ctx context.Context, req *server.BitswapWriteProvideRequest) (time.Duration, error) {
@@ -302,7 +302,7 @@ func TestClient_FindProviders(t *testing.T) {
302302

303303
findProvsIter := iter.FromSlice(c.routerProvs)
304304

305-
router.On("FindProviders", mock.Anything, cid, 20).
305+
router.On("FindProviders", mock.Anything, cid, c.expStreamingResponse).
306306
Return(findProvsIter, c.routerErr)
307307

308308
provsIter, err := client.FindProviders(ctx, cid)

routing/http/server/server.go

Lines changed: 11 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,9 @@ type FindProvidersAsyncResponse struct {
4141
}
4242

4343
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)
4847
ProvideBitswap(ctx context.Context, req *BitswapWriteProvideRequest) (time.Duration, error)
4948
Provide(ctx context.Context, req *WriteProvideRequest) (types.ProviderResponse, error)
5049
}
@@ -72,27 +71,9 @@ func WithStreamingResultsDisabled() Option {
7271
}
7372
}
7473

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-
9174
func Handler(svc ContentRouter, opts ...Option) http.Handler {
9275
server := &server{
93-
svc: svc,
94-
recordsCount: 20,
95-
streamingRecordsCount: 0,
76+
svc: svc,
9677
}
9778

9879
for _, opt := range opts {
@@ -107,10 +88,8 @@ func Handler(svc ContentRouter, opts ...Option) http.Handler {
10788
}
10889

10990
type server struct {
110-
svc ContentRouter
111-
disableNDJSON bool
112-
recordsCount int
113-
streamingRecordsCount int
91+
svc ContentRouter
92+
disableNDJSON bool
11493
}
11594

11695
func (s *server) provide(w http.ResponseWriter, httpReq *http.Request) {
@@ -193,10 +172,11 @@ func (s *server) findProviders(w http.ResponseWriter, httpReq *http.Request) {
193172

194173
var supportsNDJSON bool
195174
var supportsJSON bool
196-
var count int
175+
var streaming bool
197176
acceptHeaders := httpReq.Header.Values("Accept")
198177
if len(acceptHeaders) == 0 {
199178
handlerFunc = s.findProvidersJSON
179+
streaming = false
200180
} else {
201181
for _, acceptHeader := range acceptHeaders {
202182
for _, accept := range strings.Split(acceptHeader, ",") {
@@ -209,25 +189,25 @@ func (s *server) findProviders(w http.ResponseWriter, httpReq *http.Request) {
209189
switch mediaType {
210190
case mediaTypeJSON, mediaTypeWildcard:
211191
supportsJSON = true
212-
count = s.recordsCount
213192
case mediaTypeNDJSON:
214193
supportsNDJSON = true
215-
count = s.streamingRecordsCount
216194
}
217195
}
218196
}
219197

220198
if supportsNDJSON && !s.disableNDJSON {
221199
handlerFunc = s.findProvidersNDJSON
200+
streaming = true
222201
} else if supportsJSON {
223202
handlerFunc = s.findProvidersJSON
203+
streaming = false
224204
} else {
225205
writeErr(w, "FindProviders", http.StatusBadRequest, errors.New("no supported content types"))
226206
return
227207
}
228208
}
229209

230-
provIter, err := s.svc.FindProviders(httpReq.Context(), cid, count)
210+
provIter, err := s.svc.FindProviders(httpReq.Context(), cid, streaming)
231211
if err != nil {
232212
writeErr(w, "FindProviders", http.StatusInternalServerError, fmt.Errorf("delegate error: %w", err))
233213
return

routing/http/server/server_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func TestHeaders(t *testing.T) {
3333
cb, err := cid.Decode(c)
3434
require.NoError(t, err)
3535

36-
router.On("FindProviders", mock.Anything, cb, 0).
36+
router.On("FindProviders", mock.Anything, cb, false).
3737
Return(results, nil)
3838

3939
resp, err := http.Get(serverAddr + ProvidePath + c)
@@ -118,8 +118,8 @@ func TestResponse(t *testing.T) {
118118

119119
type mockContentRouter struct{ mock.Mock }
120120

121-
func (m *mockContentRouter) FindProviders(ctx context.Context, key cid.Cid, count int) (iter.ResultIter[types.ProviderResponse], error) {
122-
args := m.Called(ctx, key, count)
121+
func (m *mockContentRouter) FindProviders(ctx context.Context, key cid.Cid, stream bool) (iter.ResultIter[types.ProviderResponse], error) {
122+
args := m.Called(ctx, key, stream)
123123
return args.Get(0).(iter.ResultIter[types.ProviderResponse]), args.Error(1)
124124
}
125125
func (m *mockContentRouter) ProvideBitswap(ctx context.Context, req *BitswapWriteProvideRequest) (time.Duration, error) {

0 commit comments

Comments
 (0)