开发者

A use case for retry function?

开发者 https://www.devze.com 2023-04-09 02:05 出处:网络
I read this snippet, and I am trying to understand how I can use retry, and I am unable to think of a use. How are others using it?

I read this snippet, and I am trying to understand how I can use retry, and I am unable to think of a use. How are others using it?

#!开发者_Go百科/usr/bin/ruby

for i in 1..5
    retry if  i > 2
    puts "Value of local variable is #{i}"
end


There are several use cases. Here's one from the Programming Ruby book

@esmtp = true

begin
  # First try an extended login. If it fails because the
  # server doesn't support it, fall back to a normal login

  if @esmtp then
    @command.ehlo(helodom)
  else
    @command.helo(helodom)
  end

rescue ProtocolError
  if @esmtp then
    @esmtp = false
    retry
  else
    raise
  end
end

An other common case is the email delivery. You might want to retry the SMTP delivery for N times adding a sleep between each retry to avoid temporary issues caused by network connectivity.


I am using it for a module that makes an api call to a 3rd party web api, and so if it fails, I retry 2 more times.

0

精彩评论

暂无评论...
验证码 换一张
取 消