2015-11-16 57 views
2

這可能看起來像一個愚蠢的問題,但我認爲StackOverflow上的某個人可能對此有一些想法。到底是什麼,對吧?如何防止Redis :: TimeoutError:連接在Heroku上超時

我使用Heroku工作人員與1X Dynos運行Resque。有時我得到這個錯誤:Redis::TimeoutError: Connection timed out。它發生在redis寶石;這裏的堆棧跟蹤:

Redis::TimeoutError Connection timed out 
    vendor/bundle/ruby/2.1.0/gems/redis-3.2.0/lib/redis/connection/ruby.rb:55 rescue in _read_from_socket 
    vendor/bundle/ruby/2.1.0/gems/redis-3.2.0/lib/redis/connection/ruby.rb:48 _read_from_socket 
    vendor/bundle/ruby/2.1.0/gems/redis-3.2.0/lib/redis/connection/ruby.rb:41 gets 
    vendor/bundle/ruby/2.1.0/gems/redis-3.2.0/lib/redis/connection/ruby.rb:273 read 
    vendor/bundle/ruby/2.1.0/gems/redis-3.2.0/lib/redis/client.rb:245 block in read 
    vendor/bundle/ruby/2.1.0/gems/redis-3.2.0/lib/redis/client.rb:233 io 
    vendor/bundle/ruby/2.1.0/gems/redis-3.2.0/lib/redis/client.rb:244 read 
    vendor/bundle/ruby/2.1.0/gems/redis-3.2.0/lib/redis/client.rb:175 block in call_pipelined 
    vendor/bundle/ruby/2.1.0/gems/redis-3.2.0/lib/redis/client.rb:214 block (2 levels) in process 
    vendor/bundle/ruby/2.1.0/gems/redis-3.2.0/lib/redis/client.rb:340 ensure_connected 
    vendor/bundle/ruby/2.1.0/gems/redis-3.2.0/lib/redis/client.rb:204 block in process 
    vendor/bundle/ruby/2.1.0/gems/redis-3.2.0/lib/redis/client.rb:286 logging 
    vendor/bundle/ruby/2.1.0/gems/redis-3.2.0/lib/redis/client.rb:203 process 
    vendor/bundle/ruby/2.1.0/gems/redis-3.2.0/lib/redis/client.rb:174 call_pipelined 
    vendor/bundle/ruby/2.1.0/gems/redis-3.2.0/lib/redis/client.rb:146 block in call_pipeline 
    vendor/bundle/ruby/2.1.0/gems/redis-3.2.0/lib/redis/client.rb:273 with_reconnect 
    vendor/bundle/ruby/2.1.0/gems/redis-3.2.0/lib/redis/client.rb:144 call_pipeline 
    vendor/bundle/ruby/2.1.0/gems/redis-3.2.0/lib/redis.rb:2101 block in pipelined 
    vendor/bundle/ruby/2.1.0/gems/redis-3.2.0/lib/redis.rb:37 block in synchronize 
    vendor/ruby-2.1.5/lib/ruby/2.1.0/monitor.rb:211 mon_synchronize 
    vendor/bundle/ruby/2.1.0/gems/redis-3.2.0/lib/redis.rb:37 synchronize 
    vendor/bundle/ruby/2.1.0/gems/redis-3.2.0/lib/redis.rb:2097 pipelined 
    vendor/bundle/ruby/2.1.0/gems/redis-namespace-1.5.1/lib/redis/namespace.rb:413 namespaced_block 
    vendor/bundle/ruby/2.1.0/gems/redis-namespace-1.5.1/lib/redis/namespace.rb:265 pipelined 
    vendor/bundle/ruby/2.1.0/gems/resque-1.25.2/lib/resque.rb:214 push 
    vendor/bundle/ruby/2.1.0/gems/resque-1.25.2/lib/resque/job.rb:153 create 
    vendor/bundle/ruby/2.1.0/gems/resque_solo-0.1.0/lib/resque_ext/job.rb:7 create_solo 
    vendor/bundle/ruby/2.1.0/gems/resque-1.25.2/lib/resque.rb:317 enqueue_to 
    vendor/bundle/ruby/2.1.0/gems/resque-1.25.2/lib/resque.rb:298 enqueue 

我們有resque-retry成立,但我想如果enqueue調用甚至無法連接到Redis的也沒關係。

我目前的解決方案是每包呼Resque.enqueuebegin/rescue,使我們可以重新嘗試enqueue調用(按https://github.com/resque/resque/issues/840)。但是沒有更好的方法嗎?

回答

1

Heroku Redis允許您更改實例超時和maxmemory-policy設置。這些設置將保留在升級和HA故障切換中。

documentation

The timeout setting sets the number of seconds Redis waits before killing idle connections. A value of zero means that connections will not be closed. The default value is 300 seconds (5 minutes). You can change this value using the CLI:

$ heroku redis:timeout maturing-deeply-2628 --seconds 60 

Timeout for maturing-deeply-2628 (REDIS_URL) set to 60 seconds. Connections to the redis instance will be stopped after idling for 60 seconds.

貌似在resque情況下0(不使用連接的time out)將是最好的choise(--seconds 0)。