perf: 提高消息吞吐量,降低消息延迟
This commit is contained in:
parent
3ca6ed00ec
commit
1cbe8ecf56
|
@ -3,27 +3,33 @@ package client
|
||||||
import (
|
import (
|
||||||
"github.com/kercylan98/minotaur/utils/concurrent"
|
"github.com/kercylan98/minotaur/utils/concurrent"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// NewClient 创建客户端
|
// NewClient 创建客户端
|
||||||
func NewClient(core Core) *Client {
|
func NewClient(core Core) *Client {
|
||||||
client := &Client{
|
client := &Client{
|
||||||
|
cond: sync.NewCond(&sync.Mutex{}),
|
||||||
events: new(events),
|
events: new(events),
|
||||||
core: core,
|
core: core,
|
||||||
}
|
}
|
||||||
return client
|
return client
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// CloneClient 克隆客户端
|
||||||
|
func CloneClient(client *Client) *Client {
|
||||||
|
return NewClient(client.core.Clone())
|
||||||
|
}
|
||||||
|
|
||||||
// Client 客户端
|
// Client 客户端
|
||||||
type Client struct {
|
type Client struct {
|
||||||
*events
|
*events
|
||||||
core Core
|
core Core
|
||||||
mutex sync.Mutex
|
cond *sync.Cond
|
||||||
packetPool *concurrent.Pool[*Packet]
|
packetPool *concurrent.Pool[*Packet]
|
||||||
packets []*Packet
|
packets []*Packet
|
||||||
|
|
||||||
accumulate []*Packet
|
accumulate []*Packet
|
||||||
|
accumulation int // 积压消息数
|
||||||
}
|
}
|
||||||
|
|
||||||
func (slf *Client) Run() error {
|
func (slf *Client) Run() error {
|
||||||
|
@ -38,14 +44,14 @@ func (slf *Client) Run() error {
|
||||||
}()
|
}()
|
||||||
err := <-runState
|
err := <-runState
|
||||||
if err != nil {
|
if err != nil {
|
||||||
slf.mutex.Lock()
|
slf.cond.L.Lock()
|
||||||
if slf.packetPool != nil {
|
if slf.packetPool != nil {
|
||||||
slf.packetPool.Close()
|
slf.packetPool.Close()
|
||||||
slf.packetPool = nil
|
slf.packetPool = nil
|
||||||
}
|
}
|
||||||
slf.accumulate = append(slf.accumulate, slf.packets...)
|
slf.accumulate = append(slf.accumulate, slf.packets...)
|
||||||
slf.packets = nil
|
slf.packets = nil
|
||||||
slf.mutex.Unlock()
|
slf.cond.L.Unlock()
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
var wait = new(sync.WaitGroup)
|
var wait = new(sync.WaitGroup)
|
||||||
|
@ -98,7 +104,10 @@ func (slf *Client) write(wst int, packet []byte, callback ...func(err error)) {
|
||||||
if len(callback) > 0 {
|
if len(callback) > 0 {
|
||||||
p.callback = callback[0]
|
p.callback = callback[0]
|
||||||
}
|
}
|
||||||
|
slf.cond.L.Lock()
|
||||||
slf.accumulate = append(slf.accumulate, p)
|
slf.accumulate = append(slf.accumulate, p)
|
||||||
|
slf.accumulation = len(slf.accumulate) + len(slf.packets)
|
||||||
|
slf.cond.L.Unlock()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
cp := slf.packetPool.Get()
|
cp := slf.packetPool.Get()
|
||||||
|
@ -107,9 +116,11 @@ func (slf *Client) write(wst int, packet []byte, callback ...func(err error)) {
|
||||||
if len(callback) > 0 {
|
if len(callback) > 0 {
|
||||||
cp.callback = callback[0]
|
cp.callback = callback[0]
|
||||||
}
|
}
|
||||||
slf.mutex.Lock()
|
slf.cond.L.Lock()
|
||||||
slf.packets = append(slf.packets, cp)
|
slf.packets = append(slf.packets, cp)
|
||||||
slf.mutex.Unlock()
|
slf.accumulation = len(slf.accumulate) + len(slf.packets)
|
||||||
|
slf.cond.Signal()
|
||||||
|
slf.cond.L.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
// writeLoop 写循环
|
// writeLoop 写循环
|
||||||
|
@ -123,30 +134,29 @@ func (slf *Client) writeLoop(wait *sync.WaitGroup) {
|
||||||
data.callback = nil
|
data.callback = nil
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
slf.mutex.Lock()
|
slf.cond.L.Lock()
|
||||||
slf.packets = append(slf.packets, slf.accumulate...)
|
slf.packets = append(slf.packets, slf.accumulate...)
|
||||||
slf.accumulate = nil
|
slf.accumulate = nil
|
||||||
slf.mutex.Unlock()
|
slf.cond.L.Unlock()
|
||||||
defer func() {
|
defer func() {
|
||||||
if err := recover(); err != nil {
|
if err := recover(); err != nil {
|
||||||
slf.Close(err.(error))
|
slf.Close(err.(error))
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
wait.Done()
|
wait.Done()
|
||||||
|
|
||||||
for {
|
for {
|
||||||
slf.mutex.Lock()
|
slf.cond.L.Lock()
|
||||||
if slf.packetPool == nil {
|
if slf.packetPool == nil {
|
||||||
slf.mutex.Unlock()
|
slf.cond.L.Unlock()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if len(slf.packets) == 0 {
|
if len(slf.packets) == 0 {
|
||||||
slf.mutex.Unlock()
|
slf.cond.Wait()
|
||||||
time.Sleep(50 * time.Millisecond)
|
|
||||||
continue
|
|
||||||
}
|
}
|
||||||
packets := slf.packets[0:]
|
packets := slf.packets[0:]
|
||||||
slf.packets = slf.packets[0:0]
|
slf.packets = slf.packets[0:0]
|
||||||
slf.mutex.Unlock()
|
slf.cond.L.Unlock()
|
||||||
for i := 0; i < len(packets); i++ {
|
for i := 0; i < len(packets); i++ {
|
||||||
data := packets[i]
|
data := packets[i]
|
||||||
var err = slf.core.Write(data)
|
var err = slf.core.Write(data)
|
||||||
|
@ -170,3 +180,8 @@ func (slf *Client) onReceive(wst int, packet []byte) {
|
||||||
func (slf *Client) GetServerAddr() string {
|
func (slf *Client) GetServerAddr() string {
|
||||||
return slf.core.GetServerAddr()
|
return slf.core.GetServerAddr()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetMessageAccumulationTotal 获取消息积压总数
|
||||||
|
func (slf *Client) GetMessageAccumulationTotal() int {
|
||||||
|
return slf.accumulation
|
||||||
|
}
|
||||||
|
|
|
@ -14,4 +14,7 @@ type Core interface {
|
||||||
|
|
||||||
// GetServerAddr 获取服务器地址
|
// GetServerAddr 获取服务器地址
|
||||||
GetServerAddr() string
|
GetServerAddr() string
|
||||||
|
|
||||||
|
// Clone 克隆客户端
|
||||||
|
Clone() Core
|
||||||
}
|
}
|
||||||
|
|
|
@ -34,3 +34,9 @@ func (slf *TCP) Close() {
|
||||||
func (slf *TCP) GetServerAddr() string {
|
func (slf *TCP) GetServerAddr() string {
|
||||||
return slf.addr
|
return slf.addr
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (slf *TCP) Clone() Core {
|
||||||
|
return &TCP{
|
||||||
|
addr: slf.addr,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -36,3 +36,9 @@ func (slf *UnixDomainSocket) Close() {
|
||||||
func (slf *UnixDomainSocket) GetServerAddr() string {
|
func (slf *UnixDomainSocket) GetServerAddr() string {
|
||||||
return slf.addr
|
return slf.addr
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (slf *UnixDomainSocket) Clone() Core {
|
||||||
|
return &UnixDomainSocket{
|
||||||
|
addr: slf.addr,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -51,3 +51,9 @@ func (slf *Websocket) Close() {
|
||||||
func (slf *Websocket) GetServerAddr() string {
|
func (slf *Websocket) GetServerAddr() string {
|
||||||
return slf.addr
|
return slf.addr
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (slf *Websocket) Clone() Core {
|
||||||
|
return &Websocket{
|
||||||
|
addr: slf.addr,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -11,7 +11,6 @@ import (
|
||||||
"runtime/debug"
|
"runtime/debug"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// newKcpConn 创建一个处理KCP的连接
|
// newKcpConn 创建一个处理KCP的连接
|
||||||
|
@ -19,6 +18,7 @@ func newKcpConn(server *Server, session *kcp.UDPSession) *Conn {
|
||||||
c := &Conn{
|
c := &Conn{
|
||||||
ctx: server.ctx,
|
ctx: server.ctx,
|
||||||
connection: &connection{
|
connection: &connection{
|
||||||
|
cond: sync.NewCond(&sync.Mutex{}),
|
||||||
server: server,
|
server: server,
|
||||||
remoteAddr: session.RemoteAddr(),
|
remoteAddr: session.RemoteAddr(),
|
||||||
ip: session.RemoteAddr().String(),
|
ip: session.RemoteAddr().String(),
|
||||||
|
@ -41,6 +41,7 @@ func newGNetConn(server *Server, conn gnet.Conn) *Conn {
|
||||||
c := &Conn{
|
c := &Conn{
|
||||||
ctx: server.ctx,
|
ctx: server.ctx,
|
||||||
connection: &connection{
|
connection: &connection{
|
||||||
|
cond: sync.NewCond(&sync.Mutex{}),
|
||||||
server: server,
|
server: server,
|
||||||
remoteAddr: conn.RemoteAddr(),
|
remoteAddr: conn.RemoteAddr(),
|
||||||
ip: conn.RemoteAddr().String(),
|
ip: conn.RemoteAddr().String(),
|
||||||
|
@ -63,6 +64,7 @@ func newWebsocketConn(server *Server, ws *websocket.Conn, ip string) *Conn {
|
||||||
c := &Conn{
|
c := &Conn{
|
||||||
ctx: server.ctx,
|
ctx: server.ctx,
|
||||||
connection: &connection{
|
connection: &connection{
|
||||||
|
cond: sync.NewCond(&sync.Mutex{}),
|
||||||
server: server,
|
server: server,
|
||||||
remoteAddr: ws.RemoteAddr(),
|
remoteAddr: ws.RemoteAddr(),
|
||||||
ip: ip,
|
ip: ip,
|
||||||
|
@ -82,6 +84,7 @@ func newGatewayConn(conn *Conn, connId string) *Conn {
|
||||||
c := &Conn{
|
c := &Conn{
|
||||||
//ctx: server.ctx,
|
//ctx: server.ctx,
|
||||||
connection: &connection{
|
connection: &connection{
|
||||||
|
cond: sync.NewCond(&sync.Mutex{}),
|
||||||
server: conn.server,
|
server: conn.server,
|
||||||
data: map[any]any{},
|
data: map[any]any{},
|
||||||
},
|
},
|
||||||
|
@ -97,6 +100,7 @@ func NewEmptyConn(server *Server) *Conn {
|
||||||
c := &Conn{
|
c := &Conn{
|
||||||
ctx: server.ctx,
|
ctx: server.ctx,
|
||||||
connection: &connection{
|
connection: &connection{
|
||||||
|
cond: sync.NewCond(&sync.Mutex{}),
|
||||||
server: server,
|
server: server,
|
||||||
remoteAddr: &net.TCPAddr{},
|
remoteAddr: &net.TCPAddr{},
|
||||||
ip: "0.0.0.0:0",
|
ip: "0.0.0.0:0",
|
||||||
|
@ -126,7 +130,7 @@ type connection struct {
|
||||||
kcp *kcp.UDPSession
|
kcp *kcp.UDPSession
|
||||||
gw func(packet []byte)
|
gw func(packet []byte)
|
||||||
data map[any]any
|
data map[any]any
|
||||||
mutex sync.Mutex
|
cond *sync.Cond
|
||||||
packetPool *concurrent.Pool[*connPacket]
|
packetPool *concurrent.Pool[*connPacket]
|
||||||
packets []*connPacket
|
packets []*connPacket
|
||||||
}
|
}
|
||||||
|
@ -140,11 +144,11 @@ func (slf *Conn) IsEmpty() bool {
|
||||||
// - 重用连接时,会将当前连接的数据复制到新连接中
|
// - 重用连接时,会将当前连接的数据复制到新连接中
|
||||||
// - 通常在于连接断开后,重新连接时使用
|
// - 通常在于连接断开后,重新连接时使用
|
||||||
func (slf *Conn) Reuse(conn *Conn) {
|
func (slf *Conn) Reuse(conn *Conn) {
|
||||||
slf.mutex.Lock()
|
slf.cond.L.Lock()
|
||||||
conn.mutex.Lock()
|
conn.cond.L.Lock()
|
||||||
defer func() {
|
defer func() {
|
||||||
slf.mutex.Unlock()
|
slf.cond.L.Unlock()
|
||||||
conn.mutex.Unlock()
|
conn.cond.L.Unlock()
|
||||||
}()
|
}()
|
||||||
slf.Close()
|
slf.Close()
|
||||||
slf.remoteAddr = conn.remoteAddr
|
slf.remoteAddr = conn.remoteAddr
|
||||||
|
@ -253,9 +257,10 @@ func (slf *Conn) Write(packet []byte, callback ...func(err error)) {
|
||||||
if len(callback) > 0 {
|
if len(callback) > 0 {
|
||||||
cp.callback = callback[0]
|
cp.callback = callback[0]
|
||||||
}
|
}
|
||||||
slf.mutex.Lock()
|
slf.cond.L.Lock()
|
||||||
slf.packets = append(slf.packets, cp)
|
slf.packets = append(slf.packets, cp)
|
||||||
slf.mutex.Unlock()
|
slf.cond.Signal()
|
||||||
|
slf.cond.L.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
// writeLoop 写循环
|
// writeLoop 写循环
|
||||||
|
@ -278,18 +283,17 @@ func (slf *Conn) writeLoop(wait *sync.WaitGroup) {
|
||||||
}()
|
}()
|
||||||
wait.Done()
|
wait.Done()
|
||||||
for {
|
for {
|
||||||
slf.mutex.Lock()
|
slf.cond.L.Lock()
|
||||||
if slf.packetPool == nil {
|
if slf.packetPool == nil {
|
||||||
|
slf.cond.L.Unlock()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if len(slf.packets) == 0 {
|
if len(slf.packets) == 0 {
|
||||||
slf.mutex.Unlock()
|
slf.cond.Wait()
|
||||||
time.Sleep(50 * time.Millisecond)
|
|
||||||
continue
|
|
||||||
}
|
}
|
||||||
packets := slf.packets[0:]
|
packets := slf.packets[0:]
|
||||||
slf.packets = slf.packets[0:0]
|
slf.packets = slf.packets[0:0]
|
||||||
slf.mutex.Unlock()
|
slf.cond.L.Unlock()
|
||||||
for i := 0; i < len(packets); i++ {
|
for i := 0; i < len(packets); i++ {
|
||||||
data := packets[i]
|
data := packets[i]
|
||||||
var err error
|
var err error
|
||||||
|
|
Loading…
Reference in New Issue