Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
satella
Manage
Activity
Members
Labels
Plan
Issues
1
Issue boards
Milestones
Wiki
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
public
satella
Commits
5a367dc1
Commit
5a367dc1
authored
3 years ago
by
Piotr Maślanka
Browse files
Options
Downloads
Patches
Plain Diff
add ExponentialBackoff.launch
parent
6384ec69
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
CHANGELOG.md
+1
-0
1 addition, 0 deletions
CHANGELOG.md
satella/__init__.py
+1
-1
1 addition, 1 deletion
satella/__init__.py
satella/time/backoff.py
+41
-0
41 additions, 0 deletions
satella/time/backoff.py
tests/test_time.py
+16
-0
16 additions, 0 deletions
tests/test_time.py
with
59 additions
and
1 deletion
CHANGELOG.md
+
1
−
0
View file @
5a367dc1
...
...
@@ -2,3 +2,4 @@
*
added automatic enum parsing to satella's JSONEncoder
*
Satella's JSON file handling will internally use Satella's JSONEncoder
*
added ExponentialBackoff.launch
This diff is collapsed.
Click to expand it.
satella/__init__.py
+
1
−
1
View file @
5a367dc1
__version__
=
'
2.17.17a
3
'
__version__
=
'
2.17.17a
4
'
This diff is collapsed.
Click to expand it.
satella/time/backoff.py
+
41
−
0
View file @
5a367dc1
import
time
import
typing
as
tp
from
satella.coding.decorators.decorators
import
wraps
from
satella.coding.typing
import
ExceptionList
from
satella.coding.concurrent.thread
import
Condition
from
.measure
import
measure
from
..exceptions
import
WouldWaitMore
...
...
@@ -124,3 +128,40 @@ class ExponentialBackoff:
self
.
grace_counter
=
0
self
.
unavailable_until
=
None
self
.
condition
.
notify_all
()
def
launch
(
self
,
exceptions_on_failed
:
ExceptionList
=
Exception
):
"""
A decorator to simplify writing doing-something loops. Basically, this:
>>>
eb
=
ExponentialBackoff
(
start
=
2.5
,
limit
=
30
)
>>>
@eb.launch
(
TypeError
)
>>>
def
do_action
(
*
args
,
**
kwargs
):
>>>
x_do_action
(
*
args
,
**
kwargs
)
>>>
do_action
(
5
,
test
=
True
)
is equivalent to this:
>>>
eb
=
ExponentialBackoff
(
start
=
2.5
,
limit
=
30
)
>>>
while
True
:
>>>
try
:
>>>
x_do_action
(
5
,
test
=
True
)
>>>
except
TypeError
:
>>>
eb
.
failed
()
>>>
eb
.
sleep
()
:param exceptions_on_failed: a list of a single exception of exceptions
whose raising will signal that fun has failed
:return: a function, that called, will pass the exactly same parameters
"""
def
outer
(
fun
):
@wraps
(
fun
)
def
inner
(
*
args
,
**
kwargs
):
try
:
r
=
fun
(
*
args
,
**
kwargs
)
self
.
success
()
return
r
except
exceptions_on_failed
:
self
.
failed
()
self
.
sleep
()
return
inner
return
outer
This diff is collapsed.
Click to expand it.
tests/test_time.py
+
16
−
0
View file @
5a367dc1
...
...
@@ -38,6 +38,22 @@ class TestTime(unittest.TestCase):
time
.
sleep
(
1
)
eb
.
success
()
def
test_exponential_backoff_launch
(
self
):
eb
=
ExponentialBackoff
(
start
=
2
,
limit
=
30
)
i
=
1
@eb.launch
(
ValueError
)
def
do_action
():
nonlocal
i
if
i
==
3
:
return
else
:
i
+=
1
raise
ValueError
()
with
measure
()
as
m
:
do_action
()
self
.
assertGreaterEqual
(
m
(),
2
)
def
test_exponential_backoff_waiting_for_service_healthy
(
self
):
eb
=
ExponentialBackoff
(
start
=
2
,
limit
=
30
)
self
.
assertTrue
(
eb
.
ready_for_next_check
)
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment