Skip to content
This repository has been archived by the owner on Feb 1, 2023. It is now read-only.

Move connection management into networking layer #351

Merged
merged 15 commits into from
Apr 18, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 8 additions & 6 deletions network/ipfs_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,15 +90,16 @@ type impl struct {
}

type streamMessageSender struct {
to peer.ID
stream network.Stream
bsnet *impl
opts *MessageSenderOpts
to peer.ID
stream network.Stream
connected bool
bsnet *impl
opts *MessageSenderOpts
}

// Open a stream to the remote peer
func (s *streamMessageSender) Connect(ctx context.Context) (network.Stream, error) {
if s.stream != nil {
if s.connected {
return s.stream, nil
}

Expand All @@ -112,14 +113,15 @@ func (s *streamMessageSender) Connect(ctx context.Context) (network.Stream, erro
}

s.stream = stream
s.connected = true
return s.stream, nil
}

// Reset the stream
func (s *streamMessageSender) Reset() error {
if s.stream != nil {
err := s.stream.Reset()
s.stream = nil
s.connected = false
Copy link
Member

Choose a reason for hiding this comment

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

Why not just set the stream to nil? That will free up the resources as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I believe the crash was caused because we were calling SupportsHave() after a Reset():

func (s *streamMessageSender) SupportsHave() bool {
	return s.bsnet.SupportsHave(s.stream.Protocol())
}

Copy link
Member

Choose a reason for hiding this comment

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

Ah, I see. We shouldn't even construct a streamMessageSender till we have the stream.

return err
}
return nil
Expand Down