Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions diffparser.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
package diffparser

import (
"context"
"regexp"
"strconv"
"strings"
"time"

"errors"
)
Expand Down Expand Up @@ -36,6 +38,25 @@ 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):
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make one ticker as opposed to making a new timer each iteration.

View Rule

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@codelingo It'd be helpful if the comment explained a bit more of the why here: you're essentially avoiding a mem leak as a new timer object is created on each iteration.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make one ticker as opposed to making a new timer each iteration.

View Rule

case <-ctx.Done():
return attempt, ctx.Err()
}
}
}

// DiffLineMode tells the line if added, removed or unchanged
type DiffLineMode rune

Expand Down