Gev is a lightweight, fast non-blocking TCP network library / websocket server based on Reactor mode. Support custom protocols to quickly and easily build hig
Gev is a lightweight, fast non-blocking TCP network library / websocket server based on Reactor mode. Support custom protocols to quickly and easily build hig
gev is a lightweight, fast non-blocking TCP network library / websocket server based on Reactor mode.
Support custom protocols to quickly and easily build high-performance servers.
gev uses only a few goroutines, one of them listens for connections and the others (work coroutines) handle read and write events of connected clients. The count of work coroutines is configurable, which is the core number of host CPUs by default.
Test environment: Ubuntu18.04 | 4 Virtual CPUs | 4.0 GiB
limit GOMAXPROCS=1(Single thread),1 work goroutine
limit GOMAXPROCS=4,4 work goroutine
Compared with the simple performance of similar libraries, the pressure measurement method is the same as the evio project.
limit GOMAXPROCS=1,1 work goroutine
limit GOMAXPROCS=1,4 work goroutine
limit GOMAXPROCS=4,4 work goroutine
go get -u github.com/Allenxuxu/gev…Handler is an interface that programs must implement.
type CallBack interface {
OnMessage(c *Connection, ctx interface{}, data []byte) interface{}
OnClose(c *Connection)
}
type Handler interface {
CallBack
OnConnect(c *Connection)
}OnMessage will be called back when a complete data frame arrives.Users can get the data, process the business logic, and return the data that needs to be sent.
When there is data coming, gev does not call back OnMessage immediately, but instead calls back an UnPacket function.Probably the execution logic is as follows:
ctx, receivedData := c.protocol.UnPacket(c, buffer)
for ctx != nil || len(receivedData) != 0 {
sendData := c.callBack.OnMessage(c, ctx, receivedData)
if sendData != nil {
*tmpBuffer = append(*tmpBuffer, c.protocol.Packet(c, sendData)...)
}
ctx, receivedData = c.protocol.UnPacket(c, buffer)
}The UnPacket function will check whether the data in the ringbuffer is a complete data frame. If it is, the data will be unpacked and return the payload data. If it is not a complete data frame, it will return directly.
The return value of UnPacket (interface{}, []byte) will be passed in as a call to OnMessage ctx interface{}, data []byte and callback.Ctx is designed to pass special information generated when parsing data frames in the UnPacket function (which is required for complex data frame protocols), and data is used to pass payload data.
…As above, gev provides a default Protocol implementation that will fetch all data in the receive buffer ( ringbuffer ).In actual use, there is usually a data frame protocol of its own, and gev can be set in the form of a plug-in: it is set by variable parameters when creating Server.
s, err := gev.NewServer(handler,gev.Protocol(&ExampleProtocol{}))Check out the example Protocol for a detailed.
There is also a Send method that can be used for sending data. But Send puts the data to Event-Loop and invokes it to send the data rather than sending data by itself immediately.
Check out the example Server timing push for a detailed.
func (c *Connection) Send(data interface{}, opts ...ConnectionOption) errorShutdownWrite works for reverting connected status to false and closing connection.
Check out the example Maximum connections for a detailed.
func (c *Connection) ShutdownWrite() errorRingBuffer is a dynamical expansion implementation of circular buffer.
The WebSocket protocol is built on top of the TCP protocol, so gev doesn't need to be built in, but instead provides support in the form of plugins, in the plugins/websocket directory.
code…The detailed implementation can be viewed by the plugin. The source code can be viewed using the websocket example.
Paypal: Paypal/AllenXuxu
No open issues yet, or sync has not completed.