diff --git a/frontend/udp/bytepool/bytepool.go b/frontend/udp/bytepool/bytepool.go index 148247b..b42640f 100644 --- a/frontend/udp/bytepool/bytepool.go +++ b/frontend/udp/bytepool/bytepool.go @@ -11,24 +11,27 @@ type BytePool struct { func New(length int) *BytePool { var bp BytePool bp.Pool.New = func() interface{} { - return make([]byte, length) + b := make([]byte, length) + return &b } return &bp } // Get returns a byte slice from the pool. -func (bp *BytePool) Get() []byte { - return bp.Pool.Get().([]byte) +func (bp *BytePool) Get() *[]byte { + return bp.Pool.Get().(*[]byte) } // Put returns a byte slice to the pool. -func (bp *BytePool) Put(b []byte) { - b = b[:cap(b)] +func (bp *BytePool) Put(b *[]byte) { + *b = (*b)[:cap(*b)] + // Zero out the bytes. - // Apparently this specific expression is optimized by the compiler, see - // github.com/golang/go/issues/5373. - for i := range b { - b[i] = 0 + // This specific expression is optimized by the compiler: + // https://github.com/golang/go/issues/5373. + for i := range *b { + (*b)[i] = 0 } + bp.Pool.Put(b) } diff --git a/frontend/udp/frontend.go b/frontend/udp/frontend.go index 695e8ec..40a1a2d 100644 --- a/frontend/udp/frontend.go +++ b/frontend/udp/frontend.go @@ -185,7 +185,7 @@ func (t *Frontend) serve() error { // Read a UDP packet into a reusable buffer. buffer := pool.Get() - n, addr, err := t.socket.ReadFromUDP(buffer) + n, addr, err := t.socket.ReadFromUDP(*buffer) if err != nil { pool.Put(buffer) if netErr, ok := err.(net.Error); ok && netErr.Temporary() { @@ -217,7 +217,7 @@ func (t *Frontend) serve() error { } action, af, err := t.handleRequest( // Make sure the IP is copied, not referenced. - Request{buffer[:n], append([]byte{}, addr.IP...)}, + Request{(*buffer)[:n], append([]byte{}, addr.IP...)}, ResponseWriter{t.socket, addr}, ) if t.EnableRequestTiming {