From 8f2205fe16e780f32eeaf2fdb120700616ec22a1 Mon Sep 17 00:00:00 2001 From: Steven Allen Date: Fri, 17 Nov 2017 13:21:14 -0800 Subject: [PATCH 1/2] switch to libp2p/go-msgio --- interface.go | 2 +- package.json | 4 ++-- protocol.go | 2 +- rw.go | 4 ++-- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/interface.go b/interface.go index d48bf61..cbe19ba 100644 --- a/interface.go +++ b/interface.go @@ -5,9 +5,9 @@ import ( "context" "io" - msgio "github.com/jbenet/go-msgio" ci "github.com/libp2p/go-libp2p-crypto" peer "github.com/libp2p/go-libp2p-peer" + msgio "github.com/libp2p/go-msgio" ) // SessionGenerator constructs secure communication sessions for a peer. diff --git a/package.json b/package.json index f1dd396..c45d410 100644 --- a/package.json +++ b/package.json @@ -9,9 +9,9 @@ "gxDependencies": [ { "author": "whyrusleeping", - "hash": "QmRQhVisS8dmPbjBUthVkenn81pBxrx1GxE281csJhm2vL", + "hash": "QmWBug6eBS7AxRdCDVuSY5CnSit7cS2XnPFYJWqWDumhCG", "name": "go-msgio", - "version": "0.0.0" + "version": "0.0.3" }, { "author": "whyrusleeping", diff --git a/protocol.go b/protocol.go index 0a2c0d8..73bf133 100644 --- a/protocol.go +++ b/protocol.go @@ -10,10 +10,10 @@ import ( "time" logging "github.com/ipfs/go-log" - msgio "github.com/jbenet/go-msgio" ci "github.com/libp2p/go-libp2p-crypto" peer "github.com/libp2p/go-libp2p-peer" pb "github.com/libp2p/go-libp2p-secio/pb" + msgio "github.com/libp2p/go-msgio" mh "github.com/multiformats/go-multihash" ) diff --git a/rw.go b/rw.go index ec7dd10..fb19874 100644 --- a/rw.go +++ b/rw.go @@ -11,8 +11,8 @@ import ( "sync" proto "github.com/gogo/protobuf/proto" - msgio "github.com/jbenet/go-msgio" - mpool "github.com/jbenet/go-msgio/mpool" + msgio "github.com/libp2p/go-msgio" + mpool "github.com/libp2p/go-msgio/mpool" ) // ErrMACInvalid signals that a MAC verification failed From 9f79cbcd96e1edad49e9fa051f3496e1550eacf2 Mon Sep 17 00:00:00 2001 From: Steven Allen Date: Fri, 17 Nov 2017 13:22:38 -0800 Subject: [PATCH 2/2] share the buffer pool. Before, we were copying it (dangerously, locks and stuff), into every writer. Now, we share it (like we should...). note: I'm not even sure if we should be using buffer pools (the allocator may be faster in this case) but that's another issue. --- rw.go | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/rw.go b/rw.go index fb19874..150b55f 100644 --- a/rw.go +++ b/rw.go @@ -18,23 +18,17 @@ import ( // ErrMACInvalid signals that a MAC verification failed var ErrMACInvalid = errors.New("MAC verification failed") -// bufPool is a ByteSlicePool for messages. we need buffers because (sadly) -// we cannot encrypt in place-- the user needs their buffer back. -var bufPool = mpool.ByteSlicePool - type etmWriter struct { - // params - pool mpool.Pool // for the buffers with encrypted data - str cipher.Stream // the stream cipher to encrypt with - mac HMAC // the mac to authenticate data with - w io.Writer + str cipher.Stream // the stream cipher to encrypt with + mac HMAC // the mac to authenticate data with + w io.Writer sync.Mutex } // NewETMWriter Encrypt-Then-MAC func NewETMWriter(w io.Writer, s cipher.Stream, mac HMAC) msgio.WriteCloser { - return &etmWriter{w: w, str: s, mac: mac, pool: bufPool} + return &etmWriter{w: w, str: s, mac: mac} } // Write writes passed in buffer as a single message. @@ -52,7 +46,7 @@ func (w *etmWriter) WriteMsg(b []byte) error { bufsize := uint32(4 + len(b) + w.mac.Size()) // encrypt. - buf := w.pool.Get(bufsize).([]byte) + buf := mpool.ByteSlicePool.Get(bufsize).([]byte) data := buf[4 : 4+len(b)] // the pool's buffer may be larger w.str.XORKeyStream(data, b) @@ -70,7 +64,7 @@ func (w *etmWriter) WriteMsg(b []byte) error { binary.BigEndian.PutUint32(buf[:4], uint32(len(data))) _, err := w.w.Write(buf[:bufsize]) - w.pool.Put(bufsize, buf) + mpool.ByteSlicePool.Put(bufsize, buf) return err }