77 "math/rand"
88 "net"
99 "sort"
10- "strings"
1110 "sync"
1211 "time"
1312
@@ -303,36 +302,20 @@ func (h *LocalHandler) processPacket(agent *agent, p *packet.Packet) error {
303302
304303// processMessage 处理消息, 分发到本地或远程
305304func (h * LocalHandler ) processMessage (agent * agent , msg * message.Message ) {
306- var lastMid uint64
307- switch msg .Type {
308- case message .Request :
309- lastMid = msg .ID
310- case message .Notify :
311- lastMid = 0
312- default :
313- log .Info ("Invalid message type: " + msg .Type .String ())
305+ if handlerNode , found := h .localHandlers [msg .Route ]; found {
306+ h .localProcess (agent .session , msg , handlerNode )
314307 return
315308 }
316- handlerNode , found := h .localHandlers [msg .Route ]
317- if ! found {
318- h .remoteProcess (agent .session , msg , false )
319- } else {
320- h .localProcess (handlerNode , lastMid , agent .session , msg )
321- }
309+ h .remoteProcess (agent .session , msg , false )
322310}
323311
324312// remoteProcess 处理远程消息
325313func (h * LocalHandler ) remoteProcess (session * session.Session , msg * message.Message , noCopy bool ) {
326- index := strings .LastIndex (msg .Route , "." )
327- if index < 0 {
328- log .Info ("nano/handler: invalid route %s" , msg .Route )
329- return
330- }
331-
332- service := msg .Route [:index ]
314+ service := msg .Service ()
333315 members := h .findMembers (service )
334316 if len (members ) == 0 {
335- log .Info ("nano/handler: %s not found(forgot registered?)" , msg .Route )
317+ // 没有服务节点, 转本地处理 404
318+ h .localProcess (session , msg , nil )
336319 return
337320 }
338321
@@ -347,7 +330,11 @@ func (h *LocalHandler) remoteProcess(session *session.Session, msg *message.Mess
347330 } else {
348331 member := h .node .opts .RemoteServiceRoute (service , session , members )
349332 if member == nil {
350- log .Info ("customize remoteServiceRoute handler: %s is not found" , msg .Route )
333+ if env .Debug {
334+ log .Info ("customize remoteServiceRoute handler: %s is not found" , msg .Route )
335+ }
336+ // 有服务节点, 但自定义路由没匹配到, 转本地处理 404
337+ h .localProcess (session , msg , nil )
351338 return
352339 }
353340 remoteAddr = member .ServiceAddr
@@ -407,21 +394,27 @@ func (h *LocalHandler) remoteProcess(session *session.Session, msg *message.Mess
407394}
408395
409396// localProcess 本地处理
410- func (h * LocalHandler ) localProcess (handlerNode * npi.HandlerNode , lastMid uint64 , session * session.Session , msg * message.Message ) {
397+ func (h * LocalHandler ) localProcess (session * session.Session , msg * message.Message , handlerNode * npi.HandlerNode ) {
398+ // 计算响应 ID
399+ lastMid , err := msg .ResponseID ()
400+ if err != nil {
401+ log .Info (err .Error () + ":" + msg .Type .String ())
402+ return
403+ }
404+
405+ // 计算服务名
406+ service := msg .Service ()
407+
408+ // 执行管道任务
411409 if pipe := h .pipeline ; pipe != nil {
412- err : = pipe .Inbound ().Process (session , msg )
410+ err = pipe .Inbound ().Process (session , msg )
413411 if err != nil {
414412 log .Error ("Pipeline process failed." , err )
415413 return
416414 }
417415 }
418- index := strings .LastIndex (msg .Route , "." )
419- if index < 0 {
420- log .Info ("nano/handler: invalid route %s" , msg .Route )
421- return
422- }
423- service := msg .Route [:index ]
424416
417+ // 记录日志
425418 if env .Debug {
426419 log .Info ("UID=%d, Message={%s}, Data=%v" , session .UID (), msg .String (), msg .Data )
427420 }
0 commit comments