Skip to content

Commit

Permalink
chore: clean up comments
Browse files Browse the repository at this point in the history
  • Loading branch information
2color committed Dec 4, 2024
1 parent b4da9cd commit d00fcb4
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 5 deletions.
2 changes: 1 addition & 1 deletion cached_addr_book_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func TestGetCachedAddrs(t *testing.T) {
}

func TestBackground(t *testing.T) {
t.Skip("skipping until we have a better way to test background")
t.Skip("skipping until this test is less flaky")
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

Expand Down
10 changes: 6 additions & 4 deletions server_cached_router.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,16 +61,17 @@ func (r cachedRouter) FindProviders(ctx context.Context, key cid.Cid, limit int)
return nil, err
}

Check warning on line 62 in server_cached_router.go

View check run for this annotation

Codecov / codecov/patch

server_cached_router.go#L61-L62

Added lines #L61 - L62 were not covered by tests

return NewCacheFallbackIter(it, r, ctx), nil // create a new iterator that will use cache if available and fallback to `FindPeer` if no addresses are cached
return NewCacheFallbackIter(it, r, ctx), nil
}

// TODO: Open question: should we implement FindPeers to look up cache? If a FindPeer fails to return any peers, the peer is likely long offline.
// TODO: Open question: should FindPeers look up cache? If a FindPeer fails to return any peers, the peer is likely long offline.
// So it's not clear that we gain anything by returning a cached peer that is likely offline.
// func (r cachedRouter) FindPeers(ctx context.Context, pid peer.ID, limit int) (iter.ResultIter[*types.PeerRecord], error) {
// return r.router.FindPeers(ctx, pid, limit)
// }

// withAddrsFromCache returns the best list of addrs for specified [peer.ID].
// It will consult cache only if the addrs slice passed to it is empty.
// It will consult cache ONLY if the addrs slice passed to it is empty.
func (r cachedRouter) withAddrsFromCache(queryOrigin string, pid *peer.ID, addrs []types.Multiaddr) []types.Multiaddr {
// skip cache if we already have addrs
if len(addrs) > 0 {
Expand All @@ -92,7 +93,6 @@ func (r cachedRouter) withAddrsFromCache(queryOrigin string, pid *peer.ID, addrs

var _ iter.ResultIter[types.Record] = &cacheFallbackIter{}

// cacheFallbackIter is a wrapper around a results iterator that will resolve peers with no addresses from cache and if no cached addresses, will look them up via FindPeers.
type cacheFallbackIter struct {
sourceIter iter.ResultIter[types.Record]
current iter.Result[types.Record]
Expand All @@ -103,6 +103,8 @@ type cacheFallbackIter struct {
ongoingLookups atomic.Int32
}

// NewCacheFallbackIter is a wrapper around a results iterator that will resolve peers with no addresses from cache and if no cached addresses, will look them up via FindPeers.
// It's a bit complex because it ensures we continue iterating without blocking on the FindPeers call.
func NewCacheFallbackIter(sourceIter iter.ResultIter[types.Record], router cachedRouter, ctx context.Context) *cacheFallbackIter {
ctx, cancel := context.WithCancel(ctx)
return &cacheFallbackIter{
Expand Down
1 change: 1 addition & 0 deletions server_cached_router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type mockResultIter[T any] struct {
closed bool
}

// Simple mock results iter that doesn't use channels
func newMockResultIter[T any](results []iter.Result[T]) *mockResultIter[T] {
return &mockResultIter[T]{
results: results,
Expand Down

0 comments on commit d00fcb4

Please sign in to comment.