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

Commit

Permalink
Merge pull request #49 from libp2p/fix/mem
Browse files Browse the repository at this point in the history
avoid holding the message writer longer than necessary
  • Loading branch information
Stebalien authored Jun 15, 2019
2 parents a158134 + 60cfa73 commit 2078160
Showing 1 changed file with 6 additions and 7 deletions.
13 changes: 6 additions & 7 deletions protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@ const nonceSize = 16
type secureSession struct {
msgio.ReadWriteCloser

insecure net.Conn
insecureM msgio.ReadWriter
insecure net.Conn

localKey ci.PrivKey
localPeer peer.ID
Expand Down Expand Up @@ -89,7 +88,6 @@ func newSecureSession(ctx context.Context, local peer.ID, key ci.PrivKey, insecu
}

s.insecure = insecure
s.insecureM = msgio.NewReadWriter(insecure)
s.remotePeer = remotePeer

handshakeCtx, cancel := context.WithTimeout(ctx, HandshakeTimeout) // remove
Expand Down Expand Up @@ -136,6 +134,7 @@ func (s *secureSession) runHandshake(ctx context.Context) error {
}

func (s *secureSession) runHandshakeSync() error {
insecureM := msgio.NewReadWriter(s.insecure)
// =============================================================================
// step 1. Propose -- propose cipher suite + send pubkeys + nonce

Expand Down Expand Up @@ -170,11 +169,11 @@ func (s *secureSession) runHandshakeSync() error {
}

// Send Propose packet and Receive their Propose packet
proposeInBytes, err := readWriteMsg(s.insecureM, proposeOutBytes)
proposeInBytes, err := readWriteMsg(insecureM, proposeOutBytes)
if err != nil {
return err
}
defer s.insecureM.ReleaseMsg(proposeInBytes)
defer insecureM.ReleaseMsg(proposeInBytes)

// Parse their propose packet
proposeIn := new(pb.Propose)
Expand Down Expand Up @@ -281,11 +280,11 @@ func (s *secureSession) runHandshakeSync() error {
}

// Send Exchange packet and receive their Exchange packet
exchangeInBytes, err := readWriteMsg(s.insecureM, exchangeOutBytes)
exchangeInBytes, err := readWriteMsg(insecureM, exchangeOutBytes)
if err != nil {
return err
}
defer s.insecureM.ReleaseMsg(exchangeInBytes)
defer insecureM.ReleaseMsg(exchangeInBytes)

// Parse their Exchange packet.
exchangeIn := new(pb.Exchange)
Expand Down

0 comments on commit 2078160

Please sign in to comment.