This is an automated email from the ASF dual-hosted git repository.

abeizn pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/incubator-devlake.git


The following commit(s) were added to refs/heads/main by this push:
     new 6f19ff8a fix: the timeout of ApiClient is incorrectly overridden #2938
6f19ff8a is described below

commit 6f19ff8af52d0c20136fc2ca992f654ce0cd0850
Author: tk103331 <[email protected]>
AuthorDate: Mon Sep 5 09:35:09 2022 +0800

    fix: the timeout of ApiClient is incorrectly overridden #2938
---
 plugins/helper/api_async_client.go | 19 +++++++++++++++----
 plugins/helper/api_client.go       |  5 +++++
 2 files changed, 20 insertions(+), 4 deletions(-)

diff --git a/plugins/helper/api_async_client.go 
b/plugins/helper/api_async_client.go
index fea1f122..bdcb527d 100644
--- a/plugins/helper/api_async_client.go
+++ b/plugins/helper/api_async_client.go
@@ -46,6 +46,8 @@ type ApiAsyncClient struct {
        numOfWorkers int
 }
 
+const DEFAULT_TIMEOUT = 10 * time.Second
+
 // CreateAsyncApiClient creates a new ApiAsyncClient
 func CreateAsyncApiClient(
        taskCtx core.TaskContext,
@@ -57,11 +59,20 @@ func CreateAsyncApiClient(
        if err != nil {
                return nil, errors.BadInput.Wrap(err, "failed to parse 
API_RETRY", errors.AsUserMessage())
        }
-       timeout, err := utils.StrToDurationOr(taskCtx.GetConfig("API_TIMEOUT"), 
10*time.Second)
-       if err != nil {
-               return nil, errors.BadInput.Wrap(err, "failed to parse 
API_TIMEOUT", errors.AsUserMessage())
+
+       timeoutConf := taskCtx.GetConfig("API_TIMEOUT")
+       if timeoutConf != "" {
+               // override timeout value if API_TIMEOUT is provided
+               timeout, err := time.ParseDuration(timeoutConf)
+               if err != nil {
+                       return nil, errors.BadInput.Wrap(err, "failed to parse 
API_TIMEOUT", errors.AsUserMessage())
+               }
+               apiClient.SetTimeout(timeout)
+       } else if apiClient.GetTimeout() == 0 {
+               // Use DEFAULT_TIMEOUT when API_TIMEOUT is empty and ApiClient 
has no timeout set
+               apiClient.SetTimeout(DEFAULT_TIMEOUT)
        }
-       apiClient.SetTimeout(timeout)
+
        apiClient.SetLogger(taskCtx.GetLogger())
 
        globalRateLimitPerHour, err := 
utils.StrToIntOr(taskCtx.GetConfig("API_REQUESTS_PER_HOUR"), 18000)
diff --git a/plugins/helper/api_client.go b/plugins/helper/api_client.go
index 07ab53fc..c97d5459 100644
--- a/plugins/helper/api_client.go
+++ b/plugins/helper/api_client.go
@@ -135,6 +135,11 @@ func (apiClient *ApiClient) SetTimeout(timeout 
time.Duration) {
        apiClient.client.Timeout = timeout
 }
 
+// GetTimeout FIXME ...
+func (apiClient *ApiClient) GetTimeout() time.Duration {
+       return apiClient.client.Timeout
+}
+
 // SetHeaders FIXME ...
 func (apiClient *ApiClient) SetHeaders(headers map[string]string) {
        apiClient.headers = headers

Reply via email to