Add pools for packetack

This commit is contained in:
9seconds
2020-03-23 17:08:13 +03:00
parent 6449109b2b
commit 9125a29e79
4 changed files with 46 additions and 5 deletions
+3 -1
View File
@@ -88,7 +88,9 @@ func (w *wrapperClientAbridged) Write(packet conntypes.Packet, acks *conntypes.C
return nil
case packetLength < clientAbridgedLargePacketLength:
length24 := utils.ToUint24(uint32(packetLength))
buf := bytes.Buffer{}
buf := acquireClientBytesBuffer()
defer releaseClientBytesBuffer(buf)
buf.WriteByte(byte(clientAbridgedSmallPacketLength))
buf.Write(length24[:])
@@ -1,7 +1,6 @@
package packetack
import (
"bytes"
"encoding/binary"
"fmt"
"math/rand"
@@ -35,11 +34,13 @@ func (w *wrapperClientIntermediateSecure) Write(packet conntypes.Packet, acks *c
return nil
}
buf := bytes.Buffer{}
buf := acquireClientBytesBuffer()
defer releaseClientBytesBuffer(buf)
paddingLength := rand.Intn(4)
buf.Grow(4 + len(packet) + paddingLength)
binary.Write(&buf, binary.LittleEndian, uint32(len(packet)+paddingLength)) // nolint: errcheck
binary.Write(buf, binary.LittleEndian, uint32(len(packet)+paddingLength)) // nolint: errcheck
buf.Write(packet)
buf.Write(make([]byte, paddingLength))
+37
View File
@@ -0,0 +1,37 @@
package packetack
import (
"bytes"
"sync"
)
var (
poolClientBytesBuffer = sync.Pool{
New: func() interface{} {
return &bytes.Buffer{}
},
}
poolProxyBytesBuffer = sync.Pool{
New: func() interface{} {
return &bytes.Buffer{}
},
}
)
func acquireClientBytesBuffer() *bytes.Buffer {
return poolClientBytesBuffer.Get().(*bytes.Buffer)
}
func acquireProxyBytesBuffer() *bytes.Buffer {
return poolProxyBytesBuffer.Get().(*bytes.Buffer)
}
func releaseClientBytesBuffer(buf *bytes.Buffer) {
buf.Reset()
poolClientBytesBuffer.Put(buf)
}
func releaseProxyBytesBuffer(buf *bytes.Buffer) {
buf.Reset()
poolProxyBytesBuffer.Put(buf)
}
+2 -1
View File
@@ -22,7 +22,8 @@ type wrapperProxy struct {
}
func (w *wrapperProxy) Write(packet conntypes.Packet, acks *conntypes.ConnectionAcks) error {
buf := bytes.Buffer{}
buf := acquireProxyBytesBuffer()
defer releaseProxyBytesBuffer(buf)
flags := w.flags
if acks.Quick {