Permalink
Browse files

Connection pool

  • Loading branch information...
1 parent cac8bcd commit 8549bd15dd0a02e392e07a6fb1630e71aaf1db00 @guiocavalcanti guiocavalcanti committed Sep 13, 2012
Showing with 46 additions and 20 deletions.
  1. +2 −3 config/server.rb
  2. +1 −0 lib/permit.rb
  3. +1 −1 lib/permit/api.rb
  4. +13 −0 lib/permit/connection.rb
  5. +1 −1 lib/permit/policy.rb
  6. +5 −1 lib/permit/rule.rb
  7. +7 −4 spec/policy_spec.rb
  8. +16 −10 spec/rule_spec.rb
View
@@ -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
View
@@ -9,3 +9,4 @@ module Permit
require 'permit/rule'
require 'permit/policy'
require 'permit/api'
+require 'permit/connection'
View
@@ -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
View
@@ -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
View
@@ -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
View
@@ -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
View
@@ -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
View
@@ -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")
@@ -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
@@ -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
@@ -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

0 comments on commit 8549bd1

Please sign in to comment.