Skip to content

Commit

Permalink
rthooks: use a generic retry function
Browse files Browse the repository at this point in the history
Both findMirrorPod and findPod functions do the same retry loop.
Use a helper retry function instead.

Signed-off-by: Kornilios Kourtis <[email protected]>
  • Loading branch information
kkourt committed Aug 20, 2024
1 parent 9b106cd commit ebd29de
Showing 1 changed file with 20 additions and 29 deletions.
49 changes: 20 additions & 29 deletions pkg/rthooks/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,46 +110,37 @@ func (arg *CreateContainerArg) Pod() (*corev1.Pod, error) {
}

func (arg *CreateContainerArg) findMirrorPod(hash string) (*corev1.Pod, error) {
var pod *corev1.Pod
var err error

nretries := 5
for i := 0; i < nretries; i++ {
pod, err = arg.Watcher.FindMirrorPod(hash)
if err == nil {
break
}
time.Sleep(10 * time.Millisecond)
}
return retry(5, 10*time.Millisecond, func() (*corev1.Pod, error) {
return arg.Watcher.FindMirrorPod(hash)
})

if err != nil {
err = fmt.Errorf("failed to fetch pod info after %d retries: %w", nretries, err)
}

return pod, err
}

func (arg *CreateContainerArg) findPod() (*corev1.Pod, error) {
var pod *corev1.Pod
var err error

podId, err := arg.PodID()
podID, err := arg.PodID()
if err != nil {
return nil, err
}

nretries := 5
for i := 0; i < nretries; i++ {
pod, err = arg.Watcher.FindPod(podId)
return retry(5, 10*time.Millisecond, func() (*corev1.Pod, error) {
return arg.Watcher.FindPod(podID)
})
}

func retry[R any](nretries int, timeout time.Duration, fn func() (R, error)) (R, error) {
var err error
var ret R
for i := 0; ; i++ {
ret, err = fn()
if err == nil {
break
return ret, nil
}
time.Sleep(10 * time.Millisecond)
}

if err != nil {
err = fmt.Errorf("failed to fetch pod info after %d retries: %w", nretries, err)
}
if i >= nretries {
return ret, fmt.Errorf("failed to fetch pod info after %d retries: %w", nretries, err)
}

return pod, err
time.Sleep(timeout)
}
}

0 comments on commit ebd29de

Please sign in to comment.