Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/add discovery client #3

Merged
merged 5 commits into from
Jul 8, 2019
Merged
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
59 changes: 30 additions & 29 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,34 +9,34 @@ import (
pb "github.com/libp2p/go-libp2p-rendezvous/pb"

ggio "github.com/gogo/protobuf/io"
host "github.com/libp2p/go-libp2p-host"
inet "github.com/libp2p/go-libp2p-net"
peer "github.com/libp2p/go-libp2p-peer"
pstore "github.com/libp2p/go-libp2p-peerstore"

"github.com/libp2p/go-libp2p-core/host"
inet "github.com/libp2p/go-libp2p-core/network"
"github.com/libp2p/go-libp2p-core/peer"
)

var (
DiscoverAsyncInterval = 2 * time.Minute
)

type RendezvousPoint interface {
Register(ctx context.Context, ns string, ttl int) error
Register(ctx context.Context, ns string, ttl int) (time.Duration, error)
Unregister(ctx context.Context, ns string) error
Discover(ctx context.Context, ns string, limit int, cookie []byte) ([]Registration, []byte, error)
DiscoverAsync(ctx context.Context, ns string) (<-chan Registration, error)
}

type Registration struct {
Peer pstore.PeerInfo
Peer peer.AddrInfo
Ns string
Ttl int
}

type RendezvousClient interface {
Register(ctx context.Context, ns string, ttl int) error
Register(ctx context.Context, ns string, ttl int) (time.Duration, error)
Unregister(ctx context.Context, ns string) error
Discover(ctx context.Context, ns string, limit int, cookie []byte) ([]pstore.PeerInfo, []byte, error)
DiscoverAsync(ctx context.Context, ns string) (<-chan pstore.PeerInfo, error)
Discover(ctx context.Context, ns string, limit int, cookie []byte) ([]peer.AddrInfo, []byte, error)
DiscoverAsync(ctx context.Context, ns string) (<-chan peer.AddrInfo, error)
}

func NewRendezvousPoint(host host.Host, p peer.ID) RendezvousPoint {
Expand All @@ -63,52 +63,53 @@ type rendezvousClient struct {
rp RendezvousPoint
}

func (rp *rendezvousPoint) Register(ctx context.Context, ns string, ttl int) error {
func (rp *rendezvousPoint) Register(ctx context.Context, ns string, ttl int) (time.Duration, error) {
s, err := rp.host.NewStream(ctx, rp.p, RendezvousProto)
if err != nil {
return err
return 0, err
}
defer s.Close()

r := ggio.NewDelimitedReader(s, inet.MessageSizeMax)
w := ggio.NewDelimitedWriter(s)

req := newRegisterMessage(ns, pstore.PeerInfo{ID: rp.host.ID(), Addrs: rp.host.Addrs()}, ttl)
req := newRegisterMessage(ns, peer.AddrInfo{ID: rp.host.ID(), Addrs: rp.host.Addrs()}, ttl)
err = w.WriteMsg(req)
if err != nil {
return err
return 0, err
}

var res pb.Message
err = r.ReadMsg(&res)
if err != nil {
return err
return 0, err
}

if res.GetType() != pb.Message_REGISTER_RESPONSE {
return fmt.Errorf("Unexpected response: %s", res.GetType().String())
return 0, fmt.Errorf("Unexpected response: %s", res.GetType().String())
}

status := res.GetRegisterResponse().GetStatus()
response := res.GetRegisterResponse()
status := response.GetStatus()
if status != pb.Message_OK {
return RendezvousError{Status: status, Text: res.GetRegisterResponse().GetStatusText()}
return 0, RendezvousError{Status: status, Text: res.GetRegisterResponse().GetStatusText()}
}

return nil
return time.Duration(*response.Ttl) * time.Second, nil
}

func (rc *rendezvousClient) Register(ctx context.Context, ns string, ttl int) error {
func (rc *rendezvousClient) Register(ctx context.Context, ns string, ttl int) (time.Duration, error) {
if ttl < 120 {
return fmt.Errorf("registration TTL is too short")
return 0, fmt.Errorf("registration TTL is too short")
}

err := rc.rp.Register(ctx, ns, ttl)
returnedTTL, err := rc.rp.Register(ctx, ns, ttl)
if err != nil {
return err
return 0, err
}

go registerRefresh(ctx, rc.rp, ns, ttl)
return nil
return returnedTTL, nil
}

func registerRefresh(ctx context.Context, rz RendezvousPoint, ns string, ttl int) {
Expand All @@ -133,7 +134,7 @@ func registerRefresh(ctx context.Context, rz RendezvousPoint, ns string, ttl int
return
}

err := rz.Register(ctx, ns, ttl)
_, err := rz.Register(ctx, ns, ttl)
if err != nil {
log.Errorf("Error registering [%s]: %s", ns, err.Error())
errcount++
Expand Down Expand Up @@ -264,32 +265,32 @@ func discoverAsync(ctx context.Context, ns string, s inet.Stream, ch chan Regist
}
}

func (rc *rendezvousClient) Discover(ctx context.Context, ns string, limit int, cookie []byte) ([]pstore.PeerInfo, []byte, error) {
func (rc *rendezvousClient) Discover(ctx context.Context, ns string, limit int, cookie []byte) ([]peer.AddrInfo, []byte, error) {
regs, cookie, err := rc.rp.Discover(ctx, ns, limit, cookie)
if err != nil {
return nil, nil, err
}

pinfos := make([]pstore.PeerInfo, len(regs))
pinfos := make([]peer.AddrInfo, len(regs))
for i, reg := range regs {
pinfos[i] = reg.Peer
}

return pinfos, cookie, nil
}

func (rc *rendezvousClient) DiscoverAsync(ctx context.Context, ns string) (<-chan pstore.PeerInfo, error) {
func (rc *rendezvousClient) DiscoverAsync(ctx context.Context, ns string) (<-chan peer.AddrInfo, error) {
rch, err := rc.rp.DiscoverAsync(ctx, ns)
if err != nil {
return nil, err
}

ch := make(chan pstore.PeerInfo)
ch := make(chan peer.AddrInfo)
go discoverPeersAsync(ctx, rch, ch)
return ch, nil
}

func discoverPeersAsync(ctx context.Context, rch <-chan Registration, ch chan pstore.PeerInfo) {
func discoverPeersAsync(ctx context.Context, rch <-chan Registration, ch chan peer.AddrInfo) {
defer close(ch)
for {
select {
Expand Down
21 changes: 15 additions & 6 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"testing"
"time"

host "github.com/libp2p/go-libp2p-host"
pstore "github.com/libp2p/go-libp2p-peerstore"
"github.com/libp2p/go-libp2p-core/host"
"github.com/libp2p/go-libp2p-core/peer"
)

func getRendezvousClients(t *testing.T, hosts []host.Host) []RendezvousClient {
Expand All @@ -31,10 +31,13 @@ func TestClientRegistrationAndDiscovery(t *testing.T) {

clients := getRendezvousClients(t, hosts)

err = clients[0].Register(ctx, "foo1", DefaultTTL)
recordTTL, err := clients[0].Register(ctx, "foo1", DefaultTTL)
if err != nil {
t.Fatal(err)
}
if recordTTL != DefaultTTL*time.Second {
t.Fatalf("Expected record TTL to be %d seconds", DefaultTTL)
}

pi, cookie, err := clients[0].Discover(ctx, "foo1", 0, nil)
if err != nil {
Expand All @@ -46,10 +49,13 @@ func TestClientRegistrationAndDiscovery(t *testing.T) {
checkPeerInfo(t, pi[0], hosts[1])

for i, client := range clients[1:] {
err = client.Register(ctx, "foo1", DefaultTTL)
recordTTL, err = client.Register(ctx, "foo1", DefaultTTL)
if err != nil {
t.Fatal(err)
}
if recordTTL != DefaultTTL*time.Second {
t.Fatalf("Expected record TTL to be %d seconds", DefaultTTL)
}

pi, cookie, err = clients[0].Discover(ctx, "foo1", 10, cookie)
if err != nil {
Expand Down Expand Up @@ -98,10 +104,13 @@ func TestClientRegistrationAndDiscoveryAsync(t *testing.T) {
}

for i, client := range clients[0:] {
err = client.Register(ctx, "foo1", DefaultTTL)
recordTTL, err := client.Register(ctx, "foo1", DefaultTTL)
if err != nil {
t.Fatal(err)
}
if recordTTL != DefaultTTL*time.Second {
t.Fatalf("Expected record TTL to be %d seconds", DefaultTTL)
}

pi := <-ch
checkPeerInfo(t, pi, hosts[1+i])
Expand All @@ -110,7 +119,7 @@ func TestClientRegistrationAndDiscoveryAsync(t *testing.T) {
DiscoverAsyncInterval = 2 * time.Minute
}

func checkPeerInfo(t *testing.T, pi pstore.PeerInfo, host host.Host) {
func checkPeerInfo(t *testing.T, pi peer.AddrInfo, host host.Host) {
if pi.ID != host.ID() {
t.Fatal("bad registration: peer ID doesn't match host ID")
}
Expand Down
2 changes: 1 addition & 1 deletion db/dbi.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package dbi

import (
peer "github.com/libp2p/go-libp2p-peer"
"github.com/libp2p/go-libp2p-core/peer"
)

type RegistrationRecord struct {
Expand Down
2 changes: 1 addition & 1 deletion db/sqlite/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
_ "github.com/mattn/go-sqlite3"

logging "github.com/ipfs/go-log"
peer "github.com/libp2p/go-libp2p-peer"
"github.com/libp2p/go-libp2p-core/peer"
)

var log = logging.Logger("rendezvous/db")
Expand Down
2 changes: 1 addition & 1 deletion db/sqlite/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"testing"
"time"

peer "github.com/libp2p/go-libp2p-peer"
"github.com/libp2p/go-libp2p-core/peer"
ma "github.com/multiformats/go-multiaddr"
)

Expand Down
Loading