Skip to content

Commit

Permalink
extract byte buffer as interface
Browse files Browse the repository at this point in the history
  • Loading branch information
mobus committed Jan 19, 2022
1 parent 543503a commit 3c79e6d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
5 changes: 2 additions & 3 deletions examples/netepoll/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"syscall"
"time"

"github.com/oxtoacart/bpool"
"github.com/rcrowley/go-metrics"
"github.com/sunvim/utils/netpoll"
)
Expand All @@ -23,9 +24,7 @@ func main() {
}()

var handler = &netpoll.DataHandler{
NoShared: true,
NoCopy: true,
BufferSize: 1024,
Pool: bpool.NewBytePool(1024, 12*1024),
HandlerFunc: func(req []byte) (res []byte) {
res = req
return
Expand Down
13 changes: 9 additions & 4 deletions netpoll/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import (
"errors"
"net"
"sync"

"github.com/oxtoacart/bpool"
)

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

type BytePool interface {
Get() []byte
Put(b []byte)
NumPooled() int
Width() int
}

// DataHandler implements the Handler interface.
type DataHandler struct {
Pool *bpool.BytePool
Pool BytePool
upgrade func(net.Conn) (net.Conn, error)
// HandlerFunc is the data Serve function.
HandlerFunc func(req []byte) (res []byte)
Expand All @@ -83,7 +88,7 @@ type context struct {
writing sync.Mutex
upgrade bool
conn net.Conn
pool *bpool.BytePool
pool BytePool
buffer []byte
}

Expand Down

0 comments on commit 3c79e6d

Please sign in to comment.