Skip to content

Commit

Permalink
Merge pull request ipfs/go-bitswap#15 from ipfs/fix/buffer-write
Browse files Browse the repository at this point in the history
buffer writes

This commit was moved from ipfs/go-bitswap@5251947
  • Loading branch information
Stebalien authored Oct 18, 2018
2 parents 3a6d9c9 + 3ac3a96 commit 478fa88
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions bitswap/network/ipfs_impl.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package network

import (
"bufio"
"context"
"fmt"
"io"
Expand Down Expand Up @@ -70,26 +71,32 @@ func msgToStream(ctx context.Context, s inet.Stream, msg bsmsg.BitSwapMessage) e
if dl, ok := ctx.Deadline(); ok {
deadline = dl
}

if err := s.SetWriteDeadline(deadline); err != nil {
log.Warningf("error setting deadline: %s", err)
}

w := bufio.NewWriter(s)

switch s.Protocol() {
case ProtocolBitswap:
if err := msg.ToNetV1(s); err != nil {
if err := msg.ToNetV1(w); err != nil {
log.Debugf("error: %s", err)
return err
}
case ProtocolBitswapOne, ProtocolBitswapNoVers:
if err := msg.ToNetV0(s); err != nil {
if err := msg.ToNetV0(w); err != nil {
log.Debugf("error: %s", err)
return err
}
default:
return fmt.Errorf("unrecognized protocol on remote: %s", s.Protocol())
}

if err := w.Flush(); err != nil {
log.Debugf("error: %s", err)
return err
}

if err := s.SetWriteDeadline(time.Time{}); err != nil {
log.Warningf("error resetting deadline: %s", err)
}
Expand Down

0 comments on commit 478fa88

Please sign in to comment.