使用 Ruby Net 实施重新连接策略

发布于 2024-07-30 10:25:38 字数 600 浏览 9 评论 0原文

我正在开发一个小型应用程序,它将 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)}

非常感谢,始终感谢您的支持。

马特

I'm developing a small application which posts XML to some webservice.
This is done using Net::HTTP::Post::Post. However, the service provider recommends using a re-connect.

Something like:
1st request fails -> try again after 2 seconds
2nd request fails -> try again after 5 seconds
3rd request fails -> try again after 10 seconds
...

What would be a good approach to do that? Simply running the following piece of code in a loop, catching the exception and run it again after an amount of time? Or is there any other clever way to do that? Maybe the Net package even has some built in functionality that I'm not aware of?

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)}

Thanks very much, always appreciate your support.

Matt

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

倚栏听风 2024-08-06 10:25:38

这是 Ruby 的 retry 派上用场的罕见情况之一。 沿着这些思路:

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

This is one of the rare occasions when Ruby's retry comes in handy. Something along these lines:

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
高跟鞋的旋律 2024-08-06 10:25:38

我使用 gem retryable 进行重试。
有了它,代码从:

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

到:

retryable( :tries => 10, :on => [SomeException] ) do
  response = Net::HTTP.start(url.host, url.port) {|http| http.request(request)}
end

I use gem retryable for retry.
With it code transformed from:

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

To:

retryable( :tries => 10, :on => [SomeException] ) do
  response = Net::HTTP.start(url.host, url.port) {|http| http.request(request)}
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文