Package netpollmux implements a network poller based on epoll/kqueue.
- 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 | netpollmux | gnet | 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 |
go get github.com/php2go/netpollmux
import "github.com/php2go/netpollmux"
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)
}
}
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)
}
}
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)
}
}
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"))
}