Skip to content

Commit c7c5e72

Browse files
committed
fix(helper): use inline func type for GraphqlClientOption to avoid mock cycle
Replace exported GraphqlClientOption type with inline func(*GraphqlAsyncClient) in CreateAsyncGraphqlClient signature. The named type caused mockery to generate a mock file (GraphqlClientOption.go) that created an import cycle in tests.
1 parent 9cb0970 commit c7c5e72

1 file changed

Lines changed: 15 additions & 17 deletions

File tree

backend/helpers/pluginhelper/api/graphql_async_client.go

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,18 @@ package api
2020
import (
2121
"context"
2222
"fmt"
23+
"strconv"
24+
"sync"
25+
"time"
26+
2327
"github.com/apache/incubator-devlake/core/errors"
2428
"github.com/apache/incubator-devlake/core/log"
2529
"github.com/apache/incubator-devlake/core/plugin"
2630
"github.com/apache/incubator-devlake/core/utils"
27-
"strconv"
28-
"sync"
29-
"time"
3031

3132
"github.com/merico-ai/graphql"
3233
)
3334

34-
// GraphqlClientOption is a function that configures a GraphqlAsyncClient
35-
type GraphqlClientOption func(*GraphqlAsyncClient)
36-
3735
// GraphqlAsyncClient send graphql one by one
3836
type GraphqlAsyncClient struct {
3937
ctx context.Context
@@ -62,7 +60,7 @@ func CreateAsyncGraphqlClient(
6260
graphqlClient *graphql.Client,
6361
logger log.Logger,
6462
getRateRemaining func(context.Context, *graphql.Client, log.Logger) (rateRemaining int, resetAt *time.Time, err errors.Error),
65-
opts ...GraphqlClientOption,
63+
opts ...func(*GraphqlAsyncClient),
6664
) (*GraphqlAsyncClient, errors.Error) {
6765
ctxWithCancel, cancel := context.WithCancel(taskCtx.GetContext())
6866

@@ -83,7 +81,7 @@ func CreateAsyncGraphqlClient(
8381

8482
// Env config wins over everything, only if explicitly set
8583
if rateLimit := resolveRateLimit(taskCtx, logger); rateLimit != -1 {
86-
logger.Info("GRAPHQL_RATE_LIMIT env override applied: %d (was %d)", rateLimit, graphqlAsyncClient.rateRemaining)
84+
logger.Info("GRAPHQL_RATE_LIMIT env override applied: %d (was %d)", rateLimit, graphqlAsyncClient.rateRemaining)
8785
graphqlAsyncClient.rateRemaining = rateLimit
8886
}
8987

@@ -156,7 +154,7 @@ func (apiClient *GraphqlAsyncClient) updateRateRemaining(rateRemaining int, rese
156154
case <-time.After(nextDuring):
157155
newRateRemaining, newResetAt, err := apiClient.getRateRemaining(apiClient.ctx, apiClient.client, apiClient.logger)
158156
if err != nil {
159-
apiClient.logger.Warn(err, "failed to update graphql rate limit, will retry next cycle")
157+
apiClient.logger.Info("failed to update graphql rate limit, will retry next cycle: %v", err)
160158
apiClient.updateRateRemaining(apiClient.rateRemaining, nil)
161159
return
162160
}
@@ -254,7 +252,7 @@ func (apiClient *GraphqlAsyncClient) Release() {
254252
// WithFallbackRateLimit sets the initial/fallback rate limit used when
255253
// rate limit information cannot be fetched dynamically.
256254
// This value may be overridden later by getRateRemaining.
257-
func WithFallbackRateLimit(limit int) GraphqlClientOption {
255+
func WithFallbackRateLimit(limit int) func(*GraphqlAsyncClient) {
258256
return func(c *GraphqlAsyncClient) {
259257
if limit > 0 {
260258
c.rateRemaining = limit
@@ -264,11 +262,11 @@ func WithFallbackRateLimit(limit int) GraphqlClientOption {
264262

265263
// resolveRateLimit returns -1 if GRAPHQL_RATE_LIMIT is not set or invalid
266264
func resolveRateLimit(taskCtx plugin.TaskContext, logger log.Logger) int {
267-
if v := taskCtx.GetConfig("GRAPHQL_RATE_LIMIT"); v != "" {
268-
if parsed, err := strconv.Atoi(v); err == nil {
269-
return parsed
270-
}
271-
logger.Warn(nil, "invalid GRAPHQL_RATE_LIMIT, using default")
272-
}
273-
return -1
265+
if v := taskCtx.GetConfig("GRAPHQL_RATE_LIMIT"); v != "" {
266+
if parsed, err := strconv.Atoi(v); err == nil {
267+
return parsed
268+
}
269+
logger.Warn(nil, "invalid GRAPHQL_RATE_LIMIT, using default")
270+
}
271+
return -1
274272
}

0 commit comments

Comments
 (0)