Skip to content

Commit

Permalink
Merge pull request ipfs/go-bitswap#103 from bpot/bp-revert-buffered-w…
Browse files Browse the repository at this point in the history
…rite

Reduce memory allocation

This commit was moved from ipfs/go-bitswap@088fd5f
  • Loading branch information
Stebalien authored Mar 28, 2019
2 parents 207ae1d + a2360cd commit bc708fe
Showing 1 changed file with 3 additions and 10 deletions.
13 changes: 3 additions & 10 deletions bitswap/network/ipfs_impl.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package network

import (
"bufio"
"context"
"fmt"
"io"
Expand Down Expand Up @@ -74,32 +73,26 @@ 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(w); err != nil {
if err := msg.ToNetV1(s); err != nil {
log.Debugf("error: %s", err)
return err
}
case ProtocolBitswapOne, ProtocolBitswapNoVers:
if err := msg.ToNetV0(w); err != nil {
if err := msg.ToNetV0(s); 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 bc708fe

Please sign in to comment.