This commit is contained in:
liuxiaobo 2025-06-02 21:37:23 +08:00
parent 495530a575
commit b07cff2f5d
3 changed files with 19 additions and 11 deletions

View File

@ -1,5 +1,7 @@
package ipb package ipb
import "encoding/json"
func MakeMsg(serviceName string, connId uint32, userId int64, msgId int32, msg []byte) *InternalMsg { func MakeMsg(serviceName string, connId uint32, userId int64, msgId int32, msg []byte) *InternalMsg {
return &InternalMsg{ return &InternalMsg{
ServiceName: serviceName, ServiceName: serviceName,
@ -10,14 +12,15 @@ func MakeMsg(serviceName string, connId uint32, userId int64, msgId int32, msg [
} }
} }
//func MakeRpcMsg(serviceName string, connId uint32, userId int64, msgId int32, msg []byte) *InternalMsg { func MakeRpcMsg[T any](rpcMsgId string, userId int64, msg *T) *InternalMsg {
// return &InternalMsg{ data, _ := json.Marshal(msg)
// ServiceName: serviceName, return &InternalMsg{
// ConnId: connId, ServiceName: "",
// UserId: userId, ConnId: 0,
// MsgId: msgId, UserId: userId,
// Msg: msg, MsgId: 0,
// Type: MsgType_RpcMsg, Msg: data,
// RetRpcMsgId: genRpcId(), Type: MsgType_RpcMsg,
// } RpcMsgId: rpcMsgId,
//} }
}

View File

@ -13,4 +13,6 @@ func TestWipeWarn(t *testing.T) {
password := fmt.Sprintf("%x", md5.Sum([]byte(username))) password := fmt.Sprintf("%x", md5.Sum([]byte(username)))
t.Log(password) t.Log(password)
s := "hello world"
t.Log(MakeRpcMsg[string]("test.rpc", 0, &s))
} }

View File

@ -144,6 +144,9 @@ func (s *NatsService) Send(topic string, msg *ipb.InternalMsg) error {
return s.nats.Publish(topic, data) return s.nats.Publish(topic, data)
} }
// call会阻塞主协程需要起新协程去等待返回值业务逻辑则转回主协程处理。
// 不要将处理返回值的业务逻辑放到新协程,避免业务逻辑中有数据并发问题
// 比如并发call拉用户数据然后操作本地的user map
func (s *NatsService) Call(rpcTopic string, timeout time.Duration, msg *ipb.InternalMsg) (*ipb.InternalMsg, error) { func (s *NatsService) Call(rpcTopic string, timeout time.Duration, msg *ipb.InternalMsg) (*ipb.InternalMsg, error) {
data, _ := proto.Marshal(msg) data, _ := proto.Marshal(msg)
response, err := s.nats.Rpc(rpcTopic, timeout, data) response, err := s.nats.Rpc(rpcTopic, timeout, data)