Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用Ruby-Net实现重连接策略_Ruby_Webservice Client_Reconnect - Fatal编程技术网

用Ruby-Net实现重连接策略

用Ruby-Net实现重连接策略,ruby,webservice-client,reconnect,Ruby,Webservice Client,Reconnect,我正在开发一个小应用程序,它将XML发布到一些Web服务。 这是使用Net::HTTP::Post::Post完成的。但是,服务提供商建议使用重新连接 比如: 第一次请求失败->2秒后重试 第二次请求失败->5秒后重试 第三次请求失败->10秒后重试 有什么好方法可以做到这一点?只是在循环中运行下面的代码,捕获异常并在一段时间后再次运行它?或者还有其他聪明的方法吗?也许Net软件包甚至有一些我不知道的内置功能 url = URI.parse("http://some.host") reque

我正在开发一个小应用程序,它将XML发布到一些Web服务。 这是使用Net::HTTP::Post::Post完成的。但是,服务提供商建议使用重新连接

比如: 第一次请求失败->2秒后重试 第二次请求失败->5秒后重试 第三次请求失败->10秒后重试

有什么好方法可以做到这一点?只是在循环中运行下面的代码,捕获异常并在一段时间后再次运行它?或者还有其他聪明的方法吗?也许Net软件包甚至有一些我不知道的内置功能

url = URI.parse("http://some.host")

request = Net::HTTP::Post.new(url.path)

request.body = xml

request.content_type = "text/xml"


#run this line in a loop??
response = Net::HTTP.start(url.host, url.port) {|http| http.request(request)}
非常感谢,非常感谢您的支持


Matt

这是Ruby的
重试功能派上用场的罕见情况之一。大致如下:

retries = [3, 5, 10]
begin 
  response = Net::HTTP.start(url.host, url.port) {|http| http.request(request)}
rescue SomeException # I'm too lazy to look it up
  if delay = retries.shift # will be nil if the list is empty
    sleep delay
    retry # backs up to just after the "begin"
  else
    raise # with no args re-raises original error
  end
end
我使用gem进行重试。 随着it的发展,代码从:

retries = [3, 5, 10]
begin 
  response = Net::HTTP.start(url.host, url.port) {|http| http.request(request)}
rescue SomeException # I'm too lazy to look it up
  if delay = retries.shift # will be nil if the list is empty
    sleep delay
    retry # backs up to just after the "begin"
  else
    raise # with no args re-raises original error
  end
end
致:


它们是不相等的:首先是做4个延迟0,3,5,10的平局;第二种方法是延迟1秒进行10次尝试。谢谢。顺便说一句,
SomeException
必须是
StandardError
,cf:。不太好,但至少它的范围是一行,如果它是一个非瞬时的实际错误,就不会被吞没。
retryable( :tries => 10, :on => [SomeException] ) do
  response = Net::HTTP.start(url.host, url.port) {|http| http.request(request)}
end