Skip to content

Commit 543503a

Browse files
author
mobus
committed
optimize byte buffer
1 parent f8803fe commit 543503a

File tree

6 files changed

+38
-93
lines changed

6 files changed

+38
-93
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ require (
88
github.com/hslam/scheduler v0.0.0-20211028175315-641598104976
99
github.com/hslam/sendfile v1.0.1
1010
github.com/hslam/splice v1.0.3
11+
github.com/oxtoacart/bpool v0.0.0-20190530202638-03653db5a59c
1112
github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475
1213
github.com/stretchr/testify v1.7.0
1314
gopkg.in/eapache/queue.v1 v1.1.0

go.sum

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,14 @@ github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
1919
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
2020
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e h1:fD57ERR4JtEqsWbfPhv4DMiApHyliiK5xCTNVSPiaAs=
2121
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
22+
github.com/oxtoacart/bpool v0.0.0-20190530202638-03653db5a59c h1:rp5dCmg/yLR3mgFuSOe4oEnDDmGLROTvMragMUXpTQw=
23+
github.com/oxtoacart/bpool v0.0.0-20190530202638-03653db5a59c/go.mod h1:X07ZCGwUbLaax7L0S3Tw4hpejzu63ZrrQiUe6W0hcy0=
2224
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
2325
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
2426
github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475 h1:N/ElC8H3+5XpJzTSTfLsJV/mx9Q9g7kxmchpfZyxgzM=
2527
github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475/go.mod h1:bCqnVzQkZxMG4s8nGwiZ5l3QUCyqpo9Y+/ZMZ9VjZe4=
2628
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
29+
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
2730
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
2831
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
2932
golang.org/x/sys v0.0.0-20200302150141-5c8b2ff67527/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=

netpoll/handler.go

Lines changed: 9 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@ package netpoll
55

66
import (
77
"errors"
8-
"github.com/hslam/buffer"
98
"net"
109
"sync"
10+
11+
"github.com/oxtoacart/bpool"
1112
)
1213

1314
// ErrHandlerFunc is the error when the HandlerFunc is nil
@@ -71,16 +72,8 @@ func (h *ConnHandler) Serve(ctx Context) error {
7172

7273
// DataHandler implements the Handler interface.
7374
type DataHandler struct {
74-
// NoShared disables the DataHandler to use the buffer pool for high performance.
75-
// Default NoShared is false to use the buffer pool for low memory usage.
76-
NoShared bool
77-
// NoCopy returns the bytes underlying buffer when NoCopy is true,
78-
// The bytes returned is shared by all invocations of Read, so do not modify it.
79-
// Default NoCopy is false to make a copy of data for every invocations of Read.
80-
NoCopy bool
81-
// BufferSize represents the buffer size.
82-
BufferSize int
83-
upgrade func(net.Conn) (net.Conn, error)
75+
Pool *bpool.BytePool
76+
upgrade func(net.Conn) (net.Conn, error)
8477
// HandlerFunc is the data Serve function.
8578
HandlerFunc func(req []byte) (res []byte)
8679
}
@@ -90,7 +83,7 @@ type context struct {
9083
writing sync.Mutex
9184
upgrade bool
9285
conn net.Conn
93-
pool *buffer.Pool
86+
pool *bpool.BytePool
9487
buffer []byte
9588
}
9689

@@ -101,9 +94,6 @@ func (h *DataHandler) SetUpgrade(upgrade func(net.Conn) (net.Conn, error)) {
10194

10295
// Upgrade sets the net.Conn to a Context.
10396
func (h *DataHandler) Upgrade(conn net.Conn) (Context, error) {
104-
if h.BufferSize < 1 {
105-
h.BufferSize = bufferSize
106-
}
10797
if h.HandlerFunc == nil {
10898
return nil, ErrHandlerFunc
10999
}
@@ -117,12 +107,7 @@ func (h *DataHandler) Upgrade(conn net.Conn) (Context, error) {
117107
conn = c
118108
}
119109
}
120-
var ctx = &context{upgrade: upgrade, conn: conn}
121-
if h.NoShared {
122-
ctx.buffer = make([]byte, h.BufferSize)
123-
} else {
124-
ctx.pool = buffer.AssignPool(h.BufferSize)
125-
}
110+
var ctx = &context{upgrade: upgrade, conn: conn, pool: h.Pool}
126111
return ctx, nil
127112
}
128113

@@ -134,11 +119,8 @@ func (h *DataHandler) Serve(ctx Context) error {
134119
var err error
135120
var buf []byte
136121
var req []byte
137-
if h.NoShared {
138-
buf = c.buffer
139-
} else {
140-
buf = c.pool.GetBuffer(h.BufferSize)
141-
}
122+
buf = c.pool.Get()
123+
defer c.pool.Put(buf)
142124
if c.upgrade {
143125
c.reading.Lock()
144126
}
@@ -147,16 +129,9 @@ func (h *DataHandler) Serve(ctx Context) error {
147129
c.reading.Unlock()
148130
}
149131
if err != nil {
150-
if !h.NoShared {
151-
c.pool.PutBuffer(buf)
152-
}
153132
return err
154133
}
155134
req = buf[:n]
156-
if !h.NoCopy {
157-
req = make([]byte, n)
158-
copy(req, buf[:n])
159-
}
160135
res := h.HandlerFunc(req)
161136
if c.upgrade {
162137
c.writing.Lock()
@@ -165,8 +140,6 @@ func (h *DataHandler) Serve(ctx Context) error {
165140
if c.upgrade {
166141
c.writing.Unlock()
167142
}
168-
if !h.NoShared {
169-
c.pool.PutBuffer(buf)
170-
}
143+
c.pool.Put(res)
171144
return err
172145
}

netpoll/handler_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import (
88
"net"
99
"testing"
1010
"time"
11+
12+
"github.com/oxtoacart/bpool"
1113
)
1214

1315
func TestNewHandler(t *testing.T) {
@@ -68,9 +70,7 @@ func TestConnHandler(t *testing.T) {
6870

6971
func TestDataHandler(t *testing.T) {
7072
var handler = &DataHandler{
71-
NoShared: true,
72-
NoCopy: false,
73-
BufferSize: 0,
73+
Pool: bpool.NewBytePool(1024, 12*1024),
7474
}
7575
_, err := handler.Upgrade(&conn{})
7676
if err != ErrHandlerFunc {

netpoll/net_test.go

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ import (
99
"sync"
1010
"testing"
1111
"time"
12+
13+
"github.com/oxtoacart/bpool"
1214
)
1315

1416
func TestListenAndServe(t *testing.T) {
1517
var handler = &DataHandler{
16-
NoShared: false,
17-
NoCopy: false,
18-
BufferSize: 1024,
18+
Pool: bpool.NewBytePool(1024, 12*1024),
1919
HandlerFunc: func(req []byte) (res []byte) {
2020
res = req
2121
return
@@ -33,9 +33,7 @@ func TestListenAndServe(t *testing.T) {
3333

3434
func TestServe(t *testing.T) {
3535
var handler = &DataHandler{
36-
NoShared: false,
37-
NoCopy: false,
38-
BufferSize: 1024,
36+
Pool: bpool.NewBytePool(1024, 12*1024),
3937
HandlerFunc: func(req []byte) (res []byte) {
4038
res = req
4139
return
@@ -54,9 +52,7 @@ func TestServe(t *testing.T) {
5452

5553
func TestNetServer(t *testing.T) {
5654
var handler = &DataHandler{
57-
NoShared: true,
58-
NoCopy: true,
59-
BufferSize: 1024,
55+
Pool: bpool.NewBytePool(1024, 12*1024),
6056
HandlerFunc: func(req []byte) (res []byte) {
6157
res = req
6258
return

netpoll/net_unix_test.go

Lines changed: 17 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import (
1111
"sync"
1212
"testing"
1313
"time"
14+
15+
"github.com/oxtoacart/bpool"
1416
)
1517

1618
func TestServerListenAndServe(t *testing.T) {
@@ -55,9 +57,7 @@ func TestServerServe(t *testing.T) {
5557

5658
func TestServerPoll(t *testing.T) {
5759
var handler = &DataHandler{
58-
NoShared: false,
59-
NoCopy: false,
60-
BufferSize: 1024,
60+
Pool: bpool.NewBytePool(1024, 12*1024),
6161
HandlerFunc: func(req []byte) (res []byte) {
6262
res = req
6363
return
@@ -88,9 +88,7 @@ func TestServerPoll(t *testing.T) {
8888

8989
func TestServerClose(t *testing.T) {
9090
var handler = &DataHandler{
91-
NoShared: false,
92-
NoCopy: false,
93-
BufferSize: 1024,
91+
Pool: bpool.NewBytePool(1024, 12*1024),
9492
HandlerFunc: func(req []byte) (res []byte) {
9593
res = req
9694
return
@@ -117,9 +115,7 @@ func TestServerClose(t *testing.T) {
117115

118116
func TestServerNumCPU(t *testing.T) {
119117
var handler = &DataHandler{
120-
NoShared: false,
121-
NoCopy: false,
122-
BufferSize: 1024,
118+
Pool: bpool.NewBytePool(1024, 12*1024),
123119
HandlerFunc: func(req []byte) (res []byte) {
124120
res = req
125121
return
@@ -148,9 +144,7 @@ func TestServerNumCPU(t *testing.T) {
148144

149145
func TestServerTCPListener(t *testing.T) {
150146
var handler = &DataHandler{
151-
NoShared: false,
152-
NoCopy: false,
153-
BufferSize: 1024,
147+
Pool: bpool.NewBytePool(1024, 12*1024),
154148
HandlerFunc: func(req []byte) (res []byte) {
155149
res = req
156150
return
@@ -171,9 +165,7 @@ func TestServerTCPListener(t *testing.T) {
171165

172166
func TestServerUNIXListener(t *testing.T) {
173167
var handler = &DataHandler{
174-
NoShared: false,
175-
NoCopy: false,
176-
BufferSize: 1024,
168+
Pool: bpool.NewBytePool(1024, 12*1024),
177169
HandlerFunc: func(req []byte) (res []byte) {
178170
res = req
179171
return
@@ -194,9 +186,7 @@ func TestServerUNIXListener(t *testing.T) {
194186

195187
func TestTCPServer(t *testing.T) {
196188
var handler = &DataHandler{
197-
NoShared: false,
198-
NoCopy: false,
199-
BufferSize: 1024,
189+
Pool: bpool.NewBytePool(1024, 12*1024),
200190
HandlerFunc: func(req []byte) (res []byte) {
201191
res = req
202192
return
@@ -245,9 +235,7 @@ func TestTCPServer(t *testing.T) {
245235

246236
func TestUNIXServer(t *testing.T) {
247237
var handler = &DataHandler{
248-
NoShared: false,
249-
NoCopy: false,
250-
BufferSize: 1024,
238+
Pool: bpool.NewBytePool(1024, 12*1024),
251239
HandlerFunc: func(req []byte) (res []byte) {
252240
res = req
253241
return
@@ -296,9 +284,7 @@ func TestOtherServer(t *testing.T) {
296284
net.Listener
297285
}
298286
var handler = &DataHandler{
299-
NoShared: false,
300-
NoCopy: false,
301-
BufferSize: 1024,
287+
Pool: bpool.NewBytePool(1024, 12*1024),
302288
HandlerFunc: func(req []byte) (res []byte) {
303289
res = req
304290
return
@@ -342,9 +328,7 @@ func TestOtherServer(t *testing.T) {
342328

343329
func TestShared(t *testing.T) {
344330
var handler = &DataHandler{
345-
NoShared: false,
346-
NoCopy: false,
347-
BufferSize: 1024,
331+
Pool: bpool.NewBytePool(1024, 12*1024),
348332
HandlerFunc: func(req []byte) (res []byte) {
349333
res = req
350334
return
@@ -388,9 +372,7 @@ func TestShared(t *testing.T) {
388372

389373
func TestNoCopy(t *testing.T) {
390374
var handler = &DataHandler{
391-
NoShared: true,
392-
NoCopy: true,
393-
BufferSize: 1024,
375+
Pool: bpool.NewBytePool(1024, 12*1024),
394376
HandlerFunc: func(req []byte) (res []byte) {
395377
res = req
396378
return
@@ -483,9 +465,7 @@ func TestWorker(t *testing.T) {
483465

484466
func TestNoAsync(t *testing.T) {
485467
var handler = &DataHandler{
486-
NoShared: true,
487-
NoCopy: true,
488-
BufferSize: 1024,
468+
Pool: bpool.NewBytePool(1024, 12*1024),
489469
HandlerFunc: func(req []byte) (res []byte) {
490470
res = req
491471
return
@@ -530,9 +510,7 @@ func TestNoAsync(t *testing.T) {
530510

531511
func TestSharedWorkers(t *testing.T) {
532512
var handler = &DataHandler{
533-
NoShared: true,
534-
NoCopy: true,
535-
BufferSize: 1024,
513+
Pool: bpool.NewBytePool(1024, 12*1024),
536514
HandlerFunc: func(req []byte) (res []byte) {
537515
res = req
538516
return
@@ -579,9 +557,7 @@ func TestSharedWorkers(t *testing.T) {
579557

580558
func TestSharedWorkersPanic(t *testing.T) {
581559
var handler = &DataHandler{
582-
NoShared: true,
583-
NoCopy: true,
584-
BufferSize: 1024,
560+
Pool: bpool.NewBytePool(1024, 12*1024),
585561
HandlerFunc: func(req []byte) (res []byte) {
586562
res = req
587563
return
@@ -606,9 +582,7 @@ func TestSharedWorkersPanic(t *testing.T) {
606582

607583
func TestReschedule(t *testing.T) {
608584
var handler = &DataHandler{
609-
NoShared: false,
610-
NoCopy: false,
611-
BufferSize: 1024,
585+
Pool: bpool.NewBytePool(1024, 12*1024),
612586
HandlerFunc: func(req []byte) (res []byte) {
613587
res = req
614588
return
@@ -669,9 +643,7 @@ func TestReschedule(t *testing.T) {
669643

670644
func TestRescheduleDone(t *testing.T) {
671645
var handler = &DataHandler{
672-
NoShared: false,
673-
NoCopy: false,
674-
BufferSize: 1024,
646+
Pool: bpool.NewBytePool(1024, 12*1024),
675647
HandlerFunc: func(req []byte) (res []byte) {
676648
res = req
677649
return

0 commit comments

Comments
 (0)