Repository: cloudwego/kitex
File: pkg/remote/trans/nphttp2/server_handler.go (Go)

Code before:
			idx := strings.LastIndex(sm[:pos], ".")
			if idx == -1 {
				ink.SetPackageName("")
				serviceName = sm[0:pos]
			} else {
				ink.SetPackageName(sm[:idx])
				serviceName = sm[idx+1 : pos]
			}
			ink.SetServiceName(serviceName)

			rCtx = remote.SetRecvCompressor(rCtx, s.RecvCompress())
			rCtx = remote.SetSendCompressor(rCtx, s.SendCompress())

			svc := t.svcMap[serviceName]
			unknownServiceHandlerFunc := t.opt.GRPCUnknownServiceHandler
			var st streaming.Stream
			if svc == nil {
				if unknownServiceHandlerFunc != nil {
					st, rCtx = t.bindStreamIntoCtx(nil, rCtx, tr, s)
					err = execUnknownServiceHandleFunc(rCtx, ri, methodName, st, unknownServiceHandlerFunc)
				} else {
					err = remote.NewTransErrorWithMsg(remote.UnknownService, fmt.Sprintf("unknown service %s", serviceName))
				}
			} else {
				svcInfo := svc.GetServiceInfo()
				st, rCtx = t.bindStreamIntoCtx(svc.GetServiceInfo(), rCtx, tr, s)

				if svcInfo.MethodInfo(methodName) == nil {
					if unknownServiceHandlerFunc != nil {
						err = execUnknownServiceHandleFunc(rCtx, ri, methodName, st, unknownServiceHandlerFunc)
					} else {
						err = remote.NewTransErrorWithMsg(remote.UnknownMethod, fmt.Sprintf("unknown method %s", methodName))
					}
				} else {
					err = t.inkHdlFunc(rCtx, &streaming.Args{Stream: st}, nil)
				}
			}

			if err != nil {
				tr.WriteStatus(s, convertStatus(err))
				t.OnError(rCtx, err, conn)
				return
			}
			if bizStatusErr := ri.Invocation().BizStatusErr(); bizStatusErr != nil {
				var st *status.Status
				if sterr, ok := bizStatusErr.(status.Iface); ok {
					st = sterr.GRPCStatus()
				} else {
					st = status.New(codes.Internal, bizStatusErr.BizMessage())
				}
				s.SetBizStatusErr(bizStatusErr)

Review comment (suggestion):
Maybe the codes below is more simple?
``` golang
			svc := t.svcMap[serviceName]
			
			var svcInfo *serviceinfo.ServiceInfo
			if svc != nil {
				svcInfo = svc.GetServiceInfo()
			}

			st := NewStream(rCtx, svcInfo, newServerConn(tr, s), t)
			streamArg := &streaming.Args{Stream: st}

			// bind stream into ctx, in order to let user set header and trailer by provided api in meta_api.go
			rCtx = context.WithValue(rCtx, streamKey{}, st)

			if svc == nil || svcInfo.MethodInfo(methdName) == nil {
				unknownServiceHandlerFunc := t.opt.GRPCUnknownServiceHandler
				if unknownServiceHandlerFunc != nil {
					rpcinfo.Record(rCtx, ri, stats.ServerHandleStart, nil)
					err = unknownServiceHandlerFunc(rCtx, methodName, st)
					if err != nil {
						err = kerrors.ErrBiz.WithCause(err)
					}
				} else {
					if svc == nil {
						err = remote.NewTransErrorWithMsg(remote.UnknownService, fmt.Sprintf("unknown service %s", serviceName))
					} else {
						err = remote.NewTransErrorWithMsg(remote.UnknownMethod, fmt.Sprintf("unknown method %s", methodName))
					}
				}
			} else {
				err = t.inkHdlFunc(rCtx, streamArg, nil)
			}
```

Code after:
			idx := strings.LastIndex(sm[:pos], ".")
			if idx == -1 {
				ink.SetPackageName("")
				serviceName = sm[0:pos]
			} else {
				ink.SetPackageName(sm[:idx])
				serviceName = sm[idx+1 : pos]
			}
			ink.SetServiceName(serviceName)

			// set recv grpc compressor at server to decode the pack from client
			remote.SetRecvCompressor(ri, s.RecvCompress())
			// set send grpc compressor at server to encode reply pack
			remote.SetSendCompressor(ri, s.SendCompress())

			svcInfo := t.svcMap[serviceName]

			st := NewStream(rCtx, svcInfo, newServerConn(tr, s), t)
			// bind stream into ctx, in order to let user set header and trailer by provided api in meta_api.go
			rCtx = context.WithValue(rCtx, streamKey{}, st)

			if svcInfo == nil || svcInfo.MethodInfo(methodName) == nil {
				unknownServiceHandlerFunc := t.opt.GRPCUnknownServiceHandler
				if unknownServiceHandlerFunc != nil {
					rpcinfo.Record(rCtx, ri, stats.ServerHandleStart, nil)
					err = unknownServiceHandlerFunc(rCtx, methodName, st)
					if err != nil {
						err = kerrors.ErrBiz.WithCause(err)
					}
				} else {
					if svcInfo == nil {
						err = remote.NewTransErrorWithMsg(remote.UnknownService, fmt.Sprintf("unknown service %s", serviceName))
					} else {
						err = remote.NewTransErrorWithMsg(remote.UnknownMethod, fmt.Sprintf("unknown method %s", methodName))
					}
				}
			} else {
				err = t.inkHdlFunc(rCtx, &streaming.Args{Stream: st}, nil)
			}

			if err != nil {
				tr.WriteStatus(s, convertStatus(err))
				t.OnError(rCtx, err, conn)
				return
			}
			if bizStatusErr := ri.Invocation().BizStatusErr(); bizStatusErr != nil {
				var st *status.Status
				if sterr, ok := bizStatusErr.(status.Iface); ok {
					st = sterr.GRPCStatus()
				} else {
					st = status.New(codes.Internal, bizStatusErr.BizMessage())