@@ -337,20 +337,20 @@ 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- }),
346- // this should generally succeed on first or second try, because we're only waiting for the container to start
347- // to get the port mapping, not the plugin to start. The plugin will be waited for when we establish the tcp
348- // connection.
340+ options := []retry.Option {
341+ retry .RetryIf (func (err error ) bool {
342+ return err .Error () == "failed to get port mapping for container"
343+ }),
349344 retry .Attempts (containerPortMappingRetries ),
350345 retry .Delay (containerPortMappingInitialRetryDelay ),
351346 retry .DelayType (retry .BackOffDelay ),
352- retry .MaxDelay (1 * time .Second ),
353- )
347+ retry .MaxDelay (1 * time .Second ),
348+ }
349+ retrier := retry .New (options ... )
350+ err = retrier .Do (func () error {
351+ hostConnection , err = getHostConnection (ctx , cli , resp .ID , portMapping .Port )
352+ return err
353+ })
354354 if err != nil {
355355 return fmt .Errorf ("failed to get host connection: %w" , err )
356356 }
@@ -390,7 +390,17 @@ func getHostConnection(ctx context.Context, cli *dockerClient.Client, containerI
390390}
391391
392392func waitForContainerRunning (ctx context.Context , cli * dockerClient.Client , containerID string ) error {
393- err := retry .Do (func () error {
393+ options := []retry.Option {
394+ retry .RetryIf (func (err error ) bool {
395+ return err != nil
396+ }),
397+ retry .Attempts (containerRunningRetries ),
398+ retry .Delay (containerRunningInitialRetryDelay ),
399+ retry .DelayType (retry .BackOffDelay ),
400+ retry .MaxDelay (1 * time .Second ),
401+ }
402+ retrier := retry .New (options ... )
403+ err := retrier .Do (func () error {
394404 containerJSON , err := cli .ContainerInspect (ctx , containerID )
395405 if err != nil {
396406 return fmt .Errorf ("failed to inspect container: %w" , err )
@@ -404,14 +414,7 @@ func waitForContainerRunning(ctx context.Context, cli *dockerClient.Client, cont
404414 }
405415 }
406416 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- )
417+ })
415418 return err
416419}
417420
@@ -432,7 +435,16 @@ func getFreeTCPAddr() (string, error) {
432435
433436func (c * Client ) startLocal (ctx context.Context , path string ) error {
434437 attempt := 0
435- return retry .Do (
438+ options := []retry.Option {
439+ retry .Attempts (3 ),
440+ retry .Delay (1 * time .Second ),
441+ retry .LastErrorOnly (true ),
442+ retry .OnRetry (func (n uint , err error ) {
443+ c .logger .Debug ().Err (err ).Int ("attempt" , int (n )).Msg ("failed to start plugin, retrying" )
444+ }),
445+ }
446+ retrier := retry .New (options ... )
447+ return retrier .Do (
436448 func () error {
437449 attempt ++
438450 c .logger .Debug ().Str ("path" , path ).Int ("attempt" , attempt ).Msg ("starting plugin" )
@@ -454,12 +466,6 @@ func (c *Client) startLocal(ctx context.Context, path string) error {
454466 }
455467 return err
456468 },
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- }),
463469 )
464470}
465471
@@ -601,7 +607,17 @@ func (c *Client) connectUsingTCP(ctx context.Context, path string) error {
601607 return fmt .Errorf ("failed to dial grpc %s plugin at %s: %w" , c .typ .String (), path , err )
602608 }
603609
604- return retry .Do (
610+ options := []retry.Option {
611+ retry .RetryIf (func (err error ) bool {
612+ return err .Error () == "connection not ready"
613+ }),
614+ retry .Delay (containerServerHealthyInitialRetryDelay ),
615+ retry .Attempts (containerServerHealthyRetries ),
616+ retry .DelayType (retry .BackOffDelay ),
617+ retry .MaxDelay (1 * time .Second ),
618+ }
619+ retrier := retry .New (options ... )
620+ return retrier .Do (
605621 func () error {
606622 state := c .Conn .GetState ()
607623 if state == connectivity .Idle || state == connectivity .Ready {
@@ -612,13 +628,6 @@ func (c *Client) connectUsingTCP(ctx context.Context, path string) error {
612628 }
613629 return errors .New ("connection not ready" )
614630 },
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 ),
622631 )
623632}
624633
0 commit comments