Programming
Article
By Tim Lucas

Timing out and retrying calls to third parties

By Tim Lucas
Help us help you! You'll get a... FREE 6-Month Subscription to SitePoint Premium Plus you'll go in the draw to WIN a new Macbook SitePoint 2017 Survey Yes, let's Do this It only takes 5 min

When calling third parties you want to set them a sensible timeout, and you also might want to make a few attempts before giving up. Here’s a relatively succinct way to accomplish this in Ruby (3 attempts, each with a timeout of 5 seconds):


3.times do
  begin
    Timeout::timeout(5) do
      # Call your third party (for a example, a payment gateway)
    end
    @success = true
    break
  rescue Exception
    # Try again!
  end
end

if @success
  # Jump for joy!
end

I’m sure somebody will chime in with an even sexier way…

Login or Create Account to Comment
Login Create Account
Recommended
Sponsors
Get the most important and interesting stories in tech. Straight to your inbox, daily.Is it good?