-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #433 from lstocchi/i432
win-sshproxy.tid created before thread id is available
- Loading branch information
Showing
4 changed files
with
93 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package utils | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"time" | ||
|
||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
const maxRetries = 60 | ||
const initialBackoff = 100 * time.Millisecond | ||
|
||
func Retry[T comparable](ctx context.Context, retryFunc func() (T, error), retryMsg string) (T, error) { | ||
var ( | ||
returnVal T | ||
err error | ||
) | ||
|
||
backoff := initialBackoff | ||
|
||
loop: | ||
for i := 0; i < maxRetries; i++ { | ||
select { | ||
case <-ctx.Done(): | ||
break loop | ||
default: | ||
// proceed | ||
} | ||
|
||
returnVal, err = retryFunc() | ||
if err == nil { | ||
return returnVal, nil | ||
} | ||
logrus.Debugf("%s (%s)", retryMsg, backoff) | ||
Sleep(ctx, backoff) | ||
backoff = backOff(backoff) | ||
} | ||
return returnVal, fmt.Errorf("timeout: %w", err) | ||
} | ||
|
||
func backOff(delay time.Duration) time.Duration { | ||
if delay == 0 { | ||
delay = 5 * time.Millisecond | ||
} else { | ||
delay *= 2 | ||
} | ||
if delay > time.Second { | ||
delay = time.Second | ||
} | ||
return delay | ||
} | ||
|
||
func Sleep(ctx context.Context, wait time.Duration) bool { | ||
select { | ||
case <-ctx.Done(): | ||
return false | ||
case <-time.After(wait): | ||
return true | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters