Hacker News new | comments | ask | show | jobs | submit login
Backoff: Python function decorators for configurable backoff and retry (github.com)
49 points by mocko 10 months ago | hide | past | web | favorite | 3 comments

Seems very similar to the riprova library. I've found it very useful for asyncio web scrapers.

http://riprova.readthedocs.io/en/latest/ https://github.com/h2non/riprova

Cool. If I were maintaining build/deploy nonsense written using Python, this could be quite helpful. A lot of the build/deploy stuff I maintain is -alas- written as Groovy scripts for Jenkins, and I've hand-rolled a similar, simpler wrapper construct. It started up being used to place, and now it's probably used in a dozen places throughout various build, test and deploy scripts.


    sh "essential_but_unreliable_thing --failure-probability 0.01"

  def retry_unreliable(int max_attempts, Closure func) {
    def error = null
    assert max_attempts > 0
    def delay_initial = 8.0f
    def delay_multiplier = 2.0f
    def delay = delay_initial
    for (attempt = 0; attempt < max_attempts; attempt++) {
      if (error) {
        delay = delay_multiplier * delay
      try {
        error = null
      } catch (e) {
        error = e
        println("Unreliable operation failed during attempt ${attempt+1}/${max_attempts}:")
    if (error) {
      println("Unreliable operation failed repeatedly for ${max_attempts}/${max_attempts} attempts. Gave up")
      throw error
Sometimes doing something like this isn't necessarily the right course of action -- e.g. if an external service you depend on is struggling to reply to your requests because of load, hammering it with additional requests when it starts to fail isn't the right solution. But quite a lot of time ladling on stupid retry logic turns an irritating rare failure case into a non-issue.

Applications are open for YC Summer 2019

Guidelines | FAQ | Support | API | Security | Lists | Bookmarklet | Legal | Apply to YC | Contact