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

fix: add deadlines to write and read requests #81

Closed
wants to merge 2 commits into from
Closed
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
8 changes: 5 additions & 3 deletions dht_net.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
peer "github.com/libp2p/go-libp2p-peer"
)

var dhtReadMessageTimeout = time.Minute
const dhtMessageTimeout = 1 * time.Minute
var ErrReadTimeout = fmt.Errorf("timed out reading response")

type bufferedWriteCloser interface {
Expand Down Expand Up @@ -59,6 +59,7 @@ func (dht *IpfsDHT) handleNewMessage(s inet.Stream) {
for {
// receive msg
pmes := new(pb.Message)
s.SetReadDeadline(time.Now().Add(dhtMessageTimeout))
switch err := r.ReadMsg(pmes); err {
case io.EOF:
s.Close()
Expand Down Expand Up @@ -96,6 +97,7 @@ func (dht *IpfsDHT) handleNewMessage(s inet.Stream) {
}

// send out response msg
s.SetWriteDeadline(time.Now().Add(dhtMessageTimeout))
Copy link
Member

Choose a reason for hiding this comment

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

Probably best to fix libp2p/go-mplex#7 first.

err = w.WriteMsg(rpmes)
if err == nil {
err = w.Flush()
Expand Down Expand Up @@ -232,7 +234,6 @@ func (ms *messageSender) prep() error {
if err != nil {
return err
}

ms.r = ggio.NewDelimitedReader(nstr, inet.MessageSizeMax)
ms.w = newBufferedDelimitedWriter(nstr)
ms.s = nstr
Expand Down Expand Up @@ -342,10 +343,11 @@ func (ms *messageSender) writeMsg(pmes *pb.Message) error {
func (ms *messageSender) ctxReadMsg(ctx context.Context, mes *pb.Message) error {
errc := make(chan error, 1)
go func(r ggio.ReadCloser) {
ms.s.SetReadDeadline(time.Now().Add(dhtMessageTimeout))
errc <- r.ReadMsg(mes)
}(ms.r)

t := time.NewTimer(dhtReadMessageTimeout)
t := time.NewTimer(dhtMessageTimeout)
defer t.Stop()

select {
Expand Down