From f0aed6936f8ce101b5ab3750b9bbef860e22dfd4 Mon Sep 17 00:00:00 2001 From: waigani Date: Tue, 20 Oct 2020 14:42:47 +1300 Subject: [PATCH 1/2] Add Retry --- diffparser.go | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/diffparser.go b/diffparser.go index 385648c..46c36b3 100644 --- a/diffparser.go +++ b/diffparser.go @@ -4,9 +4,11 @@ package diffparser import ( + "context" "regexp" "strconv" "strings" + "time" "errors" ) @@ -36,6 +38,24 @@ type DiffRange struct { Lines []*DiffLine } +func retry(ctx context.Context, f func() error) (int, error) { + attempt := 0 + for delay := time.Millisecond; ; delay *= 2 { + attempt++ + if err := f(); err == nil { + return attempt, nil + } + if delay >= 10*time.Second { + delay = 10 * time.Second + } + select { + case <-time.After(delay): + case <-ctx.Done(): + return attempt, ctx.Err() + } + } +} + // DiffLineMode tells the line if added, removed or unchanged type DiffLineMode rune From 92d851a4c63d614d6d10e419ca460232b891f486 Mon Sep 17 00:00:00 2001 From: waigani Date: Tue, 20 Oct 2020 15:39:26 +1300 Subject: [PATCH 2/2] tweak --- diffparser.go | 1 + 1 file changed, 1 insertion(+) diff --git a/diffparser.go b/diffparser.go index 46c36b3..a82ecf9 100644 --- a/diffparser.go +++ b/diffparser.go @@ -39,6 +39,7 @@ type DiffRange struct { } func retry(ctx context.Context, f func() error) (int, error) { + attempt := 0 for delay := time.Millisecond; ; delay *= 2 { attempt++