Skip to content

Commit ca3f9bf

Browse files
committed
refactor: replace retry with retryNotify in image sync
1 parent bb2780f commit ca3f9bf

File tree

1 file changed

+21
-11
lines changed

1 file changed

+21
-11
lines changed

internal/sync/images.go

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"github.com/Altinity/docker-sync/internal/telemetry"
1212
"github.com/Altinity/docker-sync/structs"
1313
"github.com/cenkalti/backoff/v4"
14-
"github.com/google/go-containerregistry/pkg/logs"
1514
"github.com/google/go-containerregistry/pkg/name"
1615
"github.com/google/go-containerregistry/pkg/v1/remote"
1716
"github.com/rs/zerolog/log"
@@ -31,7 +30,7 @@ func checkRateLimit(err error) error {
3130
}
3231

3332
func push(ctx context.Context, image *structs.Image, desc *remote.Descriptor, dst string, tag string) error {
34-
return backoff.Retry(func() error {
33+
return backoff.RetryNotify(func() error {
3534
pushAuth, _ := getAuth(image.GetRegistry(dst), image.GetRepository(dst))
3635

3736
pusher, err := remote.NewPusher(pushAuth)
@@ -44,16 +43,22 @@ func push(ctx context.Context, image *structs.Image, desc *remote.Descriptor, ds
4443
return fmt.Errorf("failed to parse tag: %w", err)
4544
}
4645

47-
logs.Progress.Printf("Pushing %s", dstTag)
48-
4946
if err := pusher.Push(ctx, dstTag, desc); err != nil {
5047
return checkRateLimit(err)
5148
}
5249

5350
return nil
54-
}, backoff.NewExponentialBackOff(
51+
}, backoff.WithMaxRetries(backoff.NewExponentialBackOff(
5552
backoff.WithInitialInterval(1*time.Minute),
56-
))
53+
), config.SyncMaxErrors.UInt64()), func(err error, dur time.Duration) {
54+
log.Error().
55+
Err(err).
56+
Dur("backoff", dur).
57+
Str("image", image.Source).
58+
Str("tag", tag).
59+
Str("target", dst).
60+
Msg("Push failed")
61+
})
5762
}
5863

5964
func pull(ctx context.Context, puller *remote.Puller, image *structs.Image, tag string) (*remote.Descriptor, error) {
@@ -64,17 +69,22 @@ func pull(ctx context.Context, puller *remote.Puller, image *structs.Image, tag
6469

6570
var desc *remote.Descriptor
6671

67-
logs.Progress.Printf("Fetching %s", srcTag)
68-
69-
if err := backoff.Retry(func() error {
72+
if err := backoff.RetryNotify(func() error {
7073
desc, err = puller.Get(ctx, srcTag)
7174
if err != nil {
7275
return checkRateLimit(err)
7376
}
7477
return nil
75-
}, backoff.NewExponentialBackOff(
78+
}, backoff.WithMaxRetries(backoff.NewExponentialBackOff(
7679
backoff.WithInitialInterval(1*time.Minute),
77-
)); err != nil {
80+
), config.SyncMaxErrors.UInt64()), func(err error, dur time.Duration) {
81+
log.Error().
82+
Err(err).
83+
Dur("backoff", dur).
84+
Str("image", image.Source).
85+
Str("tag", tag).
86+
Msg("Pull failed")
87+
}); err != nil {
7888
return nil, err
7989
}
8090

0 commit comments

Comments
 (0)