You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@devlake.apache.org by kl...@apache.org on 2022/07/08 07:39:49 UTC

[incubator-devlake] branch main updated: fix: add release to apiclient (#2444)

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

klesh 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 f5e9b37f fix: add release to apiclient (#2444)
f5e9b37f is described below

commit f5e9b37f7d1c352e54bdeb345ef9a70c55866f66
Author: mappjzc <zh...@merico.dev>
AuthorDate: Fri Jul 8 15:39:45 2022 +0800

    fix: add release to apiclient (#2444)
    
    Add Release() for RateLimitedApiClient and ApiAsyncClient.
    Add defer Release() call at Execute of api_collector.
    
    Nddtfjiang <zh...@merico.dev>
---
 plugins/helper/api_async_client.go   | 9 +++++++++
 plugins/helper/api_collector.go      | 6 +++++-
 plugins/helper/api_collector_test.go | 1 +
 3 files changed, 15 insertions(+), 1 deletion(-)

diff --git a/plugins/helper/api_async_client.go b/plugins/helper/api_async_client.go
index 4cf5b88e..2d55ce2d 100644
--- a/plugins/helper/api_async_client.go
+++ b/plugins/helper/api_async_client.go
@@ -207,18 +207,26 @@ func (apiClient *ApiAsyncClient) WaitAsync() error {
 	return apiClient.scheduler.Wait()
 }
 
+// HasError to return if the scheduler has Error
 func (apiClient *ApiAsyncClient) HasError() bool {
 	return apiClient.scheduler.HasError()
 }
 
+// NextTick to return the NextTick of scheduler
 func (apiClient *ApiAsyncClient) NextTick(task func() error) {
 	apiClient.scheduler.NextTick(task)
 }
 
+// GetNumOfWorkers to return the Workers count if scheduler.
 func (apiClient *ApiAsyncClient) GetNumOfWorkers() int {
 	return apiClient.numOfWorkers
 }
 
+// Release will release the ApiAsyncClient with scheduler
+func (apiClient *ApiAsyncClient) Release() {
+	apiClient.scheduler.Release()
+}
+
 type RateLimitedApiClient interface {
 	GetAsync(path string, query url.Values, header http.Header, handler common.ApiAsyncCallback)
 	WaitAsync() error
@@ -226,6 +234,7 @@ type RateLimitedApiClient interface {
 	NextTick(task func() error)
 	GetNumOfWorkers() int
 	SetAfterFunction(callback common.ApiClientAfterResponse)
+	Release()
 }
 
 var _ RateLimitedApiClient = (*ApiAsyncClient)(nil)
diff --git a/plugins/helper/api_collector.go b/plugins/helper/api_collector.go
index ec2dd522..500b42ab 100644
--- a/plugins/helper/api_collector.go
+++ b/plugins/helper/api_collector.go
@@ -147,8 +147,12 @@ func (collector *ApiCollector) Execute() error {
 	collector.args.Ctx.SetProgress(0, -1)
 	if collector.args.Input != nil {
 		iterator := collector.args.Input
-		apiClient := collector.args.ApiClient
 		defer iterator.Close()
+		apiClient := collector.args.ApiClient
+		if apiClient == nil {
+			return fmt.Errorf("api_collector can not Execute with nil apiClient")
+		}
+		defer apiClient.Release()
 		for iterator.HasNext() && !apiClient.HasError() {
 			input, err := iterator.Fetch()
 			if err != nil {
diff --git a/plugins/helper/api_collector_test.go b/plugins/helper/api_collector_test.go
index 400d85f1..3a7bdd0a 100644
--- a/plugins/helper/api_collector_test.go
+++ b/plugins/helper/api_collector_test.go
@@ -76,6 +76,7 @@ func TestFetchPageUndetermined(t *testing.T) {
 	params := struct {
 		Name string
 	}{Name: "testparams"}
+	mockApi.On("Release").Return()
 
 	collector, err := NewApiCollector(ApiCollectorArgs{
 		RawDataSubTaskArgs: RawDataSubTaskArgs{