Optimize fetch manifest loop when handling notification

Signed-off-by: mmpei <peimingming@corp.netease.com>
This commit is contained in:
mmpei 2019-07-01 17:54:52 +08:00
parent b9c96f7e56
commit 5dfc3f2402
2 changed files with 8 additions and 6 deletions

View File

@ -111,7 +111,7 @@ func (n *NotificationHandler) Post() {
}()
}
if !coreutils.WaitForManifestReady(repository, tag, 5) {
if !coreutils.WaitForManifestReady(repository, tag, 6) {
log.Errorf("Manifest for image %s:%s is not ready, skip the follow up actions.", repository, tag)
return
}

View File

@ -48,14 +48,19 @@ func NewRepositoryClientForUI(username, repository string) (*registry.Repository
// WaitForManifestReady implements exponential sleeep to wait until manifest is ready in registry.
// This is a workaround for https://github.com/docker/distribution/issues/2625
func WaitForManifestReady(repository string, tag string, maxRetry int) bool {
// The initial wait interval, hard-coded to 50ms
interval := 50 * time.Millisecond
// The initial wait interval, hard-coded to 80ms, interval will be 80ms,200ms,500ms,1.25s,3.124999936s
interval := 80 * time.Millisecond
repoClient, err := NewRepositoryClientForUI("harbor-core", repository)
if err != nil {
log.Errorf("Failed to create repo client.")
return false
}
for i := 0; i < maxRetry; i++ {
if i != 0 {
log.Warningf("manifest for image %s:%s is not ready, retry after %v", repository, tag, interval)
time.Sleep(interval)
interval = time.Duration(int64(float32(interval) * 2.5))
}
_, exist, err := repoClient.ManifestExist(tag)
if err != nil {
log.Errorf("Unexpected error when checking manifest existence, image: %s:%s, error: %v", repository, tag, err)
@ -64,9 +69,6 @@ func WaitForManifestReady(repository string, tag string, maxRetry int) bool {
if exist {
return true
}
log.Warningf("manifest for image %s:%s is not ready, retry after %v", repository, tag, interval)
time.Sleep(interval)
interval = interval * 2
}
return false
}