Skip to content

Commit e9d0b41

Browse files
committed
fix: Move to new API
1 parent 8d3ea7e commit e9d0b41

2 files changed

Lines changed: 67 additions & 52 deletions

File tree

managedplugin/download.go

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,18 @@ func getURLLocation(ctx context.Context, org string, name string, version string
7474
err429 = errors.New("429")
7575
)
7676

77+
options := []retry.Option{
78+
retry.RetryIf(func(err error) bool {
79+
return err == err401 || err == err429
80+
}),
81+
retry.Context(ctx),
82+
retry.Attempts(RetryAttempts),
83+
retry.Delay(RetryWaitTime),
84+
retry.LastErrorOnly(true),
85+
}
86+
retrier := retry.New(options...)
7787
for _, downloadURL := range urls {
78-
err := retry.Do(func() error {
88+
err := retrier.Do(func() error {
7989
req, err := http.NewRequestWithContext(ctx, http.MethodGet, downloadURL, nil)
8090
if err != nil {
8191
return fmt.Errorf("failed create request %s: %w", downloadURL, err)
@@ -101,14 +111,7 @@ func getURLLocation(ctx context.Context, org string, name string, version string
101111
fmt.Printf("Failed downloading %s with status code %d\n", downloadURL, resp.StatusCode)
102112
return fmt.Errorf("statusCode %d", resp.StatusCode)
103113
}
104-
}, retry.RetryIf(func(err error) bool {
105-
return err == err401 || err == err429
106-
}),
107-
retry.Context(ctx),
108-
retry.Attempts(RetryAttempts),
109-
retry.Delay(RetryWaitTime),
110-
retry.LastErrorOnly(true),
111-
)
114+
})
112115
if err == err404 {
113116
continue
114117
}
@@ -338,7 +341,16 @@ func downloadFile(ctx context.Context, localPath string, downloadURL string, dop
338341
defer out.Close()
339342

340343
checksum := ""
341-
err = retry.Do(func() error {
344+
options := []retry.Option{
345+
retry.RetryIf(func(err error) bool {
346+
return err.Error() == "statusCode != 200"
347+
}),
348+
retry.Context(ctx),
349+
retry.Attempts(RetryAttempts),
350+
retry.Delay(RetryWaitTime),
351+
}
352+
retrier := retry.New(options...)
353+
err = retrier.Do(func() error {
342354
checksum = ""
343355
// Get the data
344356
req, err := http.NewRequestWithContext(ctx, http.MethodGet, downloadURL, nil)
@@ -384,13 +396,7 @@ func downloadFile(ctx context.Context, localPath string, downloadURL string, dop
384396
}
385397
checksum = fmt.Sprintf("%x", s.Sum(nil))
386398
return nil
387-
}, retry.RetryIf(func(err error) bool {
388-
return err.Error() == "statusCode != 200"
389-
}),
390-
retry.Context(ctx),
391-
retry.Attempts(RetryAttempts),
392-
retry.Delay(RetryWaitTime),
393-
)
399+
})
394400
if err != nil {
395401
for _, e := range err.(retry.Error) {
396402
if e.Error() == "not found" {

managedplugin/plugin.go

Lines changed: 44 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -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

392392
func 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

433436
func (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

Comments
 (0)