Skip to content

Commit

Permalink
Connection pool
Browse files Browse the repository at this point in the history
  • Loading branch information
guiocavalcanti committed Sep 13, 2012
1 parent cac8bcd commit 8549bd1
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 20 deletions.
5 changes: 2 additions & 3 deletions config/server.rb
@@ -1,5 +1,4 @@
require 'em-synchrony/em-mongo'

config['db'] = EM::Synchrony::ConnectionPool.new(:size => 3) do
connection = EM::Mongo::Connection.new.db('permit_development')
end
Permit::Connection.establish_connections(3, "development")
config['db'] = Permit::Connection.pool
1 change: 1 addition & 0 deletions lib/permit.rb
Expand Up @@ -9,3 +9,4 @@ module Permit
require 'permit/rule'
require 'permit/policy'
require 'permit/api'
require 'permit/connection'
2 changes: 1 addition & 1 deletion lib/permit/api.rb
Expand Up @@ -13,7 +13,7 @@ class API < Grape::API
filter[:actions] = {}
filter[:actions][params[:action]] = true

policy = Policy.new(filter.merge(:logger => env.logger, :db => env.db))
policy = Policy.new(filter.merge(:logger => env.logger))
rules = policy.rules.count

if rules > 0
Expand Down
13 changes: 13 additions & 0 deletions lib/permit/connection.rb
@@ -0,0 +1,13 @@
module Permit
class Connection
def self.establish_connections(pool_size, environment)
@@connections = EM::Synchrony::ConnectionPool.new(:size => pool_size) do
EM::Mongo::Connection.new.db("permit_#{environment}")
end
end

def self.pool
@@connections
end
end
end
2 changes: 1 addition & 1 deletion lib/permit/policy.rb
@@ -1,7 +1,7 @@
module Permit
class Policy
def initialize(opts={})
@db = opts.delete(:db)
@db = Connection.pool
@logger = opts.delete(:logger)
@filter = opts
end
Expand Down
6 changes: 5 additions & 1 deletion lib/permit/rule.rb
@@ -1,7 +1,11 @@
require 'em-synchrony'
require 'em-synchrony/em-mongo'

module Permit
class Rule

def initialize(opts={})
@db = opts.delete(:db)
@db = Connection.pool
@logger = opts.delete(:logger)
@filter = opts
end
Expand Down
11 changes: 7 additions & 4 deletions spec/policy_spec.rb
Expand Up @@ -2,12 +2,15 @@

module Permit
describe Policy do
let(:db) do
EM::Mongo::Connection.new.db('permit_test')
before(:all) do
EventMachine.synchrony do
Permit::Connection.establish_connections(1, "test")
EM.stop
end
end
let(:policy) { Policy.new(:db => db, :resource_id => 'r') }
let(:policy) { Policy.new(:resource_id => 'r') }
let(:rules) do
db.collection("rules")
Permit::Connection.pool.collection("rules")
end

context "finders" do
Expand Down
26 changes: 16 additions & 10 deletions spec/rule_spec.rb
Expand Up @@ -2,8 +2,20 @@

module Permit
describe Rule do
before(:all) do
EventMachine.synchrony do
Permit::Connection.establish_connections(1, "test")
EM.stop
end
end
around do
EventMachine.synchrony do
rules.remove({})
EM.stop
end
end
let(:db) do
EM::Mongo::Connection.new.db('permit_test')
Permit::Connection.pool
end
let(:rules) do
db.collection("rules")
Expand All @@ -14,16 +26,10 @@ module Permit
[{ :resource_id => "r", :subject_id => "s", :actions => { :a => true} },
{ :resource_id => "t", :subject_id => "s", :actions => { :a => true} }]
end
around do
EventMachine.synchrony do
rules.remove({})
EM.stop
end
end
it "should count data from db" do
EventMachine.synchrony do
rules.safe_insert(fixtures)
rule = Rule.new(:db => db, :resource_id => 'r')
rule = Rule.new(:resource_id => 'r')
rule.count.should == 1
EM.stop
end
Expand All @@ -32,7 +38,7 @@ module Permit
it "should find data from db" do
EventMachine.synchrony do
rules.safe_insert(fixtures)
rule = Rule.new(:db => db, :resource_id => 'r')
rule = Rule.new(:resource_id => 'r')
rule.find.to_a == fixtures.first.to_a
EM.stop
end
Expand All @@ -42,7 +48,7 @@ module Permit
context "inserts" do
it "should insert rule" do
EventMachine.synchrony do
rule = Rule.new(:db => db, :resource_id => 'r', :subject_id => 's')
rule = Rule.new(:resource_id => 'r', :subject_id => 's')
rule.insert(:action => :read)
rule.count(:actions => { :read => true }).should == 1
EM.stop
Expand Down

0 comments on commit 8549bd1

Please sign in to comment.