Compare commits
19 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 3cc18f07ee | |||
| 5d2c20534b | |||
| a0b87ae6a0 | |||
| 0eefa4fbf2 | |||
| 392aac9ea1 | |||
| f1a11503ea | |||
| 376ac9f022 | |||
| 031cbc6dd3 | |||
| 54847de3cd | |||
| da99aa13bd | |||
| c10b0743e4 | |||
| 47aa08b02a | |||
| 69ef5f8be2 | |||
| a2a76ce200 | |||
| 9e0c198013 | |||
| 19cd96eed9 | |||
| f894a1deb5 | |||
| f893c15efd | |||
| 2de09943e0 |
+10
-4
@@ -1,11 +1,18 @@
|
||||
sudo: false
|
||||
language: ruby
|
||||
|
||||
rvm:
|
||||
- 1.8.7
|
||||
- 1.9.3
|
||||
- 2.0.0
|
||||
- rbx-18mode
|
||||
- rbx-19mode
|
||||
- 2.1.10
|
||||
- 2.2.6
|
||||
- 2.3.3
|
||||
- 2.4.0
|
||||
- jruby-19mode
|
||||
- jruby-9
|
||||
|
||||
services:
|
||||
- redis-server
|
||||
|
||||
before_script:
|
||||
- git submodule update --init --recursive
|
||||
@@ -13,4 +20,3 @@ before_script:
|
||||
script: bundle exec rspec -c spec/
|
||||
|
||||
env: TRAVIS=1
|
||||
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
### 0.2.0 / 2013-10-01
|
||||
|
||||
* Migrate from Yajl to MultiJson to support JRuby
|
||||
* Trigger `close` event as required by Faye 1.0
|
||||
|
||||
|
||||
### 0.1.1 / 2013-04-28
|
||||
|
||||
* Improve garbage collection to avoid leaking Redis memory
|
||||
|
||||
|
||||
### 0.1.0 / 2012-02-26
|
||||
|
||||
* Initial release: Redis backend for Faye 0.8
|
||||
|
||||
@@ -1,9 +0,0 @@
|
||||
=== 0.1.1 / 2013-04-28
|
||||
|
||||
* Improve garbage collection to avoid leaking Redis memory
|
||||
|
||||
|
||||
=== 0.1.0 / 2012-02-26
|
||||
|
||||
* Initial release: Redis backend for Faye 0.8
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
# Code of Conduct
|
||||
|
||||
All projects under the [Faye](https://github.com/faye) umbrella are covered by
|
||||
the [Code of Conduct](https://github.com/faye/code-of-conduct).
|
||||
@@ -1,2 +1,2 @@
|
||||
source "https://rubygems.org/"
|
||||
source 'https://rubygems.org/'
|
||||
gemspec
|
||||
|
||||
@@ -0,0 +1,60 @@
|
||||
# Faye::Redis [](https://travis-ci.org/faye/faye-redis-ruby)
|
||||
|
||||
This plugin provides a Redis-based backend for the
|
||||
[Faye](http://faye.jcoglan.com) messaging server. It allows a single Faye
|
||||
service to be distributed across many front-end web servers by storing state and
|
||||
routing messages through a [Redis](http://redis.io) database server.
|
||||
|
||||
|
||||
## Usage
|
||||
|
||||
Pass in the engine and any settings you need when setting up your Faye server.
|
||||
|
||||
```rb
|
||||
require 'faye'
|
||||
require 'faye/redis'
|
||||
|
||||
bayeux = Faye::RackAdapter.new(
|
||||
:mount => '/',
|
||||
:timeout => 25,
|
||||
:engine => {
|
||||
:type => Faye::Redis,
|
||||
:host => 'redis.example.com',
|
||||
# more options
|
||||
}
|
||||
)
|
||||
```
|
||||
|
||||
The full list of settings is as follows.
|
||||
|
||||
* <b>`:uri`</b> - redis URL (example: `redis://:secretpassword@example.com:9000/4`)
|
||||
* <b>`:host`</b> - hostname of your Redis instance
|
||||
* <b>`:port`</b> - port number, default is `6379`
|
||||
* <b>`:password`</b> - password, if `requirepass` is set
|
||||
* <b>`:database`</b> - number of database to use, default is `0`
|
||||
* <b>`:namespace`</b> - prefix applied to all keys, default is `''`
|
||||
* <b>`:socket`</b> - path to Unix socket if `unixsocket` is set
|
||||
|
||||
|
||||
## License
|
||||
|
||||
(The MIT License)
|
||||
|
||||
Copyright (c) 2011-2013 James Coglan
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
this software and associated documentation files (the 'Software'), to deal in
|
||||
the Software without restriction, including without limitation the rights to
|
||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
||||
the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
||||
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
-59
@@ -1,59 +0,0 @@
|
||||
= Faye::Redis {<img src="https://secure.travis-ci.org/faye/faye-redis-ruby.png" alt="Build Status" />}[http://travis-ci.org/faye/faye-redis-ruby]
|
||||
|
||||
This plugin provides a Redis-based backend for the {Faye}[http://faye.jcoglan.com]
|
||||
messaging server. It allows a single Faye service to be distributed across many
|
||||
front-end web servers by storing state and routing messages through a
|
||||
{Redis}[http://redis.io] database server.
|
||||
|
||||
|
||||
== Usage
|
||||
|
||||
Pass in the engine and any settings you need when setting up your Faye server.
|
||||
|
||||
require 'faye'
|
||||
require 'faye/redis'
|
||||
|
||||
bayeux = Faye::RackAdapter.new(
|
||||
:mount => '/',
|
||||
:timeout => 25,
|
||||
:engine => {
|
||||
:type => Faye::Redis,
|
||||
:host => 'redis.example.com',
|
||||
# more options
|
||||
}
|
||||
)
|
||||
|
||||
The full list of settings is as follows.
|
||||
|
||||
* <b><tt>:uri</tt></b> - redis URL (example: redis://:secretpassword@example.com:9000/4)
|
||||
* <b><tt>:host</tt></b> - hostname of your Redis instance
|
||||
* <b><tt>:port</tt></b> - port number, default is +6379+
|
||||
* <b><tt>:password</tt></b> - password, if +requirepass+ is set
|
||||
* <b><tt>:database</tt></b> - number of database to use, default is +0+
|
||||
* <b><tt>:namespace</tt></b> - prefix applied to all keys, default is <tt>''</tt>
|
||||
* <b><tt>:socket</tt></b> - path to Unix socket if +unixsocket+ is set
|
||||
|
||||
|
||||
== License
|
||||
|
||||
(The MIT License)
|
||||
|
||||
Copyright (c) 2011-2012 James Coglan
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
this software and associated documentation files (the 'Software'), to deal in
|
||||
the Software without restriction, including without limitation the rights to use,
|
||||
copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
|
||||
Software, and to permit persons to whom the Software is furnished to do so,
|
||||
subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
||||
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
|
||||
+16
-13
@@ -1,20 +1,23 @@
|
||||
Gem::Specification.new do |s|
|
||||
s.name = "faye-redis"
|
||||
s.version = "0.1.1"
|
||||
s.summary = "Redis backend engine for Faye"
|
||||
s.author = "James Coglan"
|
||||
s.email = "jcoglan@gmail.com"
|
||||
s.homepage = "http://github.com/faye/faye-redis-ruby"
|
||||
s.name = 'faye-redis'
|
||||
s.version = '0.2.0'
|
||||
s.summary = 'Redis backend engine for Faye'
|
||||
s.author = 'James Coglan'
|
||||
s.email = 'jcoglan@gmail.com'
|
||||
s.homepage = 'http://github.com/faye/faye-redis-ruby'
|
||||
|
||||
s.extra_rdoc_files = %w[README.rdoc]
|
||||
s.rdoc_options = %w[--main README.rdoc]
|
||||
s.extra_rdoc_files = %w[README.md]
|
||||
s.rdoc_options = %w[--main README.md --markup markdown]
|
||||
s.require_paths = %w[lib]
|
||||
|
||||
s.files = %w[README.rdoc CHANGELOG.txt] + Dir.glob("lib/**/*.rb")
|
||||
s.files = %w[CHANGELOG.md README.md] +
|
||||
Dir.glob('lib/**/*.rb')
|
||||
|
||||
s.add_dependency "eventmachine", ">= 0.12.0"
|
||||
s.add_dependency "em-hiredis", ">= 0.2.0"
|
||||
s.add_dependency "yajl-ruby", ">= 1.0.0"
|
||||
s.add_dependency 'eventmachine', '>= 0.12.0'
|
||||
s.add_dependency 'em-hiredis', '>= 0.2.0'
|
||||
s.add_dependency 'multi_json', '>= 1.0.0'
|
||||
|
||||
s.add_development_dependency "rspec"
|
||||
s.add_development_dependency 'rspec'
|
||||
s.add_development_dependency 'rspec-eventmachine'
|
||||
s.add_development_dependency 'websocket-driver'
|
||||
end
|
||||
|
||||
+23
-27
@@ -1,14 +1,13 @@
|
||||
require 'em-hiredis'
|
||||
require 'yajl'
|
||||
require 'multi_json'
|
||||
|
||||
require File.expand_path('../redis_factory', __FILE__)
|
||||
|
||||
module Faye
|
||||
class Redis
|
||||
|
||||
DEFAULT_HOST = 'localhost'
|
||||
DEFAULT_PORT = 6379
|
||||
DEFAULT_DATABASE = 0
|
||||
DEFAULT_GC = 60
|
||||
LOCK_TIMEOUT = 120
|
||||
DEFAULT_GC = 60
|
||||
LOCK_TIMEOUT = 120
|
||||
|
||||
def self.create(server, options)
|
||||
new(server, options)
|
||||
@@ -17,32 +16,28 @@ module Faye
|
||||
def initialize(server, options)
|
||||
@server = server
|
||||
@options = options
|
||||
@factory = options[:factory] || RedisFactory.new(options)
|
||||
|
||||
init
|
||||
end
|
||||
|
||||
def init
|
||||
return if @redis
|
||||
return if @redis or !EventMachine.reactor_running?
|
||||
|
||||
uri = @options[:uri] || nil
|
||||
host = @options[:host] || DEFAULT_HOST
|
||||
port = @options[:port] || DEFAULT_PORT
|
||||
db = @options[:database] || DEFAULT_DATABASE
|
||||
auth = @options[:password] || nil
|
||||
gc = @options[:gc] || DEFAULT_GC
|
||||
@ns = @options[:namespace] || ''
|
||||
socket = @options[:socket] || nil
|
||||
@redis = @factory.call
|
||||
|
||||
if uri
|
||||
@redis = EventMachine::Hiredis.connect(uri)
|
||||
elsif socket
|
||||
@redis = EventMachine::Hiredis::Client.new(socket, nil, auth, db).connect
|
||||
else
|
||||
@redis = EventMachine::Hiredis::Client.new(host, port, auth, db).connect
|
||||
end
|
||||
@subscriber = @redis.pubsub
|
||||
|
||||
@subscriber.subscribe(@ns + '/notifications')
|
||||
@message_channel = @ns + '/notifications/messages'
|
||||
@close_channel = @ns + '/notifications/close'
|
||||
|
||||
@subscriber.subscribe(@message_channel)
|
||||
@subscriber.subscribe(@close_channel)
|
||||
@subscriber.on(:message) do |topic, message|
|
||||
empty_queue(message) if topic == @ns + '/notifications'
|
||||
empty_queue(message) if topic == @message_channel
|
||||
@server.trigger(:close, message) if topic == @close_channel
|
||||
end
|
||||
|
||||
@gc = EventMachine.add_periodic_timer(gc, &method(:gc))
|
||||
@@ -50,7 +45,8 @@ module Faye
|
||||
|
||||
def disconnect
|
||||
return unless @redis
|
||||
@subscriber.unsubscribe(@ns + '/notifications')
|
||||
@subscriber.unsubscribe(@message_channel)
|
||||
@subscriber.unsubscribe(@close_channel)
|
||||
EventMachine.cancel_timer(@gc)
|
||||
end
|
||||
|
||||
@@ -97,6 +93,7 @@ module Faye
|
||||
@redis.zrem(@ns + '/clients', client_id) do
|
||||
@server.debug 'Destroyed client ?', client_id
|
||||
@server.trigger(:disconnect, client_id)
|
||||
@redis.publish(@close_channel, client_id)
|
||||
callback.call if callback
|
||||
end
|
||||
end
|
||||
@@ -138,7 +135,7 @@ module Faye
|
||||
init
|
||||
@server.debug 'Publishing message ?', message
|
||||
|
||||
json_message = Yajl::Encoder.encode(message)
|
||||
json_message = MultiJson.dump(message)
|
||||
channels = Channel.expand(message['channel'])
|
||||
keys = channels.map { |c| @ns + "/channels#{c}" }
|
||||
|
||||
@@ -148,7 +145,7 @@ module Faye
|
||||
|
||||
@server.debug 'Queueing for client ?: ?', client_id, message
|
||||
@redis.rpush(queue, json_message)
|
||||
@redis.publish(@ns + '/notifications', client_id)
|
||||
@redis.publish(@message_channel, client_id)
|
||||
|
||||
client_exists(client_id) do |exists|
|
||||
@redis.del(queue) unless exists
|
||||
@@ -170,7 +167,7 @@ module Faye
|
||||
@redis.del(key)
|
||||
@redis.exec.callback do |json_messages, deleted|
|
||||
next unless json_messages
|
||||
messages = json_messages.map { |json| Yajl::Parser.parse(json) }
|
||||
messages = json_messages.map { |json| MultiJson.load(json) }
|
||||
@server.deliver(client_id, messages)
|
||||
end
|
||||
end
|
||||
@@ -228,4 +225,3 @@ module Faye
|
||||
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
module Faye
|
||||
class RedisFactory
|
||||
|
||||
DEFAULT_HOST = '0.0.0.0'
|
||||
DEFAULT_PORT = 6379
|
||||
DEFAULT_DATABASE = 0
|
||||
|
||||
def initialize(options)
|
||||
@options = options
|
||||
end
|
||||
|
||||
def call
|
||||
uri = @options[:uri] || nil
|
||||
socket = @options[:socket] || nil
|
||||
host = @options[:host] || DEFAULT_HOST
|
||||
port = @options[:port] || DEFAULT_PORT
|
||||
auth = @options[:password] || nil
|
||||
db = @options[:database] || DEFAULT_DATABASE
|
||||
|
||||
if uri
|
||||
EventMachine::Hiredis.connect(uri)
|
||||
elsif socket
|
||||
EventMachine::Hiredis::Client.new(socket, nil, auth, db).connect
|
||||
else
|
||||
EventMachine::Hiredis::Client.new(host, port, auth, db).connect
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
@@ -7,8 +7,8 @@ describe Faye::Redis do
|
||||
end
|
||||
|
||||
after do
|
||||
engine.disconnect
|
||||
redis = EM::Hiredis::Client.connect('localhost', 6379)
|
||||
disconnect_engine
|
||||
redis = EM::Hiredis::Client.connect('0.0.0.0', 6379)
|
||||
redis.auth(engine_opts[:password])
|
||||
redis.flushall
|
||||
end
|
||||
@@ -23,4 +23,3 @@ describe Faye::Redis do
|
||||
it_should_behave_like "faye engine"
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -39,4 +39,3 @@ list-max-ziplist-value 64
|
||||
set-max-intset-entries 512
|
||||
|
||||
activerehashing yes
|
||||
|
||||
|
||||
@@ -1,3 +1,7 @@
|
||||
require File.expand_path('../../lib/faye/redis', __FILE__)
|
||||
require 'websocket/driver'
|
||||
require File.expand_path('../../vendor/faye/spec/ruby/engine_examples', __FILE__)
|
||||
|
||||
class << Faye
|
||||
attr_accessor :logger
|
||||
end
|
||||
|
||||
Vendored
+1
-1
Submodule vendor/faye updated: c7fac489b1...4778552e83
Reference in New Issue
Block a user