mirror of
https://github.com/FlourishingWorld/hk4e.git
synced 2026-02-04 14:22:26 +08:00
perf与性能优化
This commit is contained in:
@@ -35,10 +35,10 @@ type MessageQueue struct {
|
||||
appId string
|
||||
gateTcpMqEventChan chan *GateTcpMqEvent
|
||||
gateTcpMqDeadEventChan chan string
|
||||
rpcClient *rpc.Client
|
||||
discoveryClient *rpc.DiscoveryClient
|
||||
}
|
||||
|
||||
func NewMessageQueue(serverType string, appId string, rpcClient *rpc.Client) (r *MessageQueue) {
|
||||
func NewMessageQueue(serverType string, appId string, discoveryClient *rpc.DiscoveryClient) (r *MessageQueue) {
|
||||
r = new(MessageQueue)
|
||||
conn, err := nats.Connect(config.GetConfig().MQ.NatsUrl)
|
||||
if err != nil {
|
||||
@@ -64,7 +64,7 @@ func NewMessageQueue(serverType string, appId string, rpcClient *rpc.Client) (r
|
||||
r.appId = appId
|
||||
r.gateTcpMqEventChan = make(chan *GateTcpMqEvent, 1000)
|
||||
r.gateTcpMqDeadEventChan = make(chan string, 1000)
|
||||
r.rpcClient = rpcClient
|
||||
r.discoveryClient = discoveryClient
|
||||
if serverType == api.GATE {
|
||||
go r.runGateTcpMqServer()
|
||||
} else if serverType == api.GS || serverType == api.ANTICHEAT || serverType == api.PATHFINDING {
|
||||
@@ -355,7 +355,7 @@ func (m *MessageQueue) runGateTcpMqClient() {
|
||||
}
|
||||
|
||||
func (m *MessageQueue) gateTcpMqConn(gateServerConnAddrMap map[string]bool) {
|
||||
rsp, err := m.rpcClient.Discovery.GetAllGateServerInfoList(context.TODO(), new(api.NullMsg))
|
||||
rsp, err := m.discoveryClient.GetAllGateServerInfoList(context.TODO(), new(api.NullMsg))
|
||||
if err != nil {
|
||||
logger.Error("gate tcp mq get gate list error: %v", err)
|
||||
return
|
||||
|
||||
@@ -5,48 +5,30 @@ import (
|
||||
gsapi "hk4e/gs/api"
|
||||
nodeapi "hk4e/node/api"
|
||||
|
||||
"github.com/byebyebruce/natsrpc"
|
||||
"github.com/nats-io/nats.go"
|
||||
"github.com/nats-io/nats.go/encoders/protobuf"
|
||||
)
|
||||
|
||||
// Client natsrpc客户端
|
||||
type Client struct {
|
||||
conn *nats.Conn
|
||||
Discovery *DiscoveryClient
|
||||
GM *GMClient
|
||||
}
|
||||
|
||||
// NewClient 构造
|
||||
func NewClient() (*Client, error) {
|
||||
r := new(Client)
|
||||
conn, err := nats.Connect(config.GetConfig().MQ.NatsUrl)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
r.conn = conn
|
||||
discoveryClient, err := newDiscoveryClient(conn)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
r.Discovery = discoveryClient
|
||||
gmClient, err := newGmClient(conn)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
r.GM = gmClient
|
||||
return r, nil
|
||||
}
|
||||
|
||||
// Close 销毁
|
||||
func (c *Client) Close() {
|
||||
c.conn.Close()
|
||||
}
|
||||
// natsrpc客户端
|
||||
|
||||
// DiscoveryClient node的discovery服务
|
||||
type DiscoveryClient struct {
|
||||
nodeapi.DiscoveryNATSRPCClient
|
||||
}
|
||||
|
||||
func NewDiscoveryClient() (*DiscoveryClient, error) {
|
||||
conn, err := nats.Connect(config.GetConfig().MQ.NatsUrl)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
discoveryClient, err := newDiscoveryClient(conn)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return discoveryClient, nil
|
||||
}
|
||||
|
||||
func newDiscoveryClient(conn *nats.Conn) (*DiscoveryClient, error) {
|
||||
enc, err := nats.NewEncodedConn(conn, protobuf.PROTOBUF_ENCODER)
|
||||
if err != nil {
|
||||
@@ -66,12 +48,24 @@ type GMClient struct {
|
||||
gsapi.GMNATSRPCClient
|
||||
}
|
||||
|
||||
func newGmClient(conn *nats.Conn) (*GMClient, error) {
|
||||
func NewGMClient(gsId uint32) (*GMClient, error) {
|
||||
conn, err := nats.Connect(config.GetConfig().MQ.NatsUrl)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
gmClient, err := newGmClient(conn, gsId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return gmClient, nil
|
||||
}
|
||||
|
||||
func newGmClient(conn *nats.Conn, gsId uint32) (*GMClient, error) {
|
||||
enc, err := nats.NewEncodedConn(conn, protobuf.PROTOBUF_ENCODER)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
cli, err := gsapi.NewGMNATSRPCClient(enc)
|
||||
cli, err := gsapi.NewGMNATSRPCClient(enc, natsrpc.WithClientID(gsId))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user