|
| 1 | +# netpoll |
| 2 | +[](https://pkg.go.dev/github.com/hslam/netpoll) |
| 3 | +[](https://github.com/hslam/netpoll/actions) |
| 4 | +[](https://codecov.io/gh/hslam/netpoll) |
| 5 | +[](https://goreportcard.com/report/github.com/hslam/netpoll) |
| 6 | +[](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 | + |
0 commit comments