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

bitswap: add a deadline to sendmsg calls #3445

Merged
merged 1 commit into from
Dec 5, 2016
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
2 changes: 1 addition & 1 deletion exchange/bitswap/network/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ type BitSwapNetwork interface {
}

type MessageSender interface {
SendMsg(bsmsg.BitSwapMessage) error
SendMsg(context.Context, bsmsg.BitSwapMessage) error
Close() error
}

Expand Down
24 changes: 20 additions & 4 deletions exchange/bitswap/network/ipfs_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"io"
"time"

bsmsg "github.com/ipfs/go-ipfs/exchange/bitswap/message"

Expand All @@ -20,6 +21,8 @@ import (

var log = logging.Logger("bitswap_network")

var sendMessageTimeout = time.Minute * 10

// NewFromIpfsHost returns a BitSwapNetwork supported by underlying IPFS host
func NewFromIpfsHost(host host.Host, r routing.ContentRouting) BitSwapNetwork {
bitswapNetwork := impl{
Expand Down Expand Up @@ -53,11 +56,20 @@ func (s *streamMessageSender) Close() error {
return s.s.Close()
}

func (s *streamMessageSender) SendMsg(msg bsmsg.BitSwapMessage) error {
return msgToStream(s.s, msg)
func (s *streamMessageSender) SendMsg(ctx context.Context, msg bsmsg.BitSwapMessage) error {
return msgToStream(ctx, s.s, msg)
}

func msgToStream(s inet.Stream, msg bsmsg.BitSwapMessage) error {
func msgToStream(ctx context.Context, s inet.Stream, msg bsmsg.BitSwapMessage) error {
deadline := time.Now().Add(sendMessageTimeout)
if dl, ok := ctx.Deadline(); ok {
deadline = dl
}

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

switch s.Protocol() {
case ProtocolBitswap:
if err := msg.ToNetV1(s); err != nil {
Expand All @@ -72,6 +84,10 @@ func msgToStream(s inet.Stream, msg bsmsg.BitSwapMessage) error {
default:
return fmt.Errorf("unrecognized protocol on remote: %s", s.Protocol())
}

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

Expand Down Expand Up @@ -107,7 +123,7 @@ func (bsnet *impl) SendMessage(
}
defer s.Close()

return msgToStream(s, outgoing)
return msgToStream(ctx, s, outgoing)
}

func (bsnet *impl) SetDelegate(r Receiver) {
Expand Down
4 changes: 2 additions & 2 deletions exchange/bitswap/testnet/virtual.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,8 @@ type messagePasser struct {
ctx context.Context
}

func (mp *messagePasser) SendMsg(m bsmsg.BitSwapMessage) error {
return mp.net.SendMessage(mp.ctx, mp.local, mp.target, m)
func (mp *messagePasser) SendMsg(ctx context.Context, m bsmsg.BitSwapMessage) error {
return mp.net.SendMessage(ctx, mp.local, mp.target, m)
}

func (mp *messagePasser) Close() error {
Expand Down
2 changes: 1 addition & 1 deletion exchange/bitswap/wantmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ func (mq *msgQueue) doWork(ctx context.Context) {

// send wantlist updates
for { // try to send this message until we fail.
err := mq.sender.SendMsg(wlm)
err := mq.sender.SendMsg(ctx, wlm)
if err == nil {
return
}
Expand Down