Skip to content
This repository has been archived by the owner on May 11, 2022. It is now read-only.

fix stream deadlines #107

Merged
merged 3 commits into from
Aug 25, 2021
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
30 changes: 15 additions & 15 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,17 @@ package autonat
import (
"context"
"fmt"
"time"

pb "github.com/libp2p/go-libp2p-autonat/pb"
"github.com/libp2p/go-libp2p-core/host"
"github.com/libp2p/go-libp2p-core/network"
"github.com/libp2p/go-libp2p-core/peer"
"github.com/libp2p/go-msgio/protoio"

pb "github.com/libp2p/go-libp2p-autonat/pb"

protoio "github.com/libp2p/go-msgio/protoio"
ma "github.com/multiformats/go-multiaddr"
)

// Error wraps errors signalled by AutoNAT services
type Error struct {
Status pb.Message_ResponseStatus
Text string
}

// NewAutoNATClient creates a fresh instance of an AutoNATClient
// If addrFunc is nil, h.Addrs will be used
func NewAutoNATClient(h host.Host, addrFunc AddrFunc) Client {
Expand All @@ -34,11 +28,14 @@ type client struct {
addrFunc AddrFunc
}

// DialBack asks peer p to dial us back on all addresses returned by the addrFunc.
// It blocks until we've received a response from the peer.
func (c *client) DialBack(ctx context.Context, p peer.ID) (ma.Multiaddr, error) {
s, err := c.h.NewStream(ctx, p, AutoNATProto)
if err != nil {
return nil, err
}
s.SetDeadline(time.Now().Add(streamTimeout))
// Might as well just reset the stream. Once we get to this point, we
// don't care about being nice.
defer s.Close()
Expand All @@ -47,20 +44,18 @@ func (c *client) DialBack(ctx context.Context, p peer.ID) (ma.Multiaddr, error)
w := protoio.NewDelimitedWriter(s)

req := newDialMessage(peer.AddrInfo{ID: c.h.ID(), Addrs: c.addrFunc()})
err = w.WriteMsg(req)
if err != nil {
if err := w.WriteMsg(req); err != nil {
s.Reset()
return nil, err
}

var res pb.Message
err = r.ReadMsg(&res)
if err != nil {
if err := r.ReadMsg(&res); err != nil {
s.Reset()
return nil, err
}

if res.GetType() != pb.Message_DIAL_RESPONSE {
s.Reset()
return nil, fmt.Errorf("unexpected response: %s", res.GetType().String())
}

Expand All @@ -69,12 +64,17 @@ func (c *client) DialBack(ctx context.Context, p peer.ID) (ma.Multiaddr, error)
case pb.Message_OK:
addr := res.GetDialResponse().GetAddr()
return ma.NewMultiaddrBytes(addr)

default:
return nil, Error{Status: status, Text: res.GetDialResponse().GetStatusText()}
}
}

// Error wraps errors signalled by AutoNAT services
type Error struct {
Status pb.Message_ResponseStatus
Text string
}

func (e Error) Error() string {
return fmt.Sprintf("AutoNAT error: %s (%s)", e.Text, e.Status.String())
}
Expand Down
8 changes: 3 additions & 5 deletions svc.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,16 @@ import (
"sync"
"time"

pb "github.com/libp2p/go-libp2p-autonat/pb"
"github.com/libp2p/go-libp2p-core/network"
"github.com/libp2p/go-libp2p-core/peer"
"github.com/libp2p/go-libp2p-core/peerstore"

pb "github.com/libp2p/go-libp2p-autonat/pb"

"github.com/libp2p/go-msgio/protoio"
ma "github.com/multiformats/go-multiaddr"
)

var streamReadTimeout = 60 * time.Second
var streamTimeout = 60 * time.Second
Copy link
Member

Choose a reason for hiding this comment

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

we can probably reduce this to 5-10 seconds as well.

Copy link
Member

Choose a reason for hiding this comment

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

But we can punt that.


// AutoNATService provides NAT autodetection services to other peers
type autoNATService struct {
Expand Down Expand Up @@ -49,8 +48,7 @@ func newAutoNATService(ctx context.Context, c *config) (*autoNATService, error)
}

func (as *autoNATService) handleStream(s network.Stream) {
s.SetReadDeadline(time.Now().Add(streamReadTimeout))

s.SetDeadline(time.Now().Add(streamTimeout))
defer s.Close()

pid := s.Conn().RemotePeer()
Expand Down