Skip to content

Commit

Permalink
Pass boot socket directly to surveyor
Browse files Browse the repository at this point in the history
  • Loading branch information
lthibault committed Aug 18, 2023
1 parent 80d7eac commit 25abbc3
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 14 deletions.
3 changes: 2 additions & 1 deletion boot/boot.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,8 @@ func DialMulticast(h host.Host, maddr ma.Multiaddr, opt ...socket.Option) (*surv
return nil, err
}

return survey.New(h, conn, opt...), nil
sock := socket.New(conn, withDefault(h, opt)...)
return survey.New(h, sock), nil
}

func DialPortRange(h host.Host, maddr ma.Multiaddr, opt ...socket.Option) (*crawl.Crawler, error) {
Expand Down
12 changes: 2 additions & 10 deletions boot/survey/survey.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"encoding/binary"
"errors"
"net"
"time"

"github.com/libp2p/go-libp2p/core/crypto"
Expand All @@ -27,24 +26,17 @@ type Surveyor struct {

// New surveyor. The supplied PacketConn SHOULD be bound to a multicast
// group. Use of JoinMulticastGroup to construct conn is RECOMMENDED.
func New(h host.Host, conn net.PacketConn, opt ...socket.Option) *Surveyor {
func New(h host.Host, sock *socket.Socket) *Surveyor {
s := &Surveyor{
host: h,
sock: socket.New(conn, withDefault(h, opt)...),
sock: sock,
}

go s.sock.Bind(s.handler())

return s
}

func withDefault(h host.Host, opt []socket.Option) []socket.Option {
return append([]socket.Option{
socket.WithRateLimiter(socket.NewPacketLimiter(16, 8)),
socket.WithValidator(socket.BasicValidator(h.ID())),
}, opt...)
}

func (s *Surveyor) Close() error {
return s.sock.Close()
}
Expand Down
9 changes: 6 additions & 3 deletions boot/survey/survey_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ func TestClose(t *testing.T) {
ReadFrom(gomock.Any()).
AnyTimes()

err := survey.New(h, conn).Close()
sock := socket.New(conn)
err := survey.New(h, sock).Close()
assert.NoError(t, err, "surveyor should close gracefully")
}

Expand Down Expand Up @@ -90,7 +91,8 @@ func TestAdvertise(t *testing.T) {
Return(addr).
AnyTimes()

surveyor := survey.New(tt[0].h, conn)
sock := socket.New(conn)
surveyor := survey.New(tt[0].h, sock)
defer surveyor.Close()

surveyor.Advertise(context.Background(), ns)
Expand Down Expand Up @@ -145,7 +147,8 @@ func TestFindPeers(t *testing.T) {
Close().
Times(1)

surveyor := survey.New(tt[0].h, conn)
sock := socket.New(conn)
surveyor := survey.New(tt[0].h, sock)
defer surveyor.Close()

peers, err := surveyor.FindPeers(context.Background(), ns, discovery.Limit(1))
Expand Down

0 comments on commit 25abbc3

Please sign in to comment.