Coder Social home page Coder Social logo

httpoison_retry's Introduction

HTTPoison Retry

In production, HTTP requests can often fail for random reasons that typically go away if you retry them with enough time inbetween. HTTPoison.Retry automatically handles retrying HTTP requests up to a specified number of times and waiting the specified time.

The design intent behind HTTPoison.Retry was to stay out of the way and allow the developer to add one simple call in between where you fetch the data and where you handle the response. This helps to create a barrier wall where failures are more solidly a failure rather than just a one-time blip that happened for a split second causing a cascade of errors across your system.

Installation

The package can be installed by adding httpoison_retry to your list of dependencies in mix.exs:

def deps do
  [
    {:httpoison_retry, "~> 1.0.0"}
  ]
end

Usage

Simply add a call to autoretry/2 in between your data fetch call and your handling of the response.

HTTPoison.get("https://www.example.com")
# Will retry 5 times waiting 15s between each before returning. Therefore, your process
# could end up waiting up to 75 seconds (plus the request time) on the line below
# Note: below is the same as the defaults
|> autoretry(max_attempts: 5, wait: 15_000, include_404s: false, retry_unknown_errors: false)
# Your function which will handle the response after success or failed retry
|> handle_response()

Gotchas

Don't forget that the autoretry/2 call could take a substaintial amount of time to complete. That means usage in the following areas are potential problems:

  • Within a plug/phoenix request
  • Within any GenServer process
    • Sleeping for a considerable amount of time leaves the process unable to answer other callers and will result in cascading timeouts of other processes
  • Any Task/Agent call that has a timeout period (i.e. Task.await/1)
    • Same reason as GenServers
  • Tests
    • Tests should not be making live HTTP calls anyhow, but if they do this could potentially go over the default 60 seconds in ExUnit.

httpoison_retry's People

Contributors

mgwidmann avatar ncronquist avatar jeffkreeftmeijer avatar gkumbhat avatar

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.