Skip to content

Commit 3c79e6d

Browse files
author
mobus
committed
extract byte buffer as interface
1 parent 543503a commit 3c79e6d

File tree

2 files changed

+11
-7
lines changed

2 files changed

+11
-7
lines changed

examples/netepoll/main.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"syscall"
99
"time"
1010

11+
"github.com/oxtoacart/bpool"
1112
"github.com/rcrowley/go-metrics"
1213
"github.com/sunvim/utils/netpoll"
1314
)
@@ -23,9 +24,7 @@ func main() {
2324
}()
2425

2526
var handler = &netpoll.DataHandler{
26-
NoShared: true,
27-
NoCopy: true,
28-
BufferSize: 1024,
27+
Pool: bpool.NewBytePool(1024, 12*1024),
2928
HandlerFunc: func(req []byte) (res []byte) {
3029
res = req
3130
return

netpoll/handler.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ import (
77
"errors"
88
"net"
99
"sync"
10-
11-
"github.com/oxtoacart/bpool"
1210
)
1311

1412
// ErrHandlerFunc is the error when the HandlerFunc is nil
@@ -70,9 +68,16 @@ func (h *ConnHandler) Serve(ctx Context) error {
7068
return h.serve(ctx)
7169
}
7270

71+
type BytePool interface {
72+
Get() []byte
73+
Put(b []byte)
74+
NumPooled() int
75+
Width() int
76+
}
77+
7378
// DataHandler implements the Handler interface.
7479
type DataHandler struct {
75-
Pool *bpool.BytePool
80+
Pool BytePool
7681
upgrade func(net.Conn) (net.Conn, error)
7782
// HandlerFunc is the data Serve function.
7883
HandlerFunc func(req []byte) (res []byte)
@@ -83,7 +88,7 @@ type context struct {
8388
writing sync.Mutex
8489
upgrade bool
8590
conn net.Conn
86-
pool *bpool.BytePool
91+
pool BytePool
8792
buffer []byte
8893
}
8994

0 commit comments

Comments
 (0)