flypiggyyoyoyo commented on code in PR #868:
URL: 
https://github.com/apache/incubator-seata-go/pull/868#discussion_r2263543314


##########
pkg/saga/statemachine/engine/invoker/local_invoker.go:
##########
@@ -0,0 +1,178 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package invoker
+
+import (
+       "context"
+       "fmt"
+       "github.com/seata/seata-go/pkg/saga/statemachine/statelang/state"
+       "reflect"
+       "sync"
+)
+
+type LocalServiceInvoker struct {
+       serviceRegistry map[string]interface{}
+       methodCache     map[string]*reflect.Method
+       jsonParser      JsonParser
+       mutex           sync.RWMutex
+}
+
+func NewLocalServiceInvoker() *LocalServiceInvoker {
+       return &LocalServiceInvoker{
+               serviceRegistry: make(map[string]interface{}),
+               methodCache:     make(map[string]*reflect.Method),
+               jsonParser:      &DefaultJsonParser{},
+       }
+}
+
+func (l *LocalServiceInvoker) RegisterService(serviceName string, instance 
interface{}) {
+       l.mutex.Lock()
+       defer l.mutex.Unlock()
+       l.serviceRegistry[serviceName] = instance
+}
+
+func (l *LocalServiceInvoker) Invoke(ctx context.Context, input []any, service 
state.ServiceTaskState) ([]reflect.Value, error) {
+       serviceName := service.ServiceName()
+       instance, exists := l.serviceRegistry[serviceName]
+       if !exists {
+               return nil, fmt.Errorf("service %s not registered", serviceName)
+       }
+
+       methodName := service.ServiceMethod()
+       method, err := l.getMethod(serviceName, methodName, 
service.ParameterTypes())
+       if err != nil {
+               return nil, err
+       }
+
+       params, err := l.resolveParameters(input, method.Type)
+       if err != nil {
+               return nil, err
+       }
+
+       return l.invokeMethod(instance, method, params), nil
+}
+
+func (l *LocalServiceInvoker) resolveMethod(key, serviceName, methodName 
string) (*reflect.Method, error) {
+       l.mutex.Lock()
+       defer l.mutex.Unlock()
+
+       if cachedMethod, ok := l.methodCache[key]; ok {
+               return cachedMethod, nil
+       }
+
+       instance, exists := l.serviceRegistry[serviceName]
+       if !exists {
+               return nil, fmt.Errorf("service %s not found", serviceName)
+       }
+
+       objType := reflect.TypeOf(instance)
+       method, ok := objType.MethodByName(methodName)
+       if !ok {
+               return nil, fmt.Errorf("method %s not found in service %s", 
methodName, serviceName)
+       }
+
+       l.methodCache[key] = &method
+       return &method, nil
+}
+
+func (l *LocalServiceInvoker) getMethod(serviceName, methodName string, 
paramTypes []string) (*reflect.Method, error) {
+       key := fmt.Sprintf("%s.%s", serviceName, methodName)
+
+       l.mutex.RLock()
+       if method, ok := l.methodCache[key]; ok {
+               l.mutex.RUnlock()
+               return method, nil
+       }
+       l.mutex.RUnlock()
+
+       return l.resolveMethod(key, serviceName, methodName)
+}
+
+func (l *LocalServiceInvoker) resolveParameters(input []any, methodType 
reflect.Type) ([]reflect.Value, error) {
+       paramCount := methodType.NumIn() - 1
+       params := make([]reflect.Value, paramCount)
+
+       for i := 0; i < paramCount; i++ {
+               methodParamIndex := i + 1

Review Comment:
   In the current implementation, the use of Type.MethodByName ensures that the 
obtained method is definitely an instance method, which necessarily includes a 
receiver. However, this modification can serve as a robustness enhancement, and 
I will make the change and submit it.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscr...@seata.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscr...@seata.apache.org
For additional commands, e-mail: notifications-h...@seata.apache.org

Reply via email to