Skip to content

Commit

Permalink
Merge pull request ipfs#164 from libp2p/fix/be-polite
Browse files Browse the repository at this point in the history
be more polite about closing streams
  • Loading branch information
Stebalien authored Jun 15, 2018
2 parents 2941d6e + 1d13eaf commit cb81b8d
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions dht_net.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package dht
import (
"context"
"fmt"
"io"
"sync"
"time"

Expand All @@ -22,8 +23,6 @@ func (dht *IpfsDHT) handleNewStream(s inet.Stream) {
}

func (dht *IpfsDHT) handleNewMessage(s inet.Stream) {
defer inet.FullClose(s)

ctx := dht.Context()
cr := ctxio.NewReader(ctx, s) // ok to use. we defer close stream in this func
cw := ctxio.NewWriter(ctx, s) // ok to use. we defer close stream in this func
Expand All @@ -34,7 +33,12 @@ func (dht *IpfsDHT) handleNewMessage(s inet.Stream) {
for {
// receive msg
pmes := new(pb.Message)
if err := r.ReadMsg(pmes); err != nil {
switch err := r.ReadMsg(pmes); err {
case io.EOF:
s.Close()
return
case nil:
default:
s.Reset()
log.Debugf("Error unmarshaling data: %s", err)
return
Expand Down

0 comments on commit cb81b8d

Please sign in to comment.