Skip to content

Commit

Permalink
fix issue where a new redis connection was made for each api request
Browse files Browse the repository at this point in the history
  • Loading branch information
stevegraham committed Sep 21, 2011
1 parent f127b9b commit 50f2227
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 12 deletions.
8 changes: 2 additions & 6 deletions lib/slanger/api_server.rb
Expand Up @@ -9,7 +9,7 @@
require 'rack/fiber_pool'

module Slanger
class APIServer < Sinatra::Base
class ApiServer < Sinatra::Base
use Rack::FiberPool
set :raise_errors, lambda { false }
set :show_exceptions, false
Expand All @@ -24,7 +24,7 @@ class APIServer < Sinatra::Base
authenticate { |key| Signature::Token.new key, lookup_secret[key] }

f = Fiber.current
redis.publish(params[:channel_id], payload).tap do |r|
Slanger::Redis.publish(params[:channel_id], payload).tap do |r|
r.callback { f.resume [202, {}, "202 ACCEPTED\n"] }
r.errback { f.resume [500, {}, "500 INTERNAL SERVER ERROR\n"] }
end
Expand All @@ -38,10 +38,6 @@ def payload
Hash[payload.reject { |k,v| v.nil? }].to_json
end

def redis
@redis ||= EM::Hiredis.connect
end

def lookup_secret
Hash.new "your-pusher-secret"
end
Expand Down
2 changes: 1 addition & 1 deletion lib/slanger/handler.rb
Expand Up @@ -16,7 +16,7 @@ def onmessage(msg)

def onclose
channel = Slanger::Channel.find_by_channel_id(@channel_id)
channel && channel.unsubcribe(@subscription_id)
channel.unsubcribe(@subscription_id)
end

private
Expand Down
21 changes: 19 additions & 2 deletions lib/slanger/redis.rb
@@ -1,3 +1,20 @@
require 'forwardable'

module Slanger
Redis = EM::Hiredis.connect if EM.reactor_running?
end
module Redis
extend Forwardable

def_delegator :publisher, :publish
def_delegators :subscriber, :on, :subscribe

private
def publisher
@publisher ||= EM::Hiredis.connect
end

def subscriber
@subscriber ||= EM::Hiredis.connect
end
extend self
end
end
2 changes: 1 addition & 1 deletion lib/slanger/service.rb
Expand Up @@ -11,7 +11,7 @@ def run(opts={})

opts = defaults.merge opts

Rack::Handler::Thin.run Slanger::APIServer, Host: opts[:api_host], Port: opts[:api_port]
Rack::Handler::Thin.run Slanger::ApiServer, Host: opts[:api_host], Port: opts[:api_port]
Slanger::WebSocketServer.run host: opts[:websocket_host], port: opts[:websocket_port],
debug: opts[:debug], app_key: opts[:app_key]
end
Expand Down
10 changes: 8 additions & 2 deletions slanger.rb
@@ -1,9 +1,15 @@
require 'bundler/setup'

require 'eventmachine'
require 'em-hiredis'
require 'rack'

module Slanger; end

EM.run do
File.tap do |f|
Dir[f.expand_path(f.join(f.dirname(__FILE__),'lib', 'slanger', '*.rb'))].reverse_each { |file| require file }
Dir[f.expand_path(f.join(f.dirname(__FILE__),'lib', 'slanger', '*.rb'))].each do |file|
Slanger.autoload File.basename(file, '.rb').classify, file
end
end
end
end
3 changes: 3 additions & 0 deletions spec/integration/integration_spec.rb
@@ -1,8 +1,11 @@
require 'bundler/setup'

require 'active_support/json'
require 'active_support/core_ext/hash'
require 'eventmachine'
require 'em-http-request'
require 'pusher'
require 'thin'

describe 'Integration' do
Thread.abort_on_exception = false
Expand Down

0 comments on commit 50f2227

Please sign in to comment.