Skip to content

Commit 64f92dd

Browse files
author
mobus
committed
copy netpoll from hslam
1 parent 5f394b6 commit 64f92dd

File tree

14 files changed

+263
-18
lines changed

14 files changed

+263
-18
lines changed

netpoll/LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2020 Meng Huang ([email protected])
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

netpoll/README.md

Lines changed: 192 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
1+
# netpoll
2+
[![PkgGoDev](https://pkg.go.dev/badge/github.com/hslam/netpoll)](https://pkg.go.dev/github.com/hslam/netpoll)
3+
[![Build Status](https://github.com/hslam/netpoll/workflows/build/badge.svg)](https://github.com/hslam/netpoll/actions)
4+
[![codecov](https://codecov.io/gh/hslam/netpoll/branch/master/graph/badge.svg)](https://codecov.io/gh/hslam/netpoll)
5+
[![Go Report Card](https://goreportcard.com/badge/github.com/hslam/netpoll)](https://goreportcard.com/report/github.com/hslam/netpoll)
6+
[![LICENSE](https://img.shields.io/github/license/hslam/netpoll.svg?style=flat-square)](https://github.com/hslam/netpoll/blob/master/LICENSE)
7+
8+
Package netpoll implements a network poller based on epoll/kqueue.
9+
10+
## Features
11+
12+
* Epoll/kqueue
13+
* TCP/UNIX
14+
* Compatible with the net.Conn interface.
15+
* Upgrade connection
16+
* Non-blocking I/O
17+
* [Splice](https://github.com/hslam/splice "splice")/[sendfile](https://github.com/hslam/sendfile "sendfile")
18+
* Rescheduling workers
19+
20+
**Comparison to other packages.**
21+
22+
|Package| [net](https://github.com/golang/go/tree/master/src/net "net")| [netpoll](https://github.com/hslam/netpoll "netpoll")|[gnet](https://github.com/panjf2000/gnet "gnet")|[evio](https://github.com/tidwall/evio "evio")|
23+
|:--:|:--|:--|:--|:--|
24+
|Low memory usage|No|Yes|Yes|Yes|
25+
|Non-blocking I/O|No|Yes|Yes|Yes|
26+
|Splice/sendfile|Yes|Yes|No|No|
27+
|Rescheduling|Yes|Yes|No|No|
28+
|Compatible with the net.Conn interface|Yes|Yes|No|No|
29+
30+
## [Benchmark](http://github.com/hslam/netpoll-benchmark "netpoll-benchmark")
31+
32+
<img src="https://raw.githubusercontent.com/hslam/netpoll-benchmark/master/netpoll-qps.png" width = "400" height = "300" alt="mock 0ms" align=center><img src="https://raw.githubusercontent.com/hslam/netpoll-benchmark/master/netpoll-mock-time-qps.png" width = "400" height = "300" alt="mock 1ms" align=center>
33+
34+
## Get started
35+
36+
### Install
37+
```
38+
go get github.com/hslam/netpoll
39+
```
40+
### Import
41+
```
42+
import "github.com/hslam/netpoll"
43+
```
44+
### Usage
45+
#### Simple Example
46+
```go
47+
package main
48+
49+
import "github.com/hslam/netpoll"
50+
51+
func main() {
52+
var handler = &netpoll.DataHandler{
53+
NoShared: true,
54+
NoCopy: true,
55+
BufferSize: 1024,
56+
HandlerFunc: func(req []byte) (res []byte) {
57+
res = req
58+
return
59+
},
60+
}
61+
if err := netpoll.ListenAndServe("tcp", ":9999", handler); err != nil {
62+
panic(err)
63+
}
64+
}
65+
```
66+
67+
#### [TLS](http://github.com/hslam/socket "socket") Example
68+
```go
69+
package main
70+
71+
import (
72+
"crypto/tls"
73+
"github.com/hslam/netpoll"
74+
"github.com/hslam/socket"
75+
"net"
76+
)
77+
78+
func main() {
79+
var handler = &netpoll.DataHandler{
80+
NoShared: true,
81+
NoCopy: true,
82+
BufferSize: 1024,
83+
HandlerFunc: func(req []byte) (res []byte) {
84+
res = req
85+
return
86+
},
87+
}
88+
handler.SetUpgrade(func(conn net.Conn) (net.Conn, error) {
89+
tlsConn := tls.Server(conn, socket.DefalutTLSConfig())
90+
if err := tlsConn.Handshake(); err != nil {
91+
return nil, err
92+
}
93+
return tlsConn, nil
94+
})
95+
if err := netpoll.ListenAndServe("tcp", ":9999", handler); err != nil {
96+
panic(err)
97+
}
98+
}
99+
```
100+
101+
#### [Websocket](http://github.com/hslam/websocket "websocket") Example
102+
```go
103+
package main
104+
105+
import (
106+
"github.com/hslam/netpoll"
107+
"github.com/hslam/websocket"
108+
"net"
109+
)
110+
111+
func main() {
112+
var handler = &netpoll.ConnHandler{}
113+
handler.SetUpgrade(func(conn net.Conn) (netpoll.Context, error) {
114+
return websocket.Upgrade(conn, nil)
115+
})
116+
handler.SetServe(func(context netpoll.Context) error {
117+
ws := context.(*websocket.Conn)
118+
msg, err := ws.ReadMessage()
119+
if err != nil {
120+
return err
121+
}
122+
return ws.WriteMessage(msg)
123+
})
124+
if err := netpoll.ListenAndServe("tcp", ":9999", handler); err != nil {
125+
panic(err)
126+
}
127+
}
128+
```
129+
130+
131+
#### [HTTP](http://github.com/hslam/rum "rum") Example
132+
```go
133+
package main
134+
135+
import (
136+
"bufio"
137+
"github.com/hslam/mux"
138+
"github.com/hslam/netpoll"
139+
"github.com/hslam/response"
140+
"net"
141+
"net/http"
142+
"sync"
143+
)
144+
145+
func main() {
146+
m := mux.New()
147+
m.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
148+
w.Write([]byte("Hello World"))
149+
})
150+
ListenAndServe(":8080", m)
151+
}
152+
153+
func ListenAndServe(addr string, handler http.Handler) error {
154+
var h = &netpoll.ConnHandler{}
155+
type Context struct {
156+
reader *bufio.Reader
157+
rw *bufio.ReadWriter
158+
conn net.Conn
159+
serving sync.Mutex
160+
}
161+
h.SetUpgrade(func(conn net.Conn) (netpoll.Context, error) {
162+
reader := bufio.NewReader(conn)
163+
rw := bufio.NewReadWriter(reader, bufio.NewWriter(conn))
164+
return &Context{reader: reader, conn: conn, rw: rw}, nil
165+
})
166+
h.SetServe(func(context netpoll.Context) error {
167+
ctx := context.(*Context)
168+
ctx.serving.Lock()
169+
req, err := http.ReadRequest(ctx.reader)
170+
if err != nil {
171+
ctx.serving.Unlock()
172+
return err
173+
}
174+
res := response.NewResponse(req, ctx.conn, ctx.rw)
175+
handler.ServeHTTP(res, req)
176+
res.FinishRequest()
177+
ctx.serving.Unlock()
178+
response.FreeResponse(res)
179+
return nil
180+
})
181+
return netpoll.ListenAndServe("tcp", addr, h)
182+
}
183+
```
184+
185+
### License
186+
This package is licensed under a MIT license (Copyright (c) 2020 Meng Huang)
187+
188+
189+
### Author
190+
netpoll was written by Meng Huang.
191+
192+

netpoll/handler.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1+
// Copyright (c) 2020 Meng Huang ([email protected])
2+
// This package is licensed under a MIT license that can be found in the LICENSE file.
3+
14
package netpoll
25

36
import (
47
"errors"
8+
"github.com/hslam/buffer"
59
"net"
610
"sync"
7-
8-
"github.com/sunvim/utils/linear_ac"
911
)
1012

1113
// ErrHandlerFunc is the error when the HandlerFunc is nil
@@ -88,7 +90,7 @@ type context struct {
8890
writing sync.Mutex
8991
upgrade bool
9092
conn net.Conn
91-
pool *linear_ac.Allocator
93+
pool *buffer.Pool
9294
buffer []byte
9395
}
9496

@@ -119,7 +121,7 @@ func (h *DataHandler) Upgrade(conn net.Conn) (Context, error) {
119121
if h.NoShared {
120122
ctx.buffer = make([]byte, h.BufferSize)
121123
} else {
122-
ctx.pool = linear_ac.BindNew()
124+
ctx.pool = buffer.AssignPool(h.BufferSize)
123125
}
124126
return ctx, nil
125127
}
@@ -135,7 +137,7 @@ func (h *DataHandler) Serve(ctx Context) error {
135137
if h.NoShared {
136138
buf = c.buffer
137139
} else {
138-
c.pool.NewSlice(&buf, 0, h.BufferSize)
140+
buf = c.pool.GetBuffer(h.BufferSize)
139141
}
140142
if c.upgrade {
141143
c.reading.Lock()
@@ -146,7 +148,7 @@ func (h *DataHandler) Serve(ctx Context) error {
146148
}
147149
if err != nil {
148150
if !h.NoShared {
149-
c.pool.Release()
151+
c.pool.PutBuffer(buf)
150152
}
151153
return err
152154
}
@@ -164,7 +166,7 @@ func (h *DataHandler) Serve(ctx Context) error {
164166
c.writing.Unlock()
165167
}
166168
if !h.NoShared {
167-
c.pool.Release()
169+
c.pool.PutBuffer(buf)
168170
}
169171
return err
170172
}

netpoll/handler_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// Copyright (c) 2020 Meng Huang ([email protected])
2+
// This package is licensed under a MIT license that can be found in the LICENSE file.
3+
14
package netpoll
25

36
import (

netpoll/net.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// Copyright (c) 2020 Meng Huang ([email protected])
2+
// This package is licensed under a MIT license that can be found in the LICENSE file.
3+
14
package netpoll
25

36
import (

netpoll/net_other.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// Copyright (c) 2020 Meng Huang ([email protected])
2+
// This package is licensed under a MIT license that can be found in the LICENSE file.
3+
14
//go:build !linux && !darwin && !dragonfly && !freebsd && !netbsd && !openbsd
25
// +build !linux,!darwin,!dragonfly,!freebsd,!netbsd,!openbsd
36

netpoll/net_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// Copyright (c) 2020 Meng Huang ([email protected])
2+
// This package is licensed under a MIT license that can be found in the LICENSE file.
3+
14
package netpoll
25

36
import (

netpoll/net_unix.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
1+
// Copyright (c) 2020 Meng Huang ([email protected])
2+
// This package is licensed under a MIT license that can be found in the LICENSE file.
3+
14
//go:build linux || darwin || dragonfly || freebsd || netbsd || openbsd
25
// +build linux darwin dragonfly freebsd netbsd openbsd
36

47
package netpoll
58

69
import (
710
"errors"
11+
"github.com/hslam/buffer"
12+
"github.com/hslam/scheduler"
13+
"github.com/hslam/sendfile"
14+
"github.com/hslam/splice"
815
"io"
916
"net"
1017
"os"
@@ -13,11 +20,6 @@ import (
1320
"sync/atomic"
1421
"syscall"
1522
"time"
16-
17-
"github.com/hslam/scheduler"
18-
"github.com/hslam/sendfile"
19-
"github.com/hslam/splice"
20-
"github.com/sunvim/utils/linear_ac"
2123
)
2224

2325
const (
@@ -701,11 +703,9 @@ func genericReadFrom(w io.Writer, r io.Reader, remain int64) (n int64, err error
701703
} else if remain > bufferSize {
702704
remain = bufferSize
703705
}
704-
var buf []byte
705-
pool := linear_ac.NewLinearAc()
706-
pool.NewSlice(&buf, 0, int(remain))
707-
defer pool.Release()
708-
706+
pool := buffer.AssignPool(int(remain))
707+
buf := pool.GetBuffer(int(remain))
708+
defer pool.PutBuffer(buf)
709709
var nr int
710710
nr, err = r.Read(buf)
711711
if err != nil {

netpoll/net_unix_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// Copyright (c) 2020 Meng Huang ([email protected])
2+
// This package is licensed under a MIT license that can be found in the LICENSE file.
3+
14
package netpoll
25

36
import (

netpoll/poll.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// Copyright (c) 2020 Meng Huang ([email protected])
2+
// This package is licensed under a MIT license that can be found in the LICENSE file.
3+
14
// Package netpoll implements a network poller based on epoll/kqueue.
25
package netpoll
36

0 commit comments

Comments
 (0)