Git Product home page Git Product logo

Comments (4)

bgreen-litl avatar bgreen-litl commented on May 22, 2024

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.

kkirsche avatar kkirsche commented on May 22, 2024

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.

bgreen-litl avatar bgreen-litl commented on May 22, 2024

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.

leszekhanusz avatar leszekhanusz commented on May 22, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.