GithubHelp home page GithubHelp logo

Comments (14)

luckydonald avatar luckydonald commented on August 23, 2024

Like the @on_predicate lambda

from backoff.

luckydonald avatar luckydonald commented on August 23, 2024

Actual really dirty workaround:

class DoRetryException(BaseException):
    """
    Used for backoff strategy
    """
    pass
# end class DoRetryException


def should_backoff(e):
    """
    Used for backoff strategy
    """
    if e.error_code == 429 or "too many requests" in e.description or "retry later" in e.description:
        raise DoRetryException()
    # end if
# end def



class DocumentMessage():
    # ...
    # ...

    @backoff.on_exception(backoff.expo, DoRetryException, max_tries=10, jitter=None)
    def send(self, sender: PytgbotApiBot, receiver, reply_id)->PytgbotApiMessage:
        try:
            return sender.send_document(
                receiver, self.file, caption=self.caption, reply_to_message_id=reply_id, reply_markup=self.reply_markup,
                disable_notification=self.disable_notification
            )
        except TgApiServerException as e:
            should_backoff(e)  # checks if it should raise an DoRetryException
            raise  # else it just raises as usual
        # end try
    # end def send

from backoff.

bgreen-litl avatar bgreen-litl commented on August 23, 2024

I think this is basically the same as #8 ... I'm still thinking about the best way to accommodate it. Does the keyword arg solution I put there look like it would work for you?

from backoff.

bgreen-litl avatar bgreen-litl commented on August 23, 2024

The idea is to add a keyword arg which takes a function which tests the raised exception.

    def is_fatal(e):
        return e.error_number == 42


    @backoff.on_exception(backoff.expo, SomeException,
                          giveup=is_fatal)
    def foobar():
        error_number = random.choice([42, 4458])
        raise SomeException(error_number)

How does this look to you?

from backoff.

bgreen-litl avatar bgreen-litl commented on August 23, 2024

@luckydonald This PR adds a giveup kwarg to on_exception: #13

from backoff.

luckydonald avatar luckydonald commented on August 23, 2024

Looks like the missing part :D

from backoff.

luckydonald avatar luckydonald commented on August 23, 2024

Should giveup(e) be checked for raising exceptions itself, so another backoff round starts? Or should that just fail?

from backoff.

bgreen-litl avatar bgreen-litl commented on August 23, 2024

If there's an exception raised in your giveup function I believe that should be allowed to escape.

from backoff.

luckydonald avatar luckydonald commented on August 23, 2024

Yep, probably. Else errors there would be unnoticed.

from backoff.

luckydonald avatar luckydonald commented on August 23, 2024

When will there be a new pip package? or did I miss that already?

from backoff.

bgreen-litl avatar bgreen-litl commented on August 23, 2024

Yes, this will be released and distributed on pypi as part of 1.3

from backoff.

bgreen-litl avatar bgreen-litl commented on August 23, 2024

backoff==1.3.1 is now released

from backoff.

luckydonald avatar luckydonald commented on August 23, 2024

Great!

from backoff.

luckydonald avatar luckydonald commented on August 23, 2024

#11 (comment)
In your code you say @backoff.on_exception(...): where the : is wrong.

Also could you please syntax highlight to your code (here and readme) as python by using ```python?

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.