Code-Fight commented on code in PR #769:
URL: 
https://github.com/apache/incubator-seata-go/pull/769#discussion_r1959837507


##########
pkg/saga/statemachine/engine/invoker/http_invoker.go:
##########
@@ -0,0 +1,220 @@
+/*
+ * 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 (
+       "bytes"
+       "context"
+       "encoding/json"
+       "errors"
+       "fmt"
+       "io"
+       "net/http"
+       "reflect"
+       "strings"
+       "sync"
+       "time"
+
+       "github.com/seata/seata-go/pkg/saga/statemachine/statelang/state"
+       "github.com/seata/seata-go/pkg/util/log"
+)
+
+type HTTPInvoker struct {
+       clientsMapLock sync.Mutex
+       clients        map[string]HTTPClient
+}
+
+func NewHTTPInvoker() *HTTPInvoker {
+       return &HTTPInvoker{
+               clients: make(map[string]HTTPClient),
+       }
+}
+
+func (h *HTTPInvoker) RegisterClient(serviceName string, client HTTPClient) {
+       h.clientsMapLock.Lock()
+       defer h.clientsMapLock.Unlock()
+       h.clients[serviceName] = client
+}
+
+func (h *HTTPInvoker) GetClient(serviceName string) HTTPClient {
+       h.clientsMapLock.Lock()
+       defer h.clientsMapLock.Unlock()
+       return h.clients[serviceName]
+}
+
+func (h *HTTPInvoker) Invoke(ctx context.Context, input []any, service 
state.ServiceTaskState) (output []reflect.Value, err error) {
+       serviceTaskStateImpl := service.(*state.ServiceTaskStateImpl)
+       client := h.GetClient(serviceTaskStateImpl.ServiceName())
+       if client == nil {
+               return nil, fmt.Errorf("no http client %s for service task 
state", serviceTaskStateImpl.ServiceName())
+       }
+
+       if serviceTaskStateImpl.IsAsync() {
+               go func() {
+                       _, err := client.Call(ctx, serviceTaskStateImpl, input)
+                       if err != nil {
+                               log.Errorf("invoke Service[%s].%s failed, err 
is %s", serviceTaskStateImpl.ServiceName(),
+                                       serviceTaskStateImpl.ServiceMethod(), 
err)
+                       }
+               }()
+               return nil, nil
+       }
+
+       return client.Call(ctx, serviceTaskStateImpl, input)
+}
+
+func (h *HTTPInvoker) Close(ctx context.Context) error {
+       return nil
+}
+
+type HTTPClient interface {
+       Call(ctx context.Context, serviceTaskStateImpl 
*state.ServiceTaskStateImpl, input []any) ([]reflect.Value, error)
+}
+
+type HTTPClientImpl struct {
+       serviceName string
+       baseURL     string
+       client      *http.Client
+}
+
+func NewHTTPClient(serviceName string, baseURL string, client *http.Client) 
*HTTPClientImpl {
+       if client == nil {
+               client = &http.Client{
+                       Timeout: time.Second * 30,
+               }
+       }
+       return &HTTPClientImpl{
+               serviceName: serviceName,
+               baseURL:     baseURL,
+               client:      client,
+       }
+}
+
+func (h *HTTPClientImpl) Call(ctx context.Context, serviceTaskStateImpl 
*state.ServiceTaskStateImpl, input []any) ([]reflect.Value, error) {
+       retryCountMap := make(map[state.Retry]int)
+       for {
+               res, err, shouldRetry := func() (res []reflect.Value, resErr 
error, shouldRetry bool) {
+                       defer func() {
+                               if r := recover(); r != nil {
+                                       errStr := fmt.Sprintf("%v", r)
+                                       retry := 
h.matchRetry(serviceTaskStateImpl, errStr)
+                                       resErr = errors.New(errStr)
+                                       if retry != nil {
+                                               shouldRetry = 
h.needRetry(serviceTaskStateImpl, retryCountMap, retry, resErr)
+                                       }
+                               }
+                       }()
+
+                       // 构造HTTP请求
+                       reqBody, err := json.Marshal(input)
+                       if err != nil {
+                               return nil, err, false
+                       }
+
+                       req, err := http.NewRequestWithContext(ctx,
+                               serviceTaskStateImpl.ServiceMethod(),
+                               h.baseURL+serviceTaskStateImpl.Name(),
+                               bytes.NewBuffer(reqBody))
+                       if err != nil {
+                               return nil, err, false
+                       }
+
+                       req.Header.Set("Content-Type", "application/json")
+
+                       resp, err := h.client.Do(req)
+                       if err != nil {
+                               retry := h.matchRetry(serviceTaskStateImpl, 
err.Error())
+                               if retry != nil {
+                                       return nil, err, 
h.needRetry(serviceTaskStateImpl, retryCountMap, retry, err)
+                               }
+                               return nil, err, false
+                       }
+                       defer resp.Body.Close()
+
+                       body, err := io.ReadAll(resp.Body)
+                       if err != nil {
+                               return nil, err, false
+                       }
+
+                       if resp.StatusCode >= 400 {
+                               errStr := fmt.Sprintf("HTTP error: %d - %s", 
resp.StatusCode, string(body))
+                               retry := h.matchRetry(serviceTaskStateImpl, 
errStr)
+                               if retry != nil {
+                                       return nil, errors.New(errStr), 
h.needRetry(serviceTaskStateImpl, retryCountMap, retry, err)
+                               }
+                               return nil, errors.New(errStr), false
+                       }
+
+                       // 返回响应结果

Review Comment:
   done



-- 
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