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
import "encoding/json"
func MakeMsg(serviceName string, connId uint32, userId int64, msgId int32, msg []byte) *InternalMsg {
return &InternalMsg{
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 {
// return &InternalMsg{
// ServiceName: serviceName,
// ConnId: connId,
// UserId: userId,
// MsgId: msgId,
// Msg: msg,
// Type: MsgType_RpcMsg,
// RetRpcMsgId: genRpcId(),
// }
//}
func MakeRpcMsg[T any](rpcMsgId string, userId int64, msg *T) *InternalMsg {
data, _ := json.Marshal(msg)
return &InternalMsg{
ServiceName: "",
ConnId: 0,
UserId: userId,
MsgId: 0,
Msg: data,
Type: MsgType_RpcMsg,
RpcMsgId: rpcMsgId,
}
}

View File

@ -13,4 +13,6 @@ func TestWipeWarn(t *testing.T) {
password := fmt.Sprintf("%x", md5.Sum([]byte(username)))
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)
}
// call会阻塞主协程需要起新协程去等待返回值业务逻辑则转回主协程处理。
// 不要将处理返回值的业务逻辑放到新协程,避免业务逻辑中有数据并发问题
// 比如并发call拉用户数据然后操作本地的user map
func (s *NatsService) Call(rpcTopic string, timeout time.Duration, msg *ipb.InternalMsg) (*ipb.InternalMsg, error) {
data, _ := proto.Marshal(msg)
response, err := s.nats.Rpc(rpcTopic, timeout, data)