Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
N
nebula
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
exchain
nebula
Commits
6e1a059a
Commit
6e1a059a
authored
Aug 15, 2023
by
Joshua Gutow
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
op-service: Cleanup backoff.Do implementation
parent
b9238afd
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
20 additions
and
26 deletions
+20
-26
operation.go
op-service/backoff/operation.go
+20
-26
No files found.
op-service/backoff/operation.go
View file @
6e1a059a
...
...
@@ -17,6 +17,10 @@ func (e *ErrFailedPermanently) Error() string {
return
fmt
.
Sprintf
(
"operation failed permanently after %d attempts: %v"
,
e
.
attempts
,
e
.
LastErr
)
}
func
(
e
*
ErrFailedPermanently
)
Unwrap
()
error
{
return
e
.
LastErr
}
type
pair
[
T
,
U
any
]
struct
{
a
T
b
U
...
...
@@ -35,37 +39,27 @@ func Do2[T, U any](ctx context.Context, maxAttempts int, strategy Strategy, op f
// with delays in between each retry according to the provided
// Strategy.
func
Do
[
T
any
](
ctx
context
.
Context
,
maxAttempts
int
,
strategy
Strategy
,
op
func
()
(
T
,
error
))
(
T
,
error
)
{
var
empty
T
var
empty
,
ret
T
var
err
error
if
maxAttempts
<
1
{
return
empty
,
fmt
.
Errorf
(
"need at least 1 attempt to run op, but have %d max attempts"
,
maxAttempts
)
}
var
attempt
int
reattemptCh
:=
make
(
chan
struct
{},
1
)
doReattempt
:=
func
()
{
reattemptCh
<-
struct
{}{}
}
doReattempt
()
for
{
select
{
case
<-
ctx
.
Done
()
:
for
i
:=
0
;
i
<
maxAttempts
;
i
++
{
if
ctx
.
Err
()
!=
nil
{
return
empty
,
ctx
.
Err
()
case
<-
reattemptCh
:
attempt
++
ret
,
err
:=
op
()
if
err
==
nil
{
return
ret
,
nil
}
if
attempt
==
maxAttempts
{
return
empty
,
&
ErrFailedPermanently
{
attempts
:
maxAttempts
,
LastErr
:
err
,
}
}
time
.
AfterFunc
(
strategy
.
Duration
(
attempt
-
1
),
doReattempt
)
}
ret
,
err
=
op
()
if
err
==
nil
{
return
ret
,
nil
}
// Don't sleep when we are about to exit the loop & return ErrFailedPermanently
if
i
!=
maxAttempts
-
1
{
time
.
Sleep
(
strategy
.
Duration
(
i
))
}
}
return
empty
,
&
ErrFailedPermanently
{
attempts
:
maxAttempts
,
LastErr
:
err
,
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment