Programming - - By Tim Lucas

Timing out and retrying calls to third parties

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…

Sponsors