feat 优化网络库
This commit is contained in:
@@ -21,8 +21,8 @@ public class SocketManager : MonoBehaviour
|
|||||||
|
|
||||||
public async void Connect()
|
public async void Connect()
|
||||||
{
|
{
|
||||||
_ws = new WebSocket($"wss://www.hlsq.asia/ws/?token={Random.Range(1, 1000)}");
|
// _ws = new WebSocket($"wss://www.hlsq.asia/ws/?token={Random.Range(1, 1000)}");
|
||||||
// _ws = new WebSocket($"ws://127.0.0.1:8501/?token={Random.Range(1, 1000)}");
|
_ws = new WebSocket($"ws://127.0.0.1:8501/?token={Random.Range(1, 1000)}");
|
||||||
|
|
||||||
_ws.OnOpen += () =>
|
_ws.OnOpen += () =>
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ require (
|
|||||||
github.com/golang-jwt/jwt/v5 v5.3.0
|
github.com/golang-jwt/jwt/v5 v5.3.0
|
||||||
github.com/grpc-ecosystem/grpc-gateway/v2 v2.26.3
|
github.com/grpc-ecosystem/grpc-gateway/v2 v2.26.3
|
||||||
github.com/natefinch/lumberjack v2.0.0+incompatible
|
github.com/natefinch/lumberjack v2.0.0+incompatible
|
||||||
github.com/panjf2000/gnet/v2 v2.9.1
|
github.com/panjf2000/gnet/v2 v2.9.7
|
||||||
github.com/redis/go-redis/v9 v9.10.0
|
github.com/redis/go-redis/v9 v9.10.0
|
||||||
github.com/spf13/viper v1.21.0
|
github.com/spf13/viper v1.21.0
|
||||||
go.etcd.io/etcd/api/v3 v3.6.1
|
go.etcd.io/etcd/api/v3 v3.6.1
|
||||||
|
|||||||
@@ -109,6 +109,8 @@ github.com/panjf2000/ants/v2 v2.11.3 h1:AfI0ngBoXJmYOpDh9m516vjqoUu2sLrIVgppI9TZ
|
|||||||
github.com/panjf2000/ants/v2 v2.11.3/go.mod h1:8u92CYMUc6gyvTIw8Ru7Mt7+/ESnJahz5EVtqfrilek=
|
github.com/panjf2000/ants/v2 v2.11.3/go.mod h1:8u92CYMUc6gyvTIw8Ru7Mt7+/ESnJahz5EVtqfrilek=
|
||||||
github.com/panjf2000/gnet/v2 v2.9.1 h1:bKewICy/0xnQ9PMzNaswpe/Ah14w1TrRk91LHTcbIlA=
|
github.com/panjf2000/gnet/v2 v2.9.1 h1:bKewICy/0xnQ9PMzNaswpe/Ah14w1TrRk91LHTcbIlA=
|
||||||
github.com/panjf2000/gnet/v2 v2.9.1/go.mod h1:WQTxDWYuQ/hz3eccH0FN32IVuvZ19HewEWx0l62fx7E=
|
github.com/panjf2000/gnet/v2 v2.9.1/go.mod h1:WQTxDWYuQ/hz3eccH0FN32IVuvZ19HewEWx0l62fx7E=
|
||||||
|
github.com/panjf2000/gnet/v2 v2.9.7 h1:6zW7Jl3oAfXwSuh1PxHLndoL2MQRWx0AJR6aaQjxUgA=
|
||||||
|
github.com/panjf2000/gnet/v2 v2.9.7/go.mod h1:WQTxDWYuQ/hz3eccH0FN32IVuvZ19HewEWx0l62fx7E=
|
||||||
github.com/pelletier/go-toml/v2 v2.2.4 h1:mye9XuhQ6gvn5h28+VilKrrPoQVanw5PMw/TB0t5Ec4=
|
github.com/pelletier/go-toml/v2 v2.2.4 h1:mye9XuhQ6gvn5h28+VilKrrPoQVanw5PMw/TB0t5Ec4=
|
||||||
github.com/pelletier/go-toml/v2 v2.2.4/go.mod h1:2gIqNv+qfxSVS7cM2xJQKtLSTLUE9V8t9Stt+h56mCY=
|
github.com/pelletier/go-toml/v2 v2.2.4/go.mod h1:2gIqNv+qfxSVS7cM2xJQKtLSTLUE9V8t9Stt+h56mCY=
|
||||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||||
|
|||||||
@@ -28,9 +28,9 @@ const (
|
|||||||
|
|
||||||
// ISocketServer 由应用层实现
|
// ISocketServer 由应用层实现
|
||||||
type ISocketServer interface {
|
type ISocketServer interface {
|
||||||
OnOpen(ISocketConn) ([]byte, Action) // 开启连接
|
OnOpen(ISocketConn) ([]byte, Action) // 开启连接
|
||||||
OnHandShake(ISocketConn) // 开始握手
|
OnHandShake(ISocketConn, []byte, func(ISocketConn, []byte)) Action // 开始握手
|
||||||
OnMessage(ISocketConn, []byte) Action // 收到消息
|
OnMessage(ISocketConn, []byte) Action // 收到消息
|
||||||
OnPong(ISocketConn)
|
OnPong(ISocketConn)
|
||||||
OnClose(ISocketConn, error) Action // 关闭连接
|
OnClose(ISocketConn, error) Action // 关闭连接
|
||||||
OnTick() (time.Duration, Action)
|
OnTick() (time.Duration, Action)
|
||||||
|
|||||||
@@ -12,7 +12,6 @@ import (
|
|||||||
|
|
||||||
// WSServer 实现GNet库接口
|
// WSServer 实现GNet库接口
|
||||||
type WSServer struct {
|
type WSServer struct {
|
||||||
gnet.BuiltinEventEngine
|
|
||||||
eng gnet.Engine
|
eng gnet.Engine
|
||||||
i socket.ISocketServer
|
i socket.ISocketServer
|
||||||
logger logging.Logger // 日志
|
logger logging.Logger // 日志
|
||||||
@@ -20,29 +19,31 @@ type WSServer struct {
|
|||||||
unUpgradeConn sync.Map
|
unUpgradeConn sync.Map
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewWSServer(i socket.ISocketServer, logger logging.Logger, timeout time.Duration) *WSServer {
|
func NewWSServer(i socket.ISocketServer, logger logging.Logger, upgradeTimeout time.Duration) *WSServer {
|
||||||
if i == nil {
|
if i == nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
return &WSServer{
|
return &WSServer{
|
||||||
i: i,
|
i: i,
|
||||||
logger: logger,
|
logger: logger,
|
||||||
upgradeTimeout: timeout,
|
upgradeTimeout: upgradeTimeout,
|
||||||
unUpgradeConn: sync.Map{},
|
unUpgradeConn: sync.Map{},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *WSServer) Run(logger logging.Logger, addr string, multiCore, reusePort, tick, lockOSThread, reuseAddr bool, processNum int) error {
|
func (s *WSServer) Run(addr string, multicore bool, numEventLoop int, tcpNoDelay gnet.TCPSocketOpt, readBufferCap, writeBufferCap int, lockOSThread bool, reusePort, ticker bool, logger logging.Logger) error {
|
||||||
return gnet.Run(
|
return gnet.Run(
|
||||||
s,
|
s,
|
||||||
addr,
|
addr,
|
||||||
gnet.WithMulticore(multiCore),
|
gnet.WithMulticore(multicore),
|
||||||
gnet.WithNumEventLoop(processNum),
|
gnet.WithNumEventLoop(numEventLoop),
|
||||||
gnet.WithReusePort(reusePort),
|
gnet.WithTCPNoDelay(tcpNoDelay),
|
||||||
gnet.WithTicker(tick),
|
gnet.WithReadBufferCap(readBufferCap),
|
||||||
gnet.WithLogger(logger),
|
gnet.WithWriteBufferCap(writeBufferCap),
|
||||||
gnet.WithLockOSThread(lockOSThread),
|
gnet.WithLockOSThread(lockOSThread),
|
||||||
gnet.WithReuseAddr(reuseAddr),
|
gnet.WithReusePort(reusePort),
|
||||||
|
gnet.WithTicker(ticker),
|
||||||
|
gnet.WithLogger(logger),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -55,6 +56,9 @@ func (s *WSServer) OnBoot(eng gnet.Engine) gnet.Action {
|
|||||||
return gnet.None
|
return gnet.None
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *WSServer) OnShutdown(_ gnet.Engine) {
|
||||||
|
}
|
||||||
|
|
||||||
func (s *WSServer) OnOpen(c gnet.Conn) ([]byte, gnet.Action) {
|
func (s *WSServer) OnOpen(c gnet.Conn) ([]byte, gnet.Action) {
|
||||||
ws := &WSConn{
|
ws := &WSConn{
|
||||||
Conn: c,
|
Conn: c,
|
||||||
@@ -89,67 +93,43 @@ func (s *WSServer) OnClose(c gnet.Conn, err error) (action gnet.Action) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// OnTraffic fires when a local socket receives data from the peer.
|
// OnTraffic fires when a local socket receives data from the peer.
|
||||||
func (s *WSServer) OnTraffic(c gnet.Conn) (action gnet.Action) {
|
func (s *WSServer) OnTraffic(c gnet.Conn) gnet.Action {
|
||||||
tmp := c.Context()
|
ws := c.Context().(*WSConn)
|
||||||
if tmp == nil {
|
if action := ws.readBytesBuf(c); action != gnet.None {
|
||||||
s.logger.Errorf("OnTraffic context nil: %v", c)
|
return action
|
||||||
action = gnet.Close
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
ws, ok := tmp.(*WSConn)
|
|
||||||
if !ok {
|
|
||||||
ws.logger.Errorf("OnTraffic convert ws error: %v", tmp)
|
|
||||||
action = gnet.Close
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
action = ws.readBytesBuf(c)
|
|
||||||
if action != gnet.None {
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if !ws.isUpgrade {
|
if !ws.isUpgrade {
|
||||||
var data []byte
|
data, action := ws.upgrade()
|
||||||
data, ok, action = ws.upgrade()
|
if len(data) > 0 {
|
||||||
if ok {
|
|
||||||
s.unUpgradeConn.Delete(c.RemoteAddr().String())
|
s.unUpgradeConn.Delete(c.RemoteAddr().String())
|
||||||
s.i.OnHandShake(ws)
|
action = gnet.Action(s.i.OnHandShake(ws, data, s.OnHandShakeFinish))
|
||||||
if data != nil {
|
|
||||||
err := ws.Conn.AsyncWrite(data, nil)
|
|
||||||
if err != nil {
|
|
||||||
ws.logger.Errorf("update ws write upgrade protocol error", err)
|
|
||||||
action = gnet.Close
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
msg, err := ws.readWsMessages()
|
|
||||||
if err != nil {
|
|
||||||
ws.logger.Errorf("read ws messages errors", err)
|
|
||||||
return gnet.Close
|
|
||||||
}
|
}
|
||||||
|
return action
|
||||||
|
}
|
||||||
|
|
||||||
if msg != nil {
|
if msg, err := ws.readWsMessages(); err != nil {
|
||||||
for _, m := range msg {
|
ws.logger.Errorf("read ws messages err: %v", err)
|
||||||
if socket.OpCode(m.OpCode) == socket.OpPong {
|
return gnet.Close
|
||||||
s.i.OnPong(ws)
|
} else if msg != nil {
|
||||||
continue
|
for _, m := range msg {
|
||||||
}
|
if socket.OpCode(m.OpCode) == socket.OpPong {
|
||||||
if socket.OpCode(m.OpCode) == socket.OpClose {
|
s.i.OnPong(ws)
|
||||||
return gnet.Close
|
continue
|
||||||
}
|
}
|
||||||
if socket.OpCode(m.OpCode) == socket.OpPing {
|
if socket.OpCode(m.OpCode) == socket.OpClose {
|
||||||
continue
|
return gnet.Close
|
||||||
}
|
}
|
||||||
a := s.i.OnMessage(ws, m.Payload)
|
if socket.OpCode(m.OpCode) == socket.OpPing {
|
||||||
if gnet.Action(a) != gnet.None {
|
continue
|
||||||
action = gnet.Action(a)
|
}
|
||||||
}
|
a := s.i.OnMessage(ws, m.Payload)
|
||||||
|
if gnet.Action(a) != gnet.None {
|
||||||
|
return gnet.Action(a)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return
|
return gnet.None
|
||||||
}
|
}
|
||||||
|
|
||||||
// OnTick fires immediately after the engine starts and will fire again
|
// OnTick fires immediately after the engine starts and will fire again
|
||||||
@@ -194,3 +174,14 @@ func (s *WSServer) OnTick() (delay time.Duration, action gnet.Action) {
|
|||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// OnHandShakeFinish 握手完成
|
||||||
|
func (s *WSServer) OnHandShakeFinish(conn socket.ISocketConn, hsResp []byte) {
|
||||||
|
ws := conn.(*WSConn)
|
||||||
|
if err := ws.Conn.AsyncWrite(hsResp, nil); err != nil {
|
||||||
|
ws.logger.Errorf("OnHandShakeFinish err: %v", err)
|
||||||
|
if err = ws.Close(); err != nil {
|
||||||
|
ws.logger.Errorf("OnHandShakeFinish Close error: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -15,13 +15,14 @@ import (
|
|||||||
// WSConn 实现ISocketConn接口
|
// WSConn 实现ISocketConn接口
|
||||||
type WSConn struct {
|
type WSConn struct {
|
||||||
gnet.Conn
|
gnet.Conn
|
||||||
buf bytes.Buffer
|
buf bytes.Buffer
|
||||||
logger logging.Logger
|
logger logging.Logger
|
||||||
isUpgrade bool
|
isUpgrade bool // 是否已经升级
|
||||||
isClose bool
|
upgradeResp []byte // 升级响应
|
||||||
param map[string]interface{}
|
isClose bool
|
||||||
openTime int64
|
param map[string]interface{}
|
||||||
remoteAddr string
|
openTime int64 // 开启连接的时间
|
||||||
|
remoteAddr string // 远程ID地址
|
||||||
wsMessageBuf
|
wsMessageBuf
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -60,11 +61,7 @@ func (w *WSConn) readBytesBuf(c gnet.Conn) gnet.Action {
|
|||||||
return gnet.None
|
return gnet.None
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *WSConn) upgrade() (data []byte, ok bool, action gnet.Action) {
|
func (w *WSConn) upgrade() (data []byte, action gnet.Action) {
|
||||||
if w.isUpgrade {
|
|
||||||
ok = true
|
|
||||||
return
|
|
||||||
}
|
|
||||||
buf := &w.buf
|
buf := &w.buf
|
||||||
tmpReader := bytes.NewReader(buf.Bytes())
|
tmpReader := bytes.NewReader(buf.Bytes())
|
||||||
oldLen := tmpReader.Len()
|
oldLen := tmpReader.Len()
|
||||||
@@ -83,27 +80,24 @@ func (w *WSConn) upgrade() (data []byte, ok bool, action gnet.Action) {
|
|||||||
}
|
}
|
||||||
buf.Next(skipN)
|
buf.Next(skipN)
|
||||||
if w.logger != nil {
|
if w.logger != nil {
|
||||||
w.logger.Errorf("ws upgrade error", err.Error())
|
w.logger.Errorf("ws upgrade err: %v", err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
action = gnet.Close
|
action = gnet.Close
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
buf.Next(skipN)
|
buf.Next(skipN)
|
||||||
if w.logger != nil {
|
//if w.logger != nil {
|
||||||
w.logger.Infof("ws upgrade success conn upgrade websocket protocol!")
|
// w.logger.Infof("ws upgrade success conn upgrade websocket protocol!")
|
||||||
}
|
//}
|
||||||
_ = tempWriter.Flush()
|
_ = tempWriter.Flush()
|
||||||
data = result.Bytes()
|
data = result.Bytes()
|
||||||
ok = true
|
|
||||||
w.isUpgrade = true
|
w.isUpgrade = true
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *WSConn) readWsMessages() (messages []wsutil.Message, err error) {
|
func (w *WSConn) readWsMessages() (messages []wsutil.Message, err error) {
|
||||||
in := &w.buf
|
in := &w.buf
|
||||||
//messages, err = wsutil.ReadClientMessage(in, messages)
|
|
||||||
//return
|
|
||||||
for {
|
for {
|
||||||
if w.curHeader == nil {
|
if w.curHeader == nil {
|
||||||
if in.Len() < ws.MinHeaderSize { //头长度至少是2
|
if in.Len() < ws.MinHeaderSize { //头长度至少是2
|
||||||
@@ -126,13 +120,8 @@ func (w *WSConn) readWsMessages() (messages []wsutil.Message, err error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
//in.Next(skipN)
|
|
||||||
|
|
||||||
w.curHeader = &head
|
w.curHeader = &head
|
||||||
//err = ws.WriteHeader(&msgBuf.cachedBuf, head)
|
|
||||||
//if err != nil {
|
|
||||||
// return nil, err
|
|
||||||
//}
|
|
||||||
}
|
}
|
||||||
dataLen := (int)(w.curHeader.Length)
|
dataLen := (int)(w.curHeader.Length)
|
||||||
if dataLen > 0 {
|
if dataLen > 0 {
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"gateway/config"
|
"gateway/config"
|
||||||
"gateway/internal/net/ws_gateway"
|
"gateway/internal/net/ws_gateway"
|
||||||
|
"github.com/panjf2000/gnet/v2"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
@@ -31,14 +32,16 @@ func (m *ModuleWebsocketServer) start() error {
|
|||||||
go func() {
|
go func() {
|
||||||
defer m.wg.Done()
|
defer m.wg.Done()
|
||||||
_ = m.server.Run(
|
_ = m.server.Run(
|
||||||
log.GetLogger().Named("GNET"),
|
|
||||||
fmt.Sprintf("tcp4://0.0.0.0:%v", config.Get().Serve.Socket.Web.Port),
|
fmt.Sprintf("tcp4://0.0.0.0:%v", config.Get().Serve.Socket.Web.Port),
|
||||||
true,
|
true,
|
||||||
|
0,
|
||||||
|
gnet.TCPNoDelay,
|
||||||
|
64*1024,
|
||||||
|
64*1024,
|
||||||
true,
|
true,
|
||||||
false,
|
false,
|
||||||
false,
|
|
||||||
true,
|
true,
|
||||||
8,
|
log.GetLogger().Named("GNET"),
|
||||||
)
|
)
|
||||||
}()
|
}()
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
@@ -53,7 +53,7 @@ require (
|
|||||||
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
|
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
|
||||||
github.com/natefinch/lumberjack v2.0.0+incompatible // indirect
|
github.com/natefinch/lumberjack v2.0.0+incompatible // indirect
|
||||||
github.com/panjf2000/ants/v2 v2.11.3 // indirect
|
github.com/panjf2000/ants/v2 v2.11.3 // indirect
|
||||||
github.com/panjf2000/gnet/v2 v2.9.1 // indirect
|
github.com/panjf2000/gnet/v2 v2.9.7 // indirect
|
||||||
github.com/pelletier/go-toml/v2 v2.2.4 // indirect
|
github.com/pelletier/go-toml/v2 v2.2.4 // indirect
|
||||||
github.com/prometheus/client_model v0.6.1 // indirect
|
github.com/prometheus/client_model v0.6.1 // indirect
|
||||||
github.com/prometheus/common v0.62.0 // indirect
|
github.com/prometheus/common v0.62.0 // indirect
|
||||||
|
|||||||
@@ -116,6 +116,8 @@ github.com/panjf2000/ants/v2 v2.11.3 h1:AfI0ngBoXJmYOpDh9m516vjqoUu2sLrIVgppI9TZ
|
|||||||
github.com/panjf2000/ants/v2 v2.11.3/go.mod h1:8u92CYMUc6gyvTIw8Ru7Mt7+/ESnJahz5EVtqfrilek=
|
github.com/panjf2000/ants/v2 v2.11.3/go.mod h1:8u92CYMUc6gyvTIw8Ru7Mt7+/ESnJahz5EVtqfrilek=
|
||||||
github.com/panjf2000/gnet/v2 v2.9.1 h1:bKewICy/0xnQ9PMzNaswpe/Ah14w1TrRk91LHTcbIlA=
|
github.com/panjf2000/gnet/v2 v2.9.1 h1:bKewICy/0xnQ9PMzNaswpe/Ah14w1TrRk91LHTcbIlA=
|
||||||
github.com/panjf2000/gnet/v2 v2.9.1/go.mod h1:WQTxDWYuQ/hz3eccH0FN32IVuvZ19HewEWx0l62fx7E=
|
github.com/panjf2000/gnet/v2 v2.9.1/go.mod h1:WQTxDWYuQ/hz3eccH0FN32IVuvZ19HewEWx0l62fx7E=
|
||||||
|
github.com/panjf2000/gnet/v2 v2.9.7 h1:6zW7Jl3oAfXwSuh1PxHLndoL2MQRWx0AJR6aaQjxUgA=
|
||||||
|
github.com/panjf2000/gnet/v2 v2.9.7/go.mod h1:WQTxDWYuQ/hz3eccH0FN32IVuvZ19HewEWx0l62fx7E=
|
||||||
github.com/pelletier/go-toml/v2 v2.2.4 h1:mye9XuhQ6gvn5h28+VilKrrPoQVanw5PMw/TB0t5Ec4=
|
github.com/pelletier/go-toml/v2 v2.2.4 h1:mye9XuhQ6gvn5h28+VilKrrPoQVanw5PMw/TB0t5Ec4=
|
||||||
github.com/pelletier/go-toml/v2 v2.2.4/go.mod h1:2gIqNv+qfxSVS7cM2xJQKtLSTLUE9V8t9Stt+h56mCY=
|
github.com/pelletier/go-toml/v2 v2.2.4/go.mod h1:2gIqNv+qfxSVS7cM2xJQKtLSTLUE9V8t9Stt+h56mCY=
|
||||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||||
|
|||||||
@@ -90,6 +90,7 @@ func (c *Client) CloseClient() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// onClose 客户端关闭自动触发
|
||||||
func (c *Client) onClose() {
|
func (c *Client) onClose() {
|
||||||
if c.conn != nil {
|
if c.conn != nil {
|
||||||
_ = c.conn.Close()
|
_ = c.conn.Close()
|
||||||
|
|||||||
@@ -3,8 +3,9 @@ package ws_gateway
|
|||||||
import (
|
import (
|
||||||
"common/log"
|
"common/log"
|
||||||
"common/net/socket"
|
"common/net/socket"
|
||||||
|
"common/utils"
|
||||||
"fmt"
|
"fmt"
|
||||||
ws_handler2 "gateway/internal/handler/ws_handler"
|
"gateway/internal/handler/ws_handler"
|
||||||
"go.uber.org/zap"
|
"go.uber.org/zap"
|
||||||
"strconv"
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
@@ -19,40 +20,49 @@ func (g *GatewayWsServer) OnOpen(conn socket.ISocketConn) ([]byte, socket.Action
|
|||||||
return nil, socket.None
|
return nil, socket.None
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *GatewayWsServer) OnHandShake(conn socket.ISocketConn) {
|
func (g *GatewayWsServer) OnHandShake(conn socket.ISocketConn, bytes []byte, callback func(conn socket.ISocketConn, bytes []byte)) socket.Action {
|
||||||
token, ok := conn.GetParam("token").(string)
|
token, ok := conn.GetParam("token").(string)
|
||||||
if !ok || token == "" {
|
if !ok {
|
||||||
g.logger.Warnf("token is not string")
|
g.logger.Warnf("token is not string")
|
||||||
_ = conn.Close()
|
return socket.Close
|
||||||
return
|
|
||||||
}
|
}
|
||||||
t, err := strconv.Atoi(token)
|
//claims, err := utils.ParseToken(token, config.Get().Auth.Secret)
|
||||||
if err != nil {
|
//if err != nil {
|
||||||
_ = conn.Close()
|
// g.logger.Warnf("token is invalid")
|
||||||
|
// return socket.Close
|
||||||
|
//}
|
||||||
|
|
||||||
|
t, _ := strconv.Atoi(token)
|
||||||
|
claims := utils.Claims{
|
||||||
|
USN: int64(t),
|
||||||
}
|
}
|
||||||
if oldClient := ws_handler2.UserMgr.GetByUSN(int64(t)); oldClient != nil {
|
go func(shResp []byte) {
|
||||||
oldClient.CloseClient()
|
if oldClient := ws_handler.UserMgr.GetByUSN(claims.USN); oldClient != nil {
|
||||||
}
|
oldClient.CloseClient()
|
||||||
client := ws_handler2.NewClient(int64(t), conn)
|
}
|
||||||
ws_handler2.UserMgr.Add(int64(t), client)
|
client := ws_handler.NewClient(claims.USN, conn)
|
||||||
conn.SetParam("client", client)
|
ws_handler.UserMgr.Add(claims.USN, client)
|
||||||
|
conn.SetParam("client", client)
|
||||||
|
callback(conn, shResp)
|
||||||
|
}(bytes)
|
||||||
|
return socket.None
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *GatewayWsServer) OnMessage(conn socket.ISocketConn, bytes []byte) socket.Action {
|
func (g *GatewayWsServer) OnMessage(conn socket.ISocketConn, bytes []byte) socket.Action {
|
||||||
client, ok := conn.GetParam("client").(*ws_handler2.Client)
|
client, ok := conn.GetParam("client").(*ws_handler.Client)
|
||||||
if !ok || client.USN == 0 {
|
if !ok || client.USN == 0 {
|
||||||
return socket.Close
|
return socket.Close
|
||||||
}
|
}
|
||||||
client.OnEvent(&ws_handler2.ClientEvent{Msg: bytes})
|
client.OnEvent(&ws_handler.ClientEvent{Msg: bytes})
|
||||||
return socket.None
|
return socket.None
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *GatewayWsServer) OnPong(conn socket.ISocketConn) {
|
func (g *GatewayWsServer) OnPong(conn socket.ISocketConn) {
|
||||||
client, ok := conn.GetParam("client").(*ws_handler2.Client)
|
client, ok := conn.GetParam("client").(*ws_handler.Client)
|
||||||
if !ok || client.USN == 0 {
|
if !ok || client.USN == 0 {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
client.OnEvent(&ws_handler2.PongEvent{})
|
client.OnEvent(&ws_handler.PongEvent{})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *GatewayWsServer) OnClose(_ socket.ISocketConn, _ error) socket.Action {
|
func (g *GatewayWsServer) OnClose(_ socket.ISocketConn, _ error) socket.Action {
|
||||||
|
|||||||
@@ -20,6 +20,7 @@ type Module interface {
|
|||||||
func (p *Program) Init(_ svc.Environment) error {
|
func (p *Program) Init(_ svc.Environment) error {
|
||||||
p.moduleList = append(p.moduleList, &ModuleBase{})
|
p.moduleList = append(p.moduleList, &ModuleBase{})
|
||||||
p.moduleList = append(p.moduleList, &ModuleWebsocket{})
|
p.moduleList = append(p.moduleList, &ModuleWebsocket{})
|
||||||
|
p.moduleList = append(p.moduleList, &ModulePprof{})
|
||||||
|
|
||||||
for _, module := range p.moduleList {
|
for _, module := range p.moduleList {
|
||||||
if err := module.init(); err != nil {
|
if err := module.init(); err != nil {
|
||||||
|
|||||||
44
Server/robot/app/pprof.go
Normal file
44
Server/robot/app/pprof.go
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
package app
|
||||||
|
|
||||||
|
import (
|
||||||
|
"common/log"
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"net/http"
|
||||||
|
_ "net/http/pprof"
|
||||||
|
"sync"
|
||||||
|
)
|
||||||
|
|
||||||
|
// ModulePprof pprof模块
|
||||||
|
type ModulePprof struct {
|
||||||
|
wg *sync.WaitGroup
|
||||||
|
server *http.Server
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *ModulePprof) init() error {
|
||||||
|
m.wg = &sync.WaitGroup{}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *ModulePprof) start() error {
|
||||||
|
m.wg.Add(1)
|
||||||
|
go func() {
|
||||||
|
defer m.wg.Done()
|
||||||
|
m.server = &http.Server{
|
||||||
|
Addr: "localhost:6060",
|
||||||
|
}
|
||||||
|
if err := m.server.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
|
||||||
|
log.Errorf("pprof server failed: %v", err.Error())
|
||||||
|
}
|
||||||
|
log.Infof("pprof server stop.")
|
||||||
|
}()
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *ModulePprof) stop() error {
|
||||||
|
if err := m.server.Shutdown(context.Background()); err != nil {
|
||||||
|
log.Errorf("stop pprof server failed: %v", err)
|
||||||
|
}
|
||||||
|
m.wg.Wait()
|
||||||
|
return nil
|
||||||
|
}
|
||||||
@@ -9,9 +9,9 @@ log:
|
|||||||
maxAge: 7
|
maxAge: 7
|
||||||
|
|
||||||
client:
|
client:
|
||||||
count: 100
|
count: 1100
|
||||||
usn: [ 1,1000000 ]
|
usn: [ 1,1000000 ]
|
||||||
websocket:
|
websocket:
|
||||||
address: "ws://47.108.184.184/ws/"
|
# address: "ws://47.108.184.184/ws/"
|
||||||
# address: "ws://127.0.0.1"
|
address: "ws://127.0.0.1"
|
||||||
port: 0
|
port: 8501
|
||||||
@@ -6,6 +6,7 @@ import (
|
|||||||
"google.golang.org/protobuf/proto"
|
"google.golang.org/protobuf/proto"
|
||||||
"math"
|
"math"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -49,14 +50,21 @@ func (_ *EnterInstance) Handle(data []byte, client *Client) {
|
|||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var s2cPositionPool = sync.Pool{
|
||||||
|
New: func() interface{} {
|
||||||
|
return &sc_pb.Message{}
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
type Position struct {
|
type Position struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (_ *Position) Handle(data []byte, client *Client) {
|
func (_ *Position) Handle(data []byte, client *Client) {
|
||||||
msg := &sc_pb.S2C_Position{}
|
//msg := s2cPositionPool.Get().(*sc_pb.Message)
|
||||||
if err := proto.Unmarshal(data, msg); err != nil {
|
//if err := proto.Unmarshal(data, msg); err != nil {
|
||||||
log.Errorf("handle msg error")
|
// log.Errorf("handle msg error")
|
||||||
client.Stop()
|
// client.Stop()
|
||||||
return
|
// return
|
||||||
}
|
//}
|
||||||
|
//s2cPositionPool.Put(msg)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -27,7 +27,7 @@ require (
|
|||||||
github.com/jinzhu/inflection v1.0.0 // indirect
|
github.com/jinzhu/inflection v1.0.0 // indirect
|
||||||
github.com/jinzhu/now v1.1.5 // indirect
|
github.com/jinzhu/now v1.1.5 // indirect
|
||||||
github.com/natefinch/lumberjack v2.0.0+incompatible // indirect
|
github.com/natefinch/lumberjack v2.0.0+incompatible // indirect
|
||||||
github.com/panjf2000/gnet/v2 v2.9.1 // indirect
|
github.com/panjf2000/gnet/v2 v2.9.7 // indirect
|
||||||
github.com/pelletier/go-toml/v2 v2.2.4 // indirect
|
github.com/pelletier/go-toml/v2 v2.2.4 // indirect
|
||||||
github.com/redis/go-redis/v9 v9.10.0 // indirect
|
github.com/redis/go-redis/v9 v9.10.0 // indirect
|
||||||
github.com/sagikazarmark/locafero v0.11.0 // indirect
|
github.com/sagikazarmark/locafero v0.11.0 // indirect
|
||||||
|
|||||||
@@ -59,6 +59,8 @@ github.com/natefinch/lumberjack v2.0.0+incompatible h1:4QJd3OLAMgj7ph+yZTuX13Ld4
|
|||||||
github.com/natefinch/lumberjack v2.0.0+incompatible/go.mod h1:Wi9p2TTF5DG5oU+6YfsmYQpsTIOm0B1VNzQg9Mw6nPk=
|
github.com/natefinch/lumberjack v2.0.0+incompatible/go.mod h1:Wi9p2TTF5DG5oU+6YfsmYQpsTIOm0B1VNzQg9Mw6nPk=
|
||||||
github.com/panjf2000/gnet/v2 v2.9.1 h1:bKewICy/0xnQ9PMzNaswpe/Ah14w1TrRk91LHTcbIlA=
|
github.com/panjf2000/gnet/v2 v2.9.1 h1:bKewICy/0xnQ9PMzNaswpe/Ah14w1TrRk91LHTcbIlA=
|
||||||
github.com/panjf2000/gnet/v2 v2.9.1/go.mod h1:WQTxDWYuQ/hz3eccH0FN32IVuvZ19HewEWx0l62fx7E=
|
github.com/panjf2000/gnet/v2 v2.9.1/go.mod h1:WQTxDWYuQ/hz3eccH0FN32IVuvZ19HewEWx0l62fx7E=
|
||||||
|
github.com/panjf2000/gnet/v2 v2.9.7 h1:6zW7Jl3oAfXwSuh1PxHLndoL2MQRWx0AJR6aaQjxUgA=
|
||||||
|
github.com/panjf2000/gnet/v2 v2.9.7/go.mod h1:WQTxDWYuQ/hz3eccH0FN32IVuvZ19HewEWx0l62fx7E=
|
||||||
github.com/pelletier/go-toml/v2 v2.2.4 h1:mye9XuhQ6gvn5h28+VilKrrPoQVanw5PMw/TB0t5Ec4=
|
github.com/pelletier/go-toml/v2 v2.2.4 h1:mye9XuhQ6gvn5h28+VilKrrPoQVanw5PMw/TB0t5Ec4=
|
||||||
github.com/pelletier/go-toml/v2 v2.2.4/go.mod h1:2gIqNv+qfxSVS7cM2xJQKtLSTLUE9V8t9Stt+h56mCY=
|
github.com/pelletier/go-toml/v2 v2.2.4/go.mod h1:2gIqNv+qfxSVS7cM2xJQKtLSTLUE9V8t9Stt+h56mCY=
|
||||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||||
|
|||||||
@@ -47,7 +47,7 @@ require (
|
|||||||
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 // indirect
|
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 // indirect
|
||||||
github.com/modern-go/reflect2 v1.0.2 // indirect
|
github.com/modern-go/reflect2 v1.0.2 // indirect
|
||||||
github.com/natefinch/lumberjack v2.0.0+incompatible // indirect
|
github.com/natefinch/lumberjack v2.0.0+incompatible // indirect
|
||||||
github.com/panjf2000/gnet/v2 v2.9.1 // indirect
|
github.com/panjf2000/gnet/v2 v2.9.7 // indirect
|
||||||
github.com/pelletier/go-toml/v2 v2.2.4 // indirect
|
github.com/pelletier/go-toml/v2 v2.2.4 // indirect
|
||||||
github.com/quic-go/qpack v0.5.1 // indirect
|
github.com/quic-go/qpack v0.5.1 // indirect
|
||||||
github.com/quic-go/quic-go v0.54.0 // indirect
|
github.com/quic-go/quic-go v0.54.0 // indirect
|
||||||
|
|||||||
@@ -113,6 +113,7 @@ github.com/natefinch/lumberjack v2.0.0+incompatible h1:4QJd3OLAMgj7ph+yZTuX13Ld4
|
|||||||
github.com/natefinch/lumberjack v2.0.0+incompatible/go.mod h1:Wi9p2TTF5DG5oU+6YfsmYQpsTIOm0B1VNzQg9Mw6nPk=
|
github.com/natefinch/lumberjack v2.0.0+incompatible/go.mod h1:Wi9p2TTF5DG5oU+6YfsmYQpsTIOm0B1VNzQg9Mw6nPk=
|
||||||
github.com/panjf2000/gnet/v2 v2.9.1 h1:bKewICy/0xnQ9PMzNaswpe/Ah14w1TrRk91LHTcbIlA=
|
github.com/panjf2000/gnet/v2 v2.9.1 h1:bKewICy/0xnQ9PMzNaswpe/Ah14w1TrRk91LHTcbIlA=
|
||||||
github.com/panjf2000/gnet/v2 v2.9.1/go.mod h1:WQTxDWYuQ/hz3eccH0FN32IVuvZ19HewEWx0l62fx7E=
|
github.com/panjf2000/gnet/v2 v2.9.1/go.mod h1:WQTxDWYuQ/hz3eccH0FN32IVuvZ19HewEWx0l62fx7E=
|
||||||
|
github.com/panjf2000/gnet/v2 v2.9.7/go.mod h1:WQTxDWYuQ/hz3eccH0FN32IVuvZ19HewEWx0l62fx7E=
|
||||||
github.com/pelletier/go-toml/v2 v2.2.4 h1:mye9XuhQ6gvn5h28+VilKrrPoQVanw5PMw/TB0t5Ec4=
|
github.com/pelletier/go-toml/v2 v2.2.4 h1:mye9XuhQ6gvn5h28+VilKrrPoQVanw5PMw/TB0t5Ec4=
|
||||||
github.com/pelletier/go-toml/v2 v2.2.4/go.mod h1:2gIqNv+qfxSVS7cM2xJQKtLSTLUE9V8t9Stt+h56mCY=
|
github.com/pelletier/go-toml/v2 v2.2.4/go.mod h1:2gIqNv+qfxSVS7cM2xJQKtLSTLUE9V8t9Stt+h56mCY=
|
||||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||||
|
|||||||
Reference in New Issue
Block a user