Skip to content

Commit

Permalink
Add Timeout to ConnectConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
Blinkuu committed Jul 19, 2023
1 parent 25ec9fa commit 0844bcb
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 35 deletions.
20 changes: 13 additions & 7 deletions cloudapi/insights/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ import (
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/status"

"go.k6.io/k6/lib/types"

"go.k6.io/k6/cloudapi/insights/proto/v1/ingester"
)

Expand All @@ -38,7 +36,7 @@ var (
// ClientConfig is the configuration for the client.
type ClientConfig struct {
IngesterHost string
Timeout types.NullDuration
Timeout time.Duration
ConnectConfig ClientConnectConfig
AuthConfig ClientAuthConfig
TLSConfig ClientTLSConfig
Expand All @@ -49,6 +47,7 @@ type ClientConfig struct {
type ClientConnectConfig struct {
Block bool
FailOnNonTempDialError bool
Timeout time.Duration
Dialer func(context.Context, string) (net.Conn, error)
}

Expand Down Expand Up @@ -93,7 +92,13 @@ type Client struct {
func NewDefaultClientConfigForTestRun(ingesterHost, authToken string, testRunID int64) ClientConfig {
return ClientConfig{
IngesterHost: ingesterHost,
Timeout: types.NewNullDuration(90*time.Second, false),
Timeout: 90 * time.Second,
ConnectConfig: ClientConnectConfig{
Block: false,
FailOnNonTempDialError: false,
Timeout: 10 * time.Second,
Dialer: nil,
},
AuthConfig: ClientAuthConfig{
Enabled: true,
TestRunID: testRunID,
Expand Down Expand Up @@ -140,6 +145,8 @@ func (c *Client) Dial(ctx context.Context) error {
return fmt.Errorf("failed to create dial options: %w", err)
}

ctx, cancel := context.WithTimeout(ctx, c.cfg.ConnectConfig.Timeout)
defer cancel()
conn, err := grpc.DialContext(ctx, c.cfg.IngesterHost, opts...)
if err != nil {
return fmt.Errorf("failed to dial: %w", err)
Expand All @@ -160,9 +167,6 @@ func (c *Client) IngestRequestMetadatasBatch(ctx context.Context, requestMetadat
return ErrClientClosed
}

ctx, cancel := context.WithTimeout(ctx, c.cfg.Timeout.TimeDuration())
defer cancel()

if len(requestMetadatas) < 1 {
return nil
}
Expand All @@ -172,6 +176,8 @@ func (c *Client) IngestRequestMetadatasBatch(ctx context.Context, requestMetadat
return fmt.Errorf("failed to create request from request metadatas: %w", err)
}

ctx, cancel := context.WithTimeout(ctx, c.cfg.Timeout)
defer cancel()
_, err = c.client.BatchCreateRequestMetadatas(ctx, req)
if err != nil {
st := status.Convert(err)
Expand Down
44 changes: 22 additions & 22 deletions cloudapi/insights/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"time"

"github.com/stretchr/testify/require"
"go.k6.io/k6/lib/types"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
Expand Down Expand Up @@ -92,8 +91,8 @@ func TestClient_Dial_ReturnsNoErrorWithWorkingDialer(t *testing.T) {
lis := newMockListener(t, ser)

cfg := ClientConfig{
Timeout: types.NullDurationFrom(1 * time.Second),
ConnectConfig: ClientConnectConfig{Dialer: newMockContextDialer(t, lis)},
Timeout: 1 * time.Second,
ConnectConfig: ClientConnectConfig{Timeout: 1 * time.Second, Dialer: newMockContextDialer(t, lis)},
TLSConfig: ClientTLSConfig{Insecure: true},
RetryConfig: ClientRetryConfig{RetryableStatusCodes: `"UNKNOWN","INTERNAL","UNAVAILABLE","DEADLINE_EXCEEDED"`},
}
Expand All @@ -114,8 +113,8 @@ func TestClient_Dial_ReturnsErrorWhenCalledTwice(t *testing.T) {
lis := newMockListener(t, ser)

cfg := ClientConfig{
Timeout: types.NullDurationFrom(1 * time.Second),
ConnectConfig: ClientConnectConfig{Dialer: newMockContextDialer(t, lis)},
Timeout: 1 * time.Second,
ConnectConfig: ClientConnectConfig{Timeout: 1 * time.Second, Dialer: newMockContextDialer(t, lis)},
TLSConfig: ClientTLSConfig{Insecure: true},
RetryConfig: ClientRetryConfig{RetryableStatusCodes: `"UNKNOWN","INTERNAL","UNAVAILABLE","DEADLINE_EXCEEDED"`},
}
Expand All @@ -138,6 +137,7 @@ func TestClient_Dial_ReturnsNoErrorWithFailingDialer(t *testing.T) {
ConnectConfig: ClientConnectConfig{
Block: true,
FailOnNonTempDialError: true,
Timeout: 1 * time.Second,
Dialer: func(ctx context.Context, s string) (net.Conn, error) {
return nil, &fatalError{}
},
Expand All @@ -163,7 +163,7 @@ func TestClient_Dial_ReturnsErrorWithoutRetryableStatusCodes(t *testing.T) {
lis := newMockListener(t, ser)

cfg := ClientConfig{
Timeout: types.NullDurationFrom(1 * time.Second),
Timeout: 1 * time.Second,
ConnectConfig: ClientConnectConfig{Dialer: newMockContextDialer(t, lis)},
TLSConfig: ClientTLSConfig{Insecure: true},
}
Expand All @@ -184,7 +184,7 @@ func TestClient_Dial_ReturnsErrorWithInvalidRetryableStatusCodes(t *testing.T) {
lis := newMockListener(t, ser)

cfg := ClientConfig{
Timeout: types.NullDurationFrom(1 * time.Second),
Timeout: 1 * time.Second,
ConnectConfig: ClientConnectConfig{Dialer: newMockContextDialer(t, lis)},
TLSConfig: ClientTLSConfig{Insecure: true},
RetryConfig: ClientRetryConfig{RetryableStatusCodes: "RANDOM,INTERNAL"},
Expand All @@ -206,8 +206,8 @@ func TestClient_IngestRequestMetadatasBatch_ReturnsNoErrorWithWorkingServerAndNo
lis := newMockListener(t, ser)

cfg := ClientConfig{
Timeout: types.NullDurationFrom(1 * time.Second),
ConnectConfig: ClientConnectConfig{Dialer: newMockContextDialer(t, lis)},
Timeout: 1 * time.Second,
ConnectConfig: ClientConnectConfig{Timeout: 1 * time.Second, Dialer: newMockContextDialer(t, lis)},
TLSConfig: ClientTLSConfig{Insecure: true},
RetryConfig: ClientRetryConfig{RetryableStatusCodes: `"UNKNOWN","INTERNAL","UNAVAILABLE","DEADLINE_EXCEEDED"`},
}
Expand All @@ -231,8 +231,8 @@ func TestClient_IngestRequestMetadatasBatch_ReturnsNoErrorWithWorkingServerAndNo
lis := newMockListener(t, ser)

cfg := ClientConfig{
Timeout: types.NullDurationFrom(1 * time.Second),
ConnectConfig: ClientConnectConfig{Dialer: newMockContextDialer(t, lis)},
Timeout: 1 * time.Second,
ConnectConfig: ClientConnectConfig{Timeout: 1 * time.Second, Dialer: newMockContextDialer(t, lis)},
TLSConfig: ClientTLSConfig{Insecure: true},
RetryConfig: ClientRetryConfig{RetryableStatusCodes: `"UNKNOWN","INTERNAL","UNAVAILABLE","DEADLINE_EXCEEDED"`},
}
Expand Down Expand Up @@ -272,8 +272,8 @@ func TestClient_IngestRequestMetadatasBatch_ReturnsErrorWithWorkingServerAndCanc
lis := newMockListener(t, ser)

cfg := ClientConfig{
Timeout: types.NullDurationFrom(1 * time.Second),
ConnectConfig: ClientConnectConfig{Dialer: newMockContextDialer(t, lis)},
Timeout: 1 * time.Second,
ConnectConfig: ClientConnectConfig{Timeout: 1 * time.Second, Dialer: newMockContextDialer(t, lis)},
TLSConfig: ClientTLSConfig{Insecure: true},
RetryConfig: ClientRetryConfig{RetryableStatusCodes: `"UNKNOWN","INTERNAL","UNAVAILABLE","DEADLINE_EXCEEDED"`},
}
Expand Down Expand Up @@ -308,7 +308,7 @@ func TestClient_IngestRequestMetadatasBatch_ReturnsErrorWithUninitializedClient(
lis := newMockListener(t, ser)

cfg := ClientConfig{
Timeout: types.NullDurationFrom(1 * time.Second),
Timeout: 1 * time.Second,
ConnectConfig: ClientConnectConfig{Dialer: newMockContextDialer(t, lis)},
TLSConfig: ClientTLSConfig{Insecure: true},
}
Expand Down Expand Up @@ -341,8 +341,8 @@ func TestClient_IngestRequestMetadatasBatch_ReturnsErrorWithFailingServerAndNonC
lis := newMockListener(t, ser)

cfg := ClientConfig{
Timeout: types.NullDurationFrom(1 * time.Second),
ConnectConfig: ClientConnectConfig{Dialer: newMockContextDialer(t, lis)},
Timeout: 1 * time.Second,
ConnectConfig: ClientConnectConfig{Timeout: 1 * time.Second, Dialer: newMockContextDialer(t, lis)},
TLSConfig: ClientTLSConfig{Insecure: true},
RetryConfig: ClientRetryConfig{RetryableStatusCodes: `"UNKNOWN","INTERNAL","UNAVAILABLE","DEADLINE_EXCEEDED"`},
}
Expand Down Expand Up @@ -373,8 +373,8 @@ func TestClient_IngestRequestMetadatasBatch_ReturnsNoErrorAfterRetrySeveralTimes
lis := newMockListener(t, ser)

cfg := ClientConfig{
Timeout: types.NullDurationFrom(1 * time.Second),
ConnectConfig: ClientConnectConfig{Dialer: newMockContextDialer(t, lis)},
Timeout: 1 * time.Second,
ConnectConfig: ClientConnectConfig{Timeout: 1 * time.Second, Dialer: newMockContextDialer(t, lis)},
TLSConfig: ClientTLSConfig{Insecure: true},
RetryConfig: ClientRetryConfig{
MaxAttempts: 20,
Expand Down Expand Up @@ -422,8 +422,8 @@ func TestClient_IngestRequestMetadatasBatch_ReturnsErrorAfterExhaustingMaxRetryA
lis := newMockListener(t, ser)

cfg := ClientConfig{
Timeout: types.NullDurationFrom(1 * time.Second),
ConnectConfig: ClientConnectConfig{Dialer: newMockContextDialer(t, lis)},
Timeout: 1 * time.Second,
ConnectConfig: ClientConnectConfig{Timeout: 1 * time.Second, Dialer: newMockContextDialer(t, lis)},
TLSConfig: ClientTLSConfig{Insecure: true},
RetryConfig: ClientRetryConfig{
BackoffConfig: ClientBackoffConfig{
Expand Down Expand Up @@ -469,7 +469,7 @@ func TestClient_Close_ReturnsNoErrorWhenClosedOnce(t *testing.T) {
lis := newMockListener(t, ser)

cfg := ClientConfig{
ConnectConfig: ClientConnectConfig{Dialer: newMockContextDialer(t, lis)},
ConnectConfig: ClientConnectConfig{Timeout: 1 * time.Second, Dialer: newMockContextDialer(t, lis)},
TLSConfig: ClientTLSConfig{Insecure: true},
RetryConfig: ClientRetryConfig{RetryableStatusCodes: `"UNKNOWN","INTERNAL","UNAVAILABLE","DEADLINE_EXCEEDED"`},
}
Expand All @@ -491,7 +491,7 @@ func TestClient_Close_ReturnsNoErrorWhenClosedTwice(t *testing.T) {
lis := newMockListener(t, ser)

cfg := ClientConfig{
ConnectConfig: ClientConnectConfig{Dialer: newMockContextDialer(t, lis)},
ConnectConfig: ClientConnectConfig{Timeout: 1 * time.Second, Dialer: newMockContextDialer(t, lis)},
TLSConfig: ClientTLSConfig{Insecure: true},
RetryConfig: ClientRetryConfig{RetryableStatusCodes: `"UNKNOWN","INTERNAL","UNAVAILABLE","DEADLINE_EXCEEDED"`},
}
Expand Down
4 changes: 1 addition & 3 deletions output/cloud/expv2/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,7 @@ func (o *Output) Start() error {
)
insightsClient := insights.NewClient(insightsClientConfig)

ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
if err := insightsClient.Dial(ctx); err != nil {
if err := insightsClient.Dial(context.Background()); err != nil {
return err
}

Expand Down
4 changes: 1 addition & 3 deletions output/cloud/v1/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,7 @@ func (out *Output) Start() error {
)
insightsClient := insights.NewClient(insightsClientConfig)

ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
if err := insightsClient.Dial(ctx); err != nil {
if err := insightsClient.Dial(context.Background()); err != nil {
return err
}

Expand Down

0 comments on commit 0844bcb

Please sign in to comment.