【开源自荐】UringNet新一代基于io_uring的超高性能网络库

2023-12-12 14 views
2
推荐项目
  • 类别:Go
  • 项目标题:UringNet新一代基于io_uring的超高性能网络库
  • 项目描述:UringNet由于采用了最新的异步框架,因此在同等硬件配置条件下,UringNet比目前性能最好的基于select/epoll的框架的网络I/O的峰值数据还要高10%-30%,在极端情况下有可能高出60%以上。

import ( "bytes" "github.com/y001j/UringNet" "github.com/y001j/UringNet/sockets" "os" "runtime" "sync" "time" )

type testServer struct { UringNet.BuiltinEventEngine

testloop *UringNet.Ringloop
//ring      *uring_net.URingNet
addr      string
multicore bool

}

type httpCodec struct { delimiter []byte buf []byte }

func appendResponse(hc []byte) { hc = append(hc, "HTTP/1.1 200 OK\r\nServer: uringNet\r\nContent-Type: text/plain\r\nDate: "...) hc = time.Now().AppendFormat(hc, "Mon, 02 Jan 2006 15:04:05 GMT") hc = append(*hc, "\r\nContent-Length: 12\r\n\r\nHello World!"...) }

var ( errMsg = "Internal Server Error" errMsgBytes = []byte(errMsg) )

func (ts testServer) OnTraffic(data UringNet.UserData, ringnet UringNet.URingNet) UringNet.Action {

buffer := ringnet.ReadBuffer

count := bytes.Count(buffer, []byte("GET"))
if count == 0 {
    return UringNet.None
} else {
    for i := 0; i < count; i++ {
        appendResponse(&data.WriteBuf)
    }
}
return UringNet.Echo

}

func (ts *testServer) OnWritten(data UringNet.UserData) UringNet.Action {

return UringNet.None

}

func (ts testServer) OnOpen(data UringNet.UserData) ([]byte, UringNet.Action) {

ts.SetContext(&httpCodec{delimiter: []byte("\r\n\r\n")})
return nil, UringNet.None

}

func main() { addr := os.Args[1] //runtime.GOMAXPROCS(runtime.NumCPU())

options := socket.SocketOptions{TCPNoDelay: socket.TCPNoDelay, ReusePort: true}
ringNets, _ := UringNet.NewMany(UringNet.NetAddress{socket.Tcp4, addr}, 3200, true, runtime.NumCPU(), options, &testServer{}) 

loop := UringNet.SetLoops(ringNets, 4000)

var waitgroup sync.WaitGroup
waitgroup.Add(1)

loop.RunMany()

waitgroup.Wait()

}



- 截图:

- 后续更新计划:重构代码,提高可读性和规范性。

回答