@@ -16,7 +16,7 @@ import (
1616 "sync/atomic"
1717 "time"
1818
19- "github.com/avast/retry-go/v4 "
19+ "github.com/avast/retry-go/v5 "
2020 "github.com/cloudquery/plugin-pb-go/internal/env"
2121 pbBase "github.com/cloudquery/plugin-pb-go/pb/base/v0"
2222 pbDiscovery "github.com/cloudquery/plugin-pb-go/pb/discovery/v0"
@@ -337,20 +337,23 @@ func (c *Client) startDockerPlugin(ctx context.Context, configPath string) error
337337 }
338338
339339 var hostConnection string
340- err = retry .Do (func () error {
341- hostConnection , err = getHostConnection (ctx , cli , resp .ID , portMapping .Port )
342- return err
343- }, retry .RetryIf (func (err error ) bool {
344- return err .Error () == "failed to get port mapping for container"
345- }),
340+ options := []retry.Option {
341+ retry .RetryIf (func (err error ) bool {
342+ return err .Error () == "failed to get port mapping for container"
343+ }),
346344 // this should generally succeed on first or second try, because we're only waiting for the container to start
347345 // to get the port mapping, not the plugin to start. The plugin will be waited for when we establish the tcp
348346 // connection.
349347 retry .Attempts (containerPortMappingRetries ),
350348 retry .Delay (containerPortMappingInitialRetryDelay ),
351349 retry .DelayType (retry .BackOffDelay ),
352- retry .MaxDelay (1 * time .Second ),
353- )
350+ retry .MaxDelay (1 * time .Second ),
351+ }
352+ retrier := retry .New (options ... )
353+ err = retrier .Do (func () error {
354+ hostConnection , err = getHostConnection (ctx , cli , resp .ID , portMapping .Port )
355+ return err
356+ })
354357 if err != nil {
355358 return fmt .Errorf ("failed to get host connection: %w" , err )
356359 }
@@ -390,7 +393,17 @@ func getHostConnection(ctx context.Context, cli *dockerClient.Client, containerI
390393}
391394
392395func waitForContainerRunning (ctx context.Context , cli * dockerClient.Client , containerID string ) error {
393- err := retry .Do (func () error {
396+ options := []retry.Option {
397+ retry .RetryIf (func (err error ) bool {
398+ return err != nil
399+ }),
400+ retry .Attempts (containerRunningRetries ),
401+ retry .Delay (containerRunningInitialRetryDelay ),
402+ retry .DelayType (retry .BackOffDelay ),
403+ retry .MaxDelay (1 * time .Second ),
404+ }
405+ retrier := retry .New (options ... )
406+ err := retrier .Do (func () error {
394407 containerJSON , err := cli .ContainerInspect (ctx , containerID )
395408 if err != nil {
396409 return fmt .Errorf ("failed to inspect container: %w" , err )
@@ -404,14 +417,7 @@ func waitForContainerRunning(ctx context.Context, cli *dockerClient.Client, cont
404417 }
405418 }
406419 return errors .New ("container not running" )
407- }, retry .RetryIf (func (err error ) bool {
408- return err != nil
409- }),
410- retry .Attempts (containerRunningRetries ),
411- retry .Delay (containerRunningInitialRetryDelay ),
412- retry .DelayType (retry .BackOffDelay ),
413- retry .MaxDelay (1 * time .Second ),
414- )
420+ })
415421 return err
416422}
417423
@@ -432,7 +438,16 @@ func getFreeTCPAddr() (string, error) {
432438
433439func (c * Client ) startLocal (ctx context.Context , path string ) error {
434440 attempt := 0
435- return retry .Do (
441+ options := []retry.Option {
442+ retry .Attempts (3 ),
443+ retry .Delay (1 * time .Second ),
444+ retry .LastErrorOnly (true ),
445+ retry .OnRetry (func (n uint , err error ) {
446+ c .logger .Debug ().Err (err ).Int ("attempt" , int (n )).Msg ("failed to start plugin, retrying" )
447+ }),
448+ }
449+ retrier := retry .New (options ... )
450+ return retrier .Do (
436451 func () error {
437452 attempt ++
438453 c .logger .Debug ().Str ("path" , path ).Int ("attempt" , attempt ).Msg ("starting plugin" )
@@ -454,12 +469,6 @@ func (c *Client) startLocal(ctx context.Context, path string) error {
454469 }
455470 return err
456471 },
457- retry .Attempts (3 ),
458- retry .Delay (1 * time .Second ),
459- retry .LastErrorOnly (true ),
460- retry .OnRetry (func (n uint , err error ) {
461- c .logger .Debug ().Err (err ).Int ("attempt" , int (n )).Msg ("failed to start plugin, retrying" )
462- }),
463472 )
464473}
465474
@@ -601,7 +610,17 @@ func (c *Client) connectUsingTCP(ctx context.Context, path string) error {
601610 return fmt .Errorf ("failed to dial grpc %s plugin at %s: %w" , c .typ .String (), path , err )
602611 }
603612
604- return retry .Do (
613+ options := []retry.Option {
614+ retry .RetryIf (func (err error ) bool {
615+ return err .Error () == "connection not ready"
616+ }),
617+ retry .Delay (containerServerHealthyInitialRetryDelay ),
618+ retry .Attempts (containerServerHealthyRetries ),
619+ retry .DelayType (retry .BackOffDelay ),
620+ retry .MaxDelay (1 * time .Second ),
621+ }
622+ retrier := retry .New (options ... )
623+ return retrier .Do (
605624 func () error {
606625 state := c .Conn .GetState ()
607626 if state == connectivity .Idle || state == connectivity .Ready {
@@ -612,13 +631,6 @@ func (c *Client) connectUsingTCP(ctx context.Context, path string) error {
612631 }
613632 return errors .New ("connection not ready" )
614633 },
615- retry .RetryIf (func (err error ) bool {
616- return err .Error () == "connection not ready"
617- }),
618- retry .Delay (containerServerHealthyInitialRetryDelay ),
619- retry .Attempts (containerServerHealthyRetries ),
620- retry .DelayType (retry .BackOffDelay ),
621- retry .MaxDelay (1 * time .Second ),
622634 )
623635}
624636
0 commit comments