Skip to content

Commit 686935e

Browse files
committed
temporary fix for pagination for hummingbird client
1 parent 7761a3a commit 686935e

5 files changed

Lines changed: 55 additions & 104 deletions

File tree

marketplace/service.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -664,10 +664,6 @@ func (s *Service) SearchAssets(ctx context.Context, req *connect.Request[humming
664664
tmp := addr.IA(*req.Msg.Ia)
665665
ia = &tmp
666666
}
667-
page := uint32(0)
668-
if req.Msg.Page != nil {
669-
page = *req.Msg.Page
670-
}
671667
pageSize := s.info.MaxReturnedAssets
672668
if req.Msg.MaxReturnedAssets != nil {
673669
pageSize = min(pageSize, *req.Msg.MaxReturnedAssets)
@@ -682,7 +678,7 @@ func (s *Service) SearchAssets(ctx context.Context, req *connect.Request[humming
682678
Price: req.Msg.Price,
683679
StartsAt: startsAt,
684680
StopsAt: stopsAt,
685-
Page: page,
681+
Page: req.Msg.Page,
686682
PageSize: pageSize,
687683
})
688684
if err != nil {

marketplace_client/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -925,7 +925,7 @@ func handleSearch(ctx context.Context, reader *bufio.Reader, c hummingbirdconnec
925925
msg.StopsAtEarliest = timestamppb.New(*stopsAtEarliest)
926926
}
927927
for page := uint32(0); ; page++ {
928-
msg.Page = &page
928+
msg.Page = page
929929
rep, err := c.SearchAssets(ctx, &connect.Request[hummingbird.SearchAssetsRequest]{
930930
Msg: msg,
931931
})

pkg/hummingbird/marketplace/client.go

Lines changed: 44 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,37 @@ func (c *MarketplaceClient) recursiveSelect(currentAsset *hummingbird.SearchAsse
328328

329329
}
330330

331+
func (c *MarketplaceClient) searchAllAssets(ctx context.Context, owned bool, ia *uint64, ingress *uint32, egress *uint32,
332+
minBW uint32, startsAtLatest time.Time, stopsAtLatest time.Time) ([]*hummingbird.SearchAsset, error) {
333+
// TODO: this is a temporary fix for pagination. We just fetch all pages
334+
// To properly implement pagination, especially for the combine assets case, the recursive select algorithm would have to be changed.
335+
var assets []*hummingbird.SearchAsset
336+
for i := uint32(0); ; i++ {
337+
resp, err := c.client.SearchAssets(ctx, &connect.Request[hummingbird.SearchAssetsRequest]{
338+
Msg: &hummingbird.SearchAssetsRequest{
339+
Owned: owned,
340+
Ia: ia,
341+
IfIdIngress: ingress,
342+
IfIdEgress: egress,
343+
MinRequiredBw: &minBW,
344+
StartsAtLatest: timestamppb.New(startsAtLatest),
345+
StopsAtEarliest: timestamppb.New(stopsAtLatest),
346+
Page: i,
347+
},
348+
})
349+
if err != nil {
350+
return nil, err
351+
}
352+
if len(resp.Msg.Assets) == 0 {
353+
break
354+
}
355+
for _, asset := range resp.Msg.Assets {
356+
assets = append(assets, asset)
357+
}
358+
}
359+
return assets, nil
360+
}
361+
331362
// checkoutAssetForInterfacePairWithCombine tries to find assets that can be combined on the time axis. It does not check for combinations on the bandwidth axis.
332363
func (c *MarketplaceClient) checkoutAssetForInterfacePairWithCombine(ctx context.Context, pair InterfacePair, bwInKbps uint32,
333364
startsAt time.Time, stopsAt time.Time, combineCost uint64) ([]*hummingbird.BuyAsset, error) {
@@ -336,61 +367,32 @@ func (c *MarketplaceClient) checkoutAssetForInterfacePairWithCombine(ctx context
336367
var ingressBuyAssets, egressBuyAssets, pairBuyAssets []*hummingbird.BuyAsset
337368
var ingressAssetPrice, egressAssetPrice, pairAssetPrice uint64
338369
var ok bool
339-
ingressAssetsResponse, err := c.client.SearchAssets(ctx, &connect.Request[hummingbird.SearchAssetsRequest]{
340-
Msg: &hummingbird.SearchAssetsRequest{
341-
Owned: false,
342-
Ia: &pair.IA,
343-
IfIdIngress: &pair.Ingress,
344-
MinRequiredBw: &bwInKbps,
345-
StartsAtLatest: timestamppb.New(stopsAt),
346-
StopsAtEarliest: timestamppb.New(startsAt),
347-
},
348-
})
370+
ingressAssetsResponse, err := c.searchAllAssets(ctx, false, &pair.IA, &pair.Ingress, nil, bwInKbps, stopsAt, startsAt)
349371
if err != nil {
350372
return nil, err
351373
}
352-
ingressSearchAssets := filter(ingressAssetsResponse.Msg.Assets, func(a *hummingbird.SearchAsset) bool {
374+
ingressSearchAssets := filter(ingressAssetsResponse, func(a *hummingbird.SearchAsset) bool {
353375
return a.IfIdEgress == nil
354376
})
355377
ingressBuyAssets, ingressAssetPrice, ok = c.recursiveSelectStart(ingressSearchAssets, bwInKbps, startsAt, stopsAt, combineCost)
356378
if ok {
357-
egressAssetsResponse, err := c.client.SearchAssets(ctx, &connect.Request[hummingbird.SearchAssetsRequest]{
358-
Msg: &hummingbird.SearchAssetsRequest{
359-
Owned: false,
360-
Ia: &pair.IA,
361-
IfIdEgress: &pair.Egress,
362-
MinRequiredBw: &bwInKbps,
363-
StartsAtLatest: timestamppb.New(stopsAt),
364-
StopsAtEarliest: timestamppb.New(startsAt),
365-
},
366-
})
379+
egressAssetsResponse, err := c.searchAllAssets(ctx, false, &pair.IA, nil, &pair.Egress, bwInKbps, stopsAt, startsAt)
367380
if err != nil {
368381
return nil, err
369382
}
370-
egressSearchAssets := filter(egressAssetsResponse.Msg.Assets, func(a *hummingbird.SearchAsset) bool {
383+
egressSearchAssets := filter(egressAssetsResponse, func(a *hummingbird.SearchAsset) bool {
371384
return a.IfIdIngress == nil
372385
})
373386
egressBuyAssets, egressAssetPrice, ok = c.recursiveSelectStart(egressSearchAssets, bwInKbps, startsAt, stopsAt, combineCost)
374387
if ok {
375388
ingressAndEgressSuccess = true
376389
}
377390
}
378-
379-
pairAssetsResponse, err := c.client.SearchAssets(ctx, &connect.Request[hummingbird.SearchAssetsRequest]{
380-
Msg: &hummingbird.SearchAssetsRequest{
381-
Owned: false,
382-
Ia: &pair.IA,
383-
IfIdIngress: &pair.Ingress,
384-
IfIdEgress: &pair.Egress,
385-
MinRequiredBw: &bwInKbps,
386-
StartsAtLatest: timestamppb.New(stopsAt),
387-
StopsAtEarliest: timestamppb.New(startsAt),
388-
},
389-
})
391+
pairAssetsResponse, err := c.searchAllAssets(ctx, false, &pair.IA, &pair.Ingress, &pair.Egress, bwInKbps, stopsAt, startsAt)
390392
if err != nil {
391393
return nil, err
392394
}
393-
pairBuyAssets, pairAssetPrice, ok = c.recursiveSelectStart(pairAssetsResponse.Msg.Assets, bwInKbps, startsAt, stopsAt, combineCost)
395+
pairBuyAssets, pairAssetPrice, ok = c.recursiveSelectStart(pairAssetsResponse, bwInKbps, startsAt, stopsAt, combineCost)
394396
if ok {
395397
if ingressAndEgressSuccess {
396398
if ingressAssetPrice+egressAssetPrice < pairAssetPrice {
@@ -414,56 +416,26 @@ func (c *MarketplaceClient) checkoutAssetForInterfacePairWithCombine(ctx context
414416
// The function does not combine assets. If no single (ingress-asset, egress-asset) tuple or interface-pair asset can satisfy the request, no buy order is returned.
415417
func (c *MarketplaceClient) checkoutAssetForInterfacePair(ctx context.Context, pair InterfacePair, bwInKbps uint32, startsAt time.Time, stopsAt time.Time) ([]*hummingbird.BuyAsset, error) {
416418
var ingressSearchAssets, egressSearchAssets, pairSearchAssets []*hummingbird.SearchAsset
417-
ingressAssetsResponse, err := c.client.SearchAssets(ctx, &connect.Request[hummingbird.SearchAssetsRequest]{
418-
Msg: &hummingbird.SearchAssetsRequest{
419-
Owned: false,
420-
Ia: &pair.IA,
421-
IfIdIngress: &pair.Ingress,
422-
MinRequiredBw: &bwInKbps,
423-
StartsAtLatest: timestamppb.New(startsAt),
424-
StopsAtEarliest: timestamppb.New(stopsAt),
425-
},
426-
})
419+
ingressAssetsResponse, err := c.searchAllAssets(ctx, false, &pair.IA, &pair.Ingress, nil, bwInKbps, startsAt, stopsAt)
427420
if err != nil {
428421
return nil, err
429422
}
430-
ingressSearchAssets = filter(ingressAssetsResponse.Msg.Assets, func(a *hummingbird.SearchAsset) bool {
423+
ingressSearchAssets = filter(ingressAssetsResponse, func(a *hummingbird.SearchAsset) bool {
431424
return a.IfIdEgress == nil
432425
})
433426
if len(ingressSearchAssets) != 0 {
434-
egressAssetsResponse, err := c.client.SearchAssets(ctx, &connect.Request[hummingbird.SearchAssetsRequest]{
435-
Msg: &hummingbird.SearchAssetsRequest{
436-
Owned: false,
437-
Ia: &pair.IA,
438-
IfIdEgress: &pair.Egress,
439-
MinRequiredBw: &bwInKbps,
440-
StartsAtLatest: timestamppb.New(startsAt),
441-
StopsAtEarliest: timestamppb.New(stopsAt),
442-
},
443-
})
427+
egressAssetsResponse, err := c.searchAllAssets(ctx, false, &pair.IA, nil, &pair.Ingress, bwInKbps, startsAt, stopsAt)
444428
if err != nil {
445429
return nil, err
446430
}
447-
egressSearchAssets = filter(egressAssetsResponse.Msg.Assets, func(a *hummingbird.SearchAsset) bool {
431+
egressSearchAssets = filter(egressAssetsResponse, func(a *hummingbird.SearchAsset) bool {
448432
return a.IfIdIngress == nil
449433
})
450434
}
451-
452-
pairAssetsResponse, err := c.client.SearchAssets(ctx, &connect.Request[hummingbird.SearchAssetsRequest]{
453-
Msg: &hummingbird.SearchAssetsRequest{
454-
Owned: false,
455-
Ia: &pair.IA,
456-
IfIdIngress: &pair.Ingress,
457-
IfIdEgress: &pair.Egress,
458-
MinRequiredBw: &bwInKbps,
459-
StartsAtLatest: timestamppb.New(startsAt),
460-
StopsAtEarliest: timestamppb.New(stopsAt),
461-
},
462-
})
435+
pairSearchAssets, err = c.searchAllAssets(ctx, false, &pair.IA, &pair.Ingress, &pair.Egress, bwInKbps, startsAt, stopsAt)
463436
if err != nil {
464437
return nil, err
465438
}
466-
pairSearchAssets = pairAssetsResponse.Msg.Assets
467439

468440
duration := stopsAt.Sub(startsAt)
469441
actualPrice := func(a *hummingbird.SearchAsset) uint64 {
@@ -605,31 +577,15 @@ func (c *MarketplaceClient) ObtainReservationsForInterfacePairs(ctx context.Cont
605577
}
606578
var foundAssets []*hummingbird.SearchAsset
607579
if combineAssets {
608-
foundAssetsResp, err := c.client.SearchAssets(ctx, &connect.Request[hummingbird.SearchAssetsRequest]{
609-
Msg: &hummingbird.SearchAssetsRequest{
610-
Owned: true,
611-
MinRequiredBw: &bwInKbps,
612-
StartsAtLatest: timestamppb.New(stopsAt),
613-
StopsAtEarliest: timestamppb.New(startsAt),
614-
},
615-
})
580+
foundAssets, err = c.searchAllAssets(ctx, true, nil, nil, nil, bwInKbps, stopsAt, startsAt)
616581
if err != nil {
617582
return nil, err
618583
}
619-
foundAssets = foundAssetsResp.Msg.Assets
620584
} else {
621-
foundAssetsResp, err := c.client.SearchAssets(ctx, &connect.Request[hummingbird.SearchAssetsRequest]{
622-
Msg: &hummingbird.SearchAssetsRequest{
623-
Owned: true,
624-
MinRequiredBw: &bwInKbps,
625-
StartsAtLatest: timestamppb.New(startsAt),
626-
StopsAtEarliest: timestamppb.New(stopsAt),
627-
},
628-
})
585+
foundAssets, err = c.searchAllAssets(ctx, true, nil, nil, nil, bwInKbps, startsAt, stopsAt)
629586
if err != nil {
630587
return nil, err
631588
}
632-
foundAssets = foundAssetsResp.Msg.Assets
633589
}
634590

635591
foundAssetsMap := make(map[uint64][]*hummingbird.SearchAsset)

pkg/proto/hummingbird/marketplace.pb.go

Lines changed: 8 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

proto/hummingbird/v1/marketplace.proto

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ message SearchAssetsRequest {
155155
optional google.protobuf.Timestamp starts_at_latest = 6;
156156
optional google.protobuf.Timestamp stops_at_earliest = 7;
157157
optional uint32 price = 8;
158-
optional uint32 page = 9;
158+
uint32 page = 9;
159159
// The marketplace may return less assets per page than requested.
160160
optional uint32 max_returned_assets = 10;
161161
}

0 commit comments

Comments
 (0)