发现经常有”超时“的错误信息,如/usr/lib/ruby/1.8/timeout.rb:54:in `rbuf_fill': execution expired (Timeout::Error),恩,应该是网络不稳定或者是服务器响应太慢的结果,需要捕获下这个异常并做些处理,记录如下:

需要注意的是,Timeout::Error不是StandardError的子类, 而是继承至 Interrupt class,所以捕获的时候,需要格外注意,演示如下:

require 'net/pop3'
begin
Net::POP3.auth_only(@server, @port, @username, @password)
rescue => e
write_error_to_logfile(e)
do_something_sensible
end

看上面的这段代码,当POP3服务器不能及时响应的时候,所触发的异常并不能被下面捕获到,原因就是上面说的,再看正确的处理代码:

require 'net/pop3'
begin
Net::POP3.auth_only(@server, @port, @username, @password)
rescue => e
write_error_to_logfile(e)
do_something_sensible
rescue Timeout::Error => e
write_error_to_logfile(e)
do_something_sensible_for_timeout
end

这段代码可以正常工作,并按照我们的意愿来处理了。

如果您知道对方的服务器会比较慢的响应,或者你知道网络状态不好,你可以单独设置这个TimeOut的时间,代码如下:

require 'timeout'   

...
...
begin
timeout(60) do
resp, body=3Dh.get('/index.html')
puts body
end
rescue TimeoutError
puts "Timed Out"
end

或者这样:(来源:http://textsnippets.com/posts/show/868)

http = Net::HTTP.new(url.host, url.port)
http.read_timeout=time_out 转自 http://hlee.iteye.com/blog/353732

rails timeout 异常的更多相关文章

  1. 使用StackExchange.Redis客户端进行Redis访问出现的Timeout异常排查

    问题产生 这两天业务系统在redis的使用过程中,当并行客户端数量达到200+之后,产生了大量timeout异常,典型的异常信息如下: Timeout performing HVALS Parser2 ...

  2. 504 Gateway Timeout 异常

    生产销售系统出现 504 Gateway Timeout 异常,其实就是服务器响应太慢导致nginx带来超时,先不说服务端慢的优化问题:只是单纯的解决504.到网上发现了一篇文章fix it Add ...

  3. pip install 报SSL异常和timeout异常

    在安装pip3 install virtualenv时报了SSL异常 如图 pip is configured with locations that require TLS/SSL, however ...

  4. RocketMQ的invokeSync call timeout异常的解决办法

    缘起 在RocketMQ客户端的DefaultMQPushConsumer的start方法被执行时,时不时会报出invokeSync call timeout异常,如下: Caused by: jav ...

  5. 关于No ManagedConnections available within configured blocking timeout异常的解决

    最近由于系统和业务重构需要,需要把线上1亿数据迁移到新库,由于业务变更,新表老表结构有变化,没法直接用dba dump的方式,需要自己写转换程序迁移.今天在调试的时候,碰到一个蛋疼的问题,就是一开始查 ...

  6. java读取redis的timeout异常

    http://blog.csdn.net/shuaiokshuai/article/details/23266091 FIFO Fist-in Fisrt-out 先进先出

  7. python中Requests库错误和异常

    主要有以下四种: 1.Requests抛出一个ConnectionError异常,原因为网络问题(如DNS查询失败.拒接连接等错误) 2.Response.raise_for_status()抛出一个 ...

  8. com.mysql.jdbc.exceptions.jdbc4.CommunicationsException/com.atomikos.datasource.ResourceException异常解决

    tomcat+mysql部署,每天早晨第一次访问web项目,出现mysql的连接timeout异常:com.mysql.jdbc.exceptions.jdbc4.CommunicationsExce ...

  9. Jedis timeout

    处理Jedis timeout 异常 Jedis rClient = new Jedis("localhost"); 解决步骤 关闭linux防火墙 systemctl stop ...

随机推荐

  1. Error occurred whiLe getting the data source contents for the report

    Web service request GetDataSourceContents to Report Server http://crm-vm/reportserver/ReportService2 ...

  2. Supervisord进程管理工具

    进程管理工具Supervisord Posted on 2014/06/17 by admin Supervisord 上面已经介绍了Go目前是有两种方案来实现他的daemon,但是官方本身还不支持这 ...

  3. vim -- 查找和替换

    %s/foo/bar/g 在所有行中寻找‘foo’,并且用‘bar’替换 :s/foo/bar/g 在当前行寻找‘foo’,并且用‘foo’替换 :%s/foo/bar/gc 将每一个‘foo',并用 ...

  4. mysql—触发器trigger

    触发器(trigger):一种特殊的事物, 监视某种事物操作(insert/update/delete), 并触发相关操作(insert/update/delete). 触发器(trigger)创建4 ...

  5. Tuning 13 Using oracle blocks Efficiently

    推进使用自动管理 automatic segment 1 个 Blocks = 2的幂次方倍 tablespace 像一块地 segment 像一个房子 extents 向一个装砖头的框 blocks ...

  6. 从git中更新本地需要填写的正则

    <IfModule mod_rewrite.c> Options +FollowSymlinks RewriteEngine On RewriteCond %{REQUEST_FILENA ...

  7. String类和StringBuffer类

    位于java.lang包中,这个包中的类使用时不用导入 String类一旦初始化就不可以改变,而stringbuffer则可以.它用于封装内容可变的字符串.它可以使用tostring()转换成stri ...

  8. mybatis 处理 mysql 表中的 text类型的 字段

    在mysql 中 text类型的字段: service_detail text NULL 服务描述   . 对应java文件中 model 中的 String:  private String ser ...

  9. AderTemplate

    http://www.cnblogs.com/kwklover/archive/2007/07/12/815509.html 概述 AderTemplate是一个小型的模板引擎.无论是拿来直接使用还是 ...

  10. mstsc远程登录设置

    mstsc终于可以连上了, 1.系统属性 远程允许, 2.开启三个服务: Remote Desktop ConfigurationRemote Desktop ServicesRemote Deskt ...