websocket-rails: Websockets causing a Redis ECONNREFUSED error

I am getting a curious connection refused error from Redis that causes the server to fail.

/Users/gavin/.rvm/gems/ruby-2.2.1@kugadi/gems/redis-3.2.1/lib/redis/client.rb:331:in `rescue in establish_connection': Error connecting to Redis on 127.0.0.1:6379 (Errno::ECONNREFUSED) (Redis::CannotConnectError)
Exiting
    from /Users/gavin/.rvm/gems/ruby-2.2.1@kugadi/gems/redis-3.2.1/lib/redis/client.rb:317:in `establish_connection'
    from /Users/gavin/.rvm/gems/ruby-2.2.1@kugadi/gems/redis-3.2.1/lib/redis/client.rb:94:in `block in connect'
    from /Users/gavin/.rvm/gems/ruby-2.2.1@kugadi/gems/redis-3.2.1/lib/redis/client.rb:279:in `with_reconnect'
    from /Users/gavin/.rvm/gems/ruby-2.2.1@kugadi/gems/redis-3.2.1/lib/redis/client.rb:93:in `connect'
    from /Users/gavin/.rvm/gems/ruby-2.2.1@kugadi/gems/redis-3.2.1/lib/redis/client.rb:350:in `ensure_connected'
    from /Users/gavin/.rvm/gems/ruby-2.2.1@kugadi/gems/redis-3.2.1/lib/redis/client.rb:207:in `block in process'
    from /Users/gavin/.rvm/gems/ruby-2.2.1@kugadi/gems/redis-3.2.1/lib/redis/client.rb:292:in `logging'
    from /Users/gavin/.rvm/gems/ruby-2.2.1@kugadi/gems/redis-3.2.1/lib/redis/client.rb:206:in `process'
    from /Users/gavin/.rvm/gems/ruby-2.2.1@kugadi/gems/redis-3.2.1/lib/redis/client.rb:112:in `call'
    from /Users/gavin/.rvm/gems/ruby-2.2.1@kugadi/gems/redis-3.2.1/lib/redis.rb:1901:in `block in hget'
    from /Users/gavin/.rvm/gems/ruby-2.2.1@kugadi/gems/redis-3.2.1/lib/redis.rb:37:in `block in synchronize'
    from /Users/gavin/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/monitor.rb:211:in `mon_synchronize'
    from /Users/gavin/.rvm/gems/ruby-2.2.1@kugadi/gems/redis-3.2.1/lib/redis.rb:37:in `synchronize'
    from /Users/gavin/.rvm/gems/ruby-2.2.1@kugadi/gems/redis-3.2.1/lib/redis.rb:1900:in `hget'
    from /Users/gavin/.rvm/gems/ruby-2.2.1@kugadi/gems/redis-objects-1.1.0/lib/redis/hash_key.rb:27:in `hget'
    from /Users/gavin/.rvm/gems/ruby-2.2.1@kugadi/gems/websocket-rails-0.7.0/lib/websocket_rails/channel.rb:58:in `token'
    from /Users/gavin/.rvm/gems/ruby-2.2.1@kugadi/gems/websocket-rails-0.7.0/lib/websocket_rails/channel.rb:31:in `trigger'
    from /Users/gavin/RubymineProjects/overton/app/models/geographic_fix.rb:15:in `block in send_to_websocket'

Process finished with exit code 1

I have put Redis in debug mode and I can see the connections. I have redis-cli running in monitor mode which is one connection. I then get 2 new ones for the stand alone server and some more for the main server. Here is an extract from the redis log.

82377:M 27 Apr 13:15:58.618 - Accepted 127.0.0.1:56393
82377:M 27 Apr 13:16:02.562 - DB 0: 3 keys (0 volatile) in 4 slots HT.
82377:M 27 Apr 13:16:02.563 - 1 clients connected (0 slaves), 977856 bytes in use
82377:M 27 Apr 13:16:11.106 - DB 0: 3 keys (0 volatile) in 4 slots HT.
82377:M 27 Apr 13:16:11.107 - 1 clients connected (0 slaves), 977856 bytes in use
82377:M 27 Apr 13:16:19.695 - DB 0: 3 keys (0 volatile) in 4 slots HT.
82377:M 27 Apr 13:16:19.696 - 1 clients connected (0 slaves), 977856 bytes in use
82377:M 27 Apr 13:16:28.297 - DB 0: 3 keys (0 volatile) in 4 slots HT.
82377:M 27 Apr 13:16:28.297 - 1 clients connected (0 slaves), 977856 bytes in use
82377:M 27 Apr 13:16:36.820 - DB 0: 3 keys (0 volatile) in 4 slots HT.
82377:M 27 Apr 13:16:36.820 - 1 clients connected (0 slaves), 977856 bytes in use
82377:M 27 Apr 13:16:45.455 - DB 0: 3 keys (0 volatile) in 4 slots HT.
82377:M 27 Apr 13:16:45.455 - 1 clients connected (0 slaves), 977856 bytes in use
82377:M 27 Apr 13:16:46.848 - Accepted 127.0.0.1:56395
82377:M 27 Apr 13:16:46.874 - Accepted 127.0.0.1:56396
82377:M 27 Apr 13:16:53.891 - DB 0: 3 keys (0 volatile) in 4 slots HT.
82377:M 27 Apr 13:16:53.891 - 3 clients connected (0 slaves), 1012832 bytes in use
82377:M 27 Apr 13:17:02.504 - DB 0: 3 keys (0 volatile) in 4 slots HT.
82377:M 27 Apr 13:17:02.504 - 3 clients connected (0 slaves), 1012832 bytes in use
82377:M 27 Apr 13:17:11.100 - DB 0: 3 keys (0 volatile) in 4 slots HT.
82377:M 27 Apr 13:17:11.100 - 3 clients connected (0 slaves), 1012832 bytes in use
82377:M 27 Apr 13:17:13.846 - Accepted 127.0.0.1:56397
82377:M 27 Apr 13:17:13.847 - Accepted 127.0.0.1:56398
82377:M 27 Apr 13:17:19.651 - DB 0: 3 keys (0 volatile) in 4 slots HT.
82377:M 27 Apr 13:17:19.651 - 5 clients connected (0 slaves), 1047728 bytes in use
82377:M 27 Apr 13:17:28.254 - DB 0: 3 keys (0 volatile) in 4 slots HT.
82377:M 27 Apr 13:17:28.254 - 5 clients connected (0 slaves), 1047728 bytes in use
82377:M 27 Apr 13:17:36.871 - DB 0: 3 keys (0 volatile) in 4 slots HT.
82377:M 27 Apr 13:17:36.871 - 5 clients connected (0 slaves), 1047728 bytes in use
82377:M 27 Apr 13:17:45.502 - DB 0: 3 keys (0 volatile) in 4 slots HT.
82377:M 27 Apr 13:17:45.502 - 5 clients connected (0 slaves), 1047728 bytes in use
82377:M 27 Apr 13:17:53.004 - Accepted 127.0.0.1:56401
82377:M 27 Apr 13:17:53.104 - Client closed connection
82377:M 27 Apr 13:17:53.128 - Accepted 127.0.0.1:56402
82377:M 27 Apr 13:17:53.172 - Client closed connection
82377:M 27 Apr 13:17:53.172 - Accepted 127.0.0.1:56403
82377:M 27 Apr 13:17:53.173 - Client closed connection
82377:M 27 Apr 13:17:53.173 - Client closed connection
82377:M 27 Apr 13:17:53.173 - Accepted 127.0.0.1:56404
82377:M 27 Apr 13:17:53.193 - Client closed connection
82377:M 27 Apr 13:17:53.255 - Client closed connection
82377:M 27 Apr 13:17:53.936 - DB 0: 3 keys (0 volatile) in 4 slots HT.
82377:M 27 Apr 13:17:53.936 - 3 clients connected (0 slaves), 1012832 bytes in use
82377:M 27 Apr 13:18:02.399 - DB 0: 3 keys (0 volatile) in 4 slots HT.
82377:M 27 Apr 13:18:02.399 - 3 clients connected (0 slaves), 1012832 bytes in use

Here is the Redis monitor ouput

Gavins-MBP:~ gavin$ redis-cli monitor
OK
1430155006.874010 [0 127.0.0.1:56395] "sismember" "websocket_rails.active_servers" "0EEXNlgIKgLkvJ2zNearKw"
1430155006.874465 [0 127.0.0.1:56395] "sadd" "websocket_rails.active_servers" "0EEXNlgIKgLkvJ2zNearKw"
1430155006.895314 [0 127.0.0.1:56396] "subscribe" "websocket_rails.events"
1430155033.846412 [0 127.0.0.1:56397] "sismember" "websocket_rails.active_servers" "MLVoGmT94bm2_smOSk56LQ"
1430155033.846744 [0 127.0.0.1:56397] "sadd" "websocket_rails.active_servers" "MLVoGmT94bm2_smOSk56LQ"
1430155033.847751 [0 127.0.0.1:56398] "subscribe" "websocket_rails.events"
1430155073.171663 [0 127.0.0.1:56402] "srem" "websocket_rails.active_servers" "MLVoGmT94bm2_smOSk56LQ"
^C
Gavins-MBP:~ gavin$ 

My configuration is

WebsocketRails.setup do |config|

  # Uncomment to override the default log level. The log level can be
  # any of the standard Logger log levels. By default it will mirror the
  # current Rails environment log level.
  # config.log_level = :debug

  # Uncomment to change the default log file path.
  # config.log_path = "#{Rails.root}/log/websocket_rails.log"

  # Set to true if you wish to log the internal websocket_rails events
  # such as the keepalive `websocket_rails.ping` event.
  # config.log_internal_events = false

  # Change to true to enable standalone server mode
  # Start the standalone server with rake websocket_rails:start_server
  # * Requires Redis
  config.standalone = true

  # Change to true to enable channel synchronization between
  # multiple server instances.
  # * Requires Redis.
  config.synchronize = false

  # Uncomment and edit to point to a different redis instance.
  # Will not be used unless standalone or synchronization mode
  # is enabled.
  # config.redis_options = {:host => 'localhost', :port => '6379'}
  # config.redis_options = { driver: :ruby }

  # By default, all subscribers in to a channel will be removed
  # when that channel is made private. If you don't wish active
  # subscribers to be removed from a previously public channel
  # when making it private, set the following to true.
  # config.keep_subscribers_when_private = false

  # Used as the key for the WebsocketRails.users Hash. This method
  # will be called on the `current_user` object in your controller
  # if one exists. If `current_user` does not exist or does not
  # respond to the identifier, the key will default to `connection.id`
  # config.user_identifier = :id

  # Uncomment and change this option to override the class associated
  # with your `current_user` object. This class will be used when
  # synchronization is enabled and you trigger events from background
  # jobs using the WebsocketRails.users UserManager.
  # config.user_class = User

end

Any idea why this might be happening?

About this issue

  • Original URL
  • State: closed
  • Created 9 years ago
  • Comments: 20 (9 by maintainers)

Commits related to this issue

Most upvoted comments

@MhdSyrwan - I managed to solve the problem for me. What I did is add the following monkey patch to the websocket_rails.rb initializer in my rails app:

$global_redis = Redis.new

module WebsocketRails
  class Synchronization
    def redis
      @redis = $global_redis
    end

    def ruby_redis
      @ruby_redis = $global_redis
    end
  end
end

Not the most perfect solution but it works. An improvement would be to add re-connection if the connection to the global redis is dropped - But I have been working with the above configuration in production for about a week and no problems so far.

My environment is: Rails 3.2 running Thin webserver with 6 instances. Websockets configuration is: config.standalone = false config.synchronize = true