Comments (4)
Do you mean some kind of API that a 3rd party library could could expose to its clients to transparently provide backoff's functionality?
from backoff.
I believe so. To rephrase myself, I'd like to use a library like this when building an API client library. In that library, I'd like to expose the option to end users of using exponential backoff without their need to implement that. I was hoping some form of conditional backoff (where the user could set backoff=true) or (backoff=false) and then either have the backoff functionality enabled or disabled based on that decision.
Does that make sense?
from backoff.
Yes. It's an interesting thought. I think like you might end up wanting to expose more configuration options than just True or False though. Maybe you'd want to mirror some subset of backoff's options as kwargs in your library? I'd have to think about this more.
from backoff.
Let's say you have a class with a do_stuff
method:
class MyClass:
def __init__(self):
pass
def do_stuff(self):
print("do stuff")
If you want to implement conditional backoff to this method, you can
add a parameter to the __init__
method which takes either a boolean
or a full backoff decorator.
If you do that, by default the retries are enabled (or disabled)
with the boolean but allows the user to define a completely
specific backoff profile as well.
You rename your do_stuff
method to _do_stuff_once
and create
the do_stuff
method in the __init__
with either a default backoff
decorator or the decorator provided by the user.
_CallableT = TypeVar("_CallableT", bound=Callable[..., Any])
_Decorator = Callable[[_CallableT], _CallableT]
class MyClass:
def __init__(
self,
retry: Union[bool, _Decorator] = True,
):
"""
:param retry: Either a Boolean to activate/deactivate the retries OR
a backoff decorator to provide specific retries parameters.
"""
if retry is True:
# By default, retry repeatedly, with maximum 60 seconds between retries
retry_decorator = backoff.on_exception(
backoff.expo,
Exception,
max_value=60,
)
elif retry is False:
retry_decorator = lambda e: e
else:
assert callable(retry)
retry_decorator = retry
# Creating the do_stuff method with the defined backoff decorator
self.do_stuff = retry_decorator(self._do_stuff_once)
def _do_stuff_once(self):
print("do stuff")
If you do that, the user has complete control over the type of backoff profile:
# Default usage: retries with your default backoff profile
m = MyClass()
# User disabled the retries
m = MyClass(retry=False)
# User provides a custom backoff decorator
retry_decorator=backoff.on_exception(
backoff.expo,
Exception,
max_value=300,
)
m = MyClass(retry=retry_decorator)
This is the approach I used in graphql-python/gql PR #324
from backoff.
Related Issues (20)
- 2.2.0 release gives type errors for previously valid usage? HOT 1
- Max tries
- Duplicate logging in some special cases HOT 1
- How do I use this?
- Obtain value returned by backoff_handler function after maximum tries is reached
- max_retries HOT 1
- max_time only applies _after_ first retry?
- Canonical way to handle error bursting
- Add exponential decay
- How to handle backoff on Facebook cursor? HOT 1
- Improve Typing and Error Handling in `on_exception`
- question about max_time
- Feature Request: add a way to use backoff in a context manager HOT 2
- feature request: toggle decorator on/off
- `backoff.on_predicate` does not work for `staticmethod`s
- Expose typing hints as they are part of the API
- Add support for globally disabling retries
- How to provide arguments to `on_backoff` function HOT 1
- Feature Request: support direct invocation with functools.partial
- Maintenance help? HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from backoff.