# netpollmux
[![PkgGoDev](https://pkg.go.dev/badge/github.com/php2go/netpollmux)](https://pkg.go.dev/github.com/php2go/netpollmux)
[![Go Report Card](https://goreportcard.com/badge/github.com/php2go/netpollmux)](https://goreportcard.com/report/github.com/php2go/netpollmux)
[![LICENSE](https://img.shields.io/github/license/php2go/netpollmux.svg?style=flat-square)](https://github.com/php2go/netpollmux/blob/master/LICENSE)
Package netpollmux implements a network poller based on epoll/kqueue.
## Features
* Epoll/kqueue
* TCP/UNIX
* Compatible with the net.Conn interface.
* Upgrade connection
* Non-blocking I/O
* Splice/sendfile
* Rescheduling workers
**Comparison to other packages.**
|Package| [net](https://github.com/golang/go/tree/master/src/net "net")| [netpollmux](https://github.com/php2go/netpollmux "netpoll")|[gnet](https://github.com/panjf2000/gnet "gnet")|[evio](https://github.com/tidwall/evio "evio")|
|:--:|:--|:--|:--|:--|
|Low memory usage|No|Yes|Yes|Yes|
|Non-blocking I/O|No|Yes|Yes|Yes|
|Splice/sendfile|Yes|Yes|No|No|
|Rescheduling|Yes|Yes|No|No|
|Compatible with the net.Conn interface|Yes|Yes|No|No|
## [Benchmark](http://github.com/hslam/netpoll-benchmark "netpoll-benchmark")
<img src="https://raw.githubusercontent.com/php2go/netpollmux/main/netpoll-qps.png" width = "400" height = "300" alt="mock 0ms" align=center><img src="https://raw.githubusercontent.com/php2go/netpollmux/main/netpoll-mock-time-qps.png" width = "400" height = "300" alt="mock 1ms" align=center>
## Get started
### Install
```
go get github.com/php2go/netpollmux
```
### Import
```
import "github.com/php2go/netpollmux"
```
### Usage
#### Simple Example
```go
package main
import "github.com/php2go/netpollmux"
func main() {
var handler = &netpoll.DataHandler{
NoShared: true,
NoCopy: true,
BufferSize: 1024,
HandlerFunc: func(req []byte) (res []byte) {
res = req
return
},
}
if err := netpoll.ListenAndServe("tcp", ":9999", handler); err != nil {
panic(err)
}
}
```
#### TLS Example
```go
package main
import (
"crypto/tls"
"github.com/php2go/netpollmux/internal/socket"
"github.com/php2go/netpollmux/netpoll"
"net"
)
func main() {
var handler = &netpoll.DataHandler{
NoShared: true,
NoCopy: true,
BufferSize: 1024,
HandlerFunc: func(req []byte) (res []byte) {
res = req
return
},
}
handler.SetUpgrade(func(conn net.Conn) (net.Conn, error) {
tlsConn := tls.Server(conn, socket.DefalutTLSConfig())
if err := tlsConn.Handshake(); err != nil {
return nil, err
}
return tlsConn, nil
})
if err := netpoll.ListenAndServe("tcp", ":9999", handler); err != nil {
panic(err)
}
}
```
#### Websocket Example
```go
package main
import (
"github.com/php2go/netpollmux/internal/websocket"
"github.com/php2go/netpollmux/netpoll"
"net"
)
func main() {
var handler = &netpoll.ConnHandler{}
handler.SetUpgrade(func(conn net.Conn) (netpoll.Context, error) {
return websocket.Upgrade(conn, nil)
})
handler.SetServe(func(context netpoll.Context) error {
ws := context.(*websocket.Conn)
msg, err := ws.ReadMessage()
if err != nil {
return err
}
return ws.WriteMessage(msg)
})
if err := netpoll.ListenAndServe("tcp", ":9999", handler); err != nil {
panic(err)
}
}
```
#### HTTP Example
```go
package main
import (
"bufio"
"log"
"net"
"net/http"
"sync"
"github.com/php2go/netpollmux/internal/logger"
"github.com/php2go/netpollmux/mux"
"github.com/php2go/netpollmux/netpoll"
)
func main() {
m := mux.NewRoute()
m.GET("/hello/:id", func(w http.ResponseWriter, req *http.Request, params httprouter.Params) {
pp := req.URL.Query()
logger.Info("query params:", params, pp)
mux.JSON(w, req, []string{"hello world"}, http.StatusOK)
})
log.Fatal(m.Run(":8080"))
}
```
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
io模型 计算机的io模型区分为多种,目前用的最多的也就是nio、epoll、select。 结合不同场景使用不同的io模型才是正解。 具体可以查看我之前写的io模型演进。io模型演进 golang中网络io golang天然适合并发,为什么?一个是轻量级的协程,二个是将复杂的io进行了抽象化,简化了流程。 groutinue 针对io事件的调度 我们这里假设你对groutinue调度已经有一定的了解了。 我们知道,在go中,每个process绑定一个虚拟的machine,而在machine中,是具有一个g0的,g0在本地遍历自己的队列获取g或者从全局队列获取
资源推荐
资源详情
资源评论
收起资源包目录
netpollmux 实现了一个基于 epollkqueue 的网络轮询器。.rar (91个子文件)
netpollmux 实现了一个基于 epollkqueue 的网络轮询器。
新建文本文档.txt 19B
netpollmux-main
netpoll-qps.png 26KB
go.mod 89B
.github
ISSUE_TEMPLATE
feature_request.md 595B
bug_report.md 834B
go.sum 352B
version.go 45B
LICENSE 11KB
netpoll
net.go 2KB
poll_test.go 1KB
poll_bsd.go 3KB
net_test.go 2KB
handler_test.go 2KB
handler.go 4KB
net_unix_test.go 21KB
net_other.go 2KB
poll_linux.go 3KB
poll_other.go 1KB
poll.go 326B
net_unix.go 17KB
netpoll-mock-time-qps.png 43KB
mux
proxy.go 733B
route_test.go 8KB
compress_deflate.go 872B
mux_test.go 10KB
response_test.go 8KB
header.go 2KB
header_encoding.go 408B
response.go 14KB
request.go 3KB
header_content.go 670B
header_test.go 390B
request_test.go 2KB
remote.go 641B
proxy_test.go 445B
render_template.go 3KB
header_access_control.go 735B
mux.go 8KB
render.go 5KB
compress.go 2KB
compress_gzip.go 637B
internal
scheduler
scheduler_test.go 4KB
scheduler.go 5KB
sendfile
sendfile_unix.go 1007B
sendfile.go 985B
sendfile_windows.go 246B
sendfile_other.go 387B
sendfile_test.go 5KB
mmap
mmap.go 1KB
mmap_test.go 1KB
mmap_unix.go 1KB
mmap_windows.go 3KB
mmap_other.go 3KB
socket
messages.go 5KB
socket_test.go 6KB
socket_ws.go 4KB
socket_inproc.go 5KB
socket_http.go 7KB
tls_test.go 872B
socket_inproc_test.go 385B
messages_test.go 2KB
socket_tcp.go 5KB
tls.go 4KB
socket_unix.go 5KB
socket.go 3KB
logger
logger.go 12KB
logger_test.go 2KB
writer
writer_test.go 4KB
writer.go 5KB
inproc
inproc.go 4KB
inproc_test.go 1KB
websocket
messages.go 3KB
frame_test.go 5KB
websocket.go 4KB
websocket_test.go 14KB
messages_test.go 3KB
conn_test.go 2KB
handshake_test.go 4KB
frame.go 7KB
conn.go 3KB
handshake.go 4KB
splice
splice_linux.go 3KB
splice_other.go 1015B
splice_test.go 4KB
splice.go 4KB
buffer
buffer.go 2KB
buffer_test.go 10KB
test
netpoll.go 1KB
netpoll_benchmark
server.go 683B
.gitignore 372B
README.md 4KB
共 91 条
- 1
资源评论
野生的狒狒
- 粉丝: 2474
- 资源: 2142
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功