支持海量连接的 websocket 库,callback 写法
https://github.com/antlabs/greatws
早期阶段,暂时不建议生产使用
type echoHandler struct{}
func (e *echoHandler) OnOpen(c *greatws.Conn) {
// fmt.Printf("OnOpen: %p\n", c)
}
func (e *echoHandler) OnMessage(c *greatws.Conn, op greatws.Opcode, msg []byte) {
if err := c.WriteTimeout(op, msg, 3*time.Second); err != nil {
fmt.Println("write fail:", err)
}
// if err := c.WriteMessage(op, msg); err != nil {
// slog.Error("write fail:", err)
// }
}
func (e *echoHandler) OnClose(c *greatws.Conn, err error) {
errMsg := ""
if err != nil {
errMsg = err.Error()
}
slog.Error("OnClose:", errMsg)
}
type handler struct {
m *greatws.MultiEventLoop
}
func (h *handler) echo(w http.ResponseWriter, r *http.Request) {
c, err := greatws.Upgrade(w, r,
greatws.WithServerReplyPing(),
// greatws.WithServerDecompression(),
greatws.WithServerIgnorePong(),
greatws.WithServerCallback(&echoHandler{}),
// greatws.WithServerEnableUTF8Check(),
greatws.WithServerReadTimeout(5*time.Second),
greatws.WithServerMultiEventLoop(h.m),
)
if err != nil {
slog.Error("Upgrade fail:", "err", err.Error())
}
_ = c
}
func main() {
var h handler
h.m = greatws.NewMultiEventLoopMust(greatws.WithEventLoops(0), greatws.WithMaxEventNum(256), greatws.WithLogLevel(slog.LevelError)) // epoll, kqueue
h.m.Start()
fmt.Printf("apiname:%s\n", h.m.GetApiName())
mux := &http.ServeMux{}
mux.HandleFunc("/autobahn", h.echo)
rawTCP, err := net.Listen("tcp", ":9001")
if err != nil {
fmt.Println("Listen fail:", err)
return
}
log.Println("non-tls server exit:", http.Serve(rawTCP, mux))
}
BenchType : BenchEcho
Framework : greatws
TPS : 27954
EER : 225.42
Min : 35.05us
Avg : 1.79s
Max : 2.74s
TP50 : 1.88s
TP75 : 1.95s
TP90 : 1.99s
TP95 : 2.02s
TP99 : 2.09s
Used : 178.86s
Total : 5000000
Success : 5000000
Failed : 0
Conns : 1000000
Concurrency: 50000
Payload : 1024
CPU Min : 41.62%
CPU Avg : 124.01%
CPU Max : 262.72%
MEM Min : 555.25M
MEM Avg : 562.44M
MEM Max : 626.47M
BenchType : BenchEcho
Framework : greatws
TPS : 82088
EER : 447.72
Min : -1ns
Avg : 605.25ms
Max : 1.68s
TP50 : 609.79ms
TP75 : 709.26ms
TP90 : 761.86ms
TP95 : 771.77ms
TP99 : 779.10ms
Used : 50.47s
Total : 5000000
Success : 4142842
Failed : 857158
Conns : 1000000
Concurrency: 50000
Payload : 1024
CPU Min : 114.33%
CPU Avg : 183.35%
CPU Max : 280.22%
MEM Min : 625.27M
MEM Avg : 632.89M
MEM Max : 666.96M
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.