Skip to content
This repository was archived by the owner on Feb 7, 2024. It is now read-only.

Add findprovs #47

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package shell

import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
Expand All @@ -13,6 +14,8 @@ import (
"strings"
"time"

pstore "github.com/libp2p/go-libp2p-peerstore"
notif "github.com/libp2p/go-libp2p-routing/notifications"
ma "github.com/multiformats/go-multiaddr"
manet "github.com/multiformats/go-multiaddr-net"
files "github.com/whyrusleeping/go-multipart-files"
Expand Down Expand Up @@ -371,6 +374,53 @@ func (s *Shell) FindPeer(peer string) (*PeerInfo, error) {
return &str.Responses[0], nil
}

func (s *Shell) FindProvs(ctx context.Context, cid string) (<-chan pstore.PeerInfo, error) {
ctx, cancel := context.WithCancel(ctx)

resp, err := s.newRequest("dht/findprovs", cid).Send(s.httpcli)
if err != nil {
return nil, err
}

if resp.Error != nil {
return nil, resp.Error
}

// 4 is arbitrary here just to make the channel buffered
outchan := make(chan pstore.PeerInfo, 4)

go func() {
defer close(outchan)
defer cancel()

var n notif.QueryEvent
decoder := json.NewDecoder(resp.Output)
for {
err := decoder.Decode(&n)
if err != nil {
return
}

if n.Type == notif.Provider {
for _, p := range n.Responses {
select {
case outchan <- *p:
case <-ctx.Done():
return
}
}
}
}
}()

go func() {
<-ctx.Done()
resp.Close()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I fail to see why not defer resp.Close() in the previous go-routine but I assume there is a reason?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, json.NewDecoder(resp.Output) will hang until the resp is closed. So this thing breaks out the NewDecoder.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wait, if NewDecoder hangs, then it's going to hang until someone cancels the context, because resp.Close() is not called until then, and the context won't be cancelled because NewDecoder is hanging ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We self cancel/exit when responses from the endpoint end. If someone wants to exit early he has to cancel context.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, not NewDecoder but decoder.Decode.

}()

return outchan, nil
}

func (s *Shell) Refs(hash string, recursive bool) (<-chan string, error) {
req := s.newRequest("refs", hash)
if recursive {
Expand Down
16 changes: 16 additions & 0 deletions shell_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package shell

import (
"bytes"
"context"
"crypto/md5"
"fmt"
"io"
Expand Down Expand Up @@ -212,3 +213,18 @@ func TestObjectStat(t *testing.T) {
is.Equal(stat.LinksSize, 3)
is.Equal(stat.CumulativeSize, 1688)
}

func TestFindProvs(t *testing.T) {
is := is.New(t)
s := NewShell(shellUrl)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

c, err := s.FindProvs(ctx, "Qme1g4e3m2SmdiSGGU3vSWmUStwUjc5oECnEriaK9Xa1HU")
is.Nil(err)

p := <-c
t.Logf("prov: %s", p)
is.NotNil(p)
is.NotNil(p.ID)
}