- NBIO - NON-BLOCKING IO
- Linux: Epoll, both ET/LT(as default) supported
- BSD(MacOS): Kqueue
- Windows: Based on std net, for debugging only
- TCP/UDP/Unix Socket supported
- TLS supported
- HTTP/HTTPS 1.x supported
- Websocket supported, Passes the Autobahn Test Suite,
OnOpen/OnMessage/OnClose
order guaranteed
- Implements a non-blocking net.Conn(except windows)
- SetDeadline/SetReadDeadline/SetWriteDeadline supported
- Concurrent Write/Close supported(both nbio.Conn and nbio/nbhttp/websocket.Conn)
package main
import (
"log"
"github.com/gozelle/nbio"
)
func main() {
engine := nbio.NewEngine(nbio.Config{
Network: "tcp",//"udp", "unix"
Addrs: []string{":8888"},
MaxWriteBufferSize: 6 * 1024 * 1024,
})
// hanlde new connection
engine.OnOpen(func(c *nbio.Conn) {
log.Println("OnOpen:", c.RemoteAddr().String())
})
// hanlde connection closed
engine.OnClose(func(c *nbio.Conn, err error) {
log.Println("OnClose:", c.RemoteAddr().String(), err)
})
// handle data
engine.OnData(func(c *nbio.Conn, data []byte) {
c.Write(append([]byte{}, data...))
})
err := engine.Start()
if err != nil {
log.Fatalf("nbio.Start failed: %v\n", err)
return
}
defer engine.Stop()
<-make(chan int)
}
IOMod | Remarks |
---|---|
IOModNonBlocking | There's no difference between this IOMod and the old version with no IOMod. All the connections will be handled by poller. |
IOModBlocking | All the connections will be handled by at least one goroutine, for websocket, we can set Upgrader.BlockingModAsyncWrite=true to handle writting with a separated goroutine and then avoid Head-of-line blocking on broadcasting scenarios. |
IOModMixed | We set the Engine.MaxBlockingOnline, if the online num is smaller than it, the new connection will be handled by single goroutine as IOModBlocking, else the new connection will be handled by poller. |
The IOModBlocking
aims to improve the performance for low online service, it runs faster than std.
The IOModMixed
aims to keep a balance between performance and cpu/mem cost in different scenarios: when there are not too many online connections, it performs better than std, or else it can serve lots of online connections and keep healthy.
package main
import (
"fmt"
"log"
"net/http"
"github.com/gozelle/nbio/nbhttp/websocket"
)
func echo(w http.ResponseWriter, r *http.Request) {
u := websocket.NewUpgrader()
u.OnMessage(func(c *websocket.Conn, mt websocket.MessageType, data []byte) {
c.WriteMessage(mt, data)
})
_, err := u.Upgrade(w, r, nil)
if err != nil {
log.Print("upgrade:", err)
return
}
}
func main() {
mux := &http.ServeMux{}
mux.HandleFunc("/ws", echo)
server := http.Server{
Addr: "localhost:8080",
Handler: mux,
}
fmt.Println("server exit:", server.ListenAndServe())
}