A general-purpose object "pool" for storing general objects. It can be a connection pool, cache, or even just a factory.
It takes the same parameters as ActiveRecord's connection pool and was built specifically for the gem Mystic
(it's Mystic
's connection pool)
AccessStack is threadsafe, obviously. It uses the threadsafety
gem.
Add this line to your application's Gemfile:
gem 'access_stack'
And then execute:
$ bundle
Or install it yourself as:
$ gem install access_stack
You can create a stack:
require "access_stack"
s = AccessStack.new(
:pool => 3, # size of pool (default 5)
:checkout_timeout => 10, # Timeout in seconds for checking a connection out of the pool (default 5)
:reaping_frequency => 5, # How often to run the reaper, in seconds (Default nil - don't run the reaper)
:dead_connection_timeout # How long in seconds a connection is considered alive (default 5)
:create => lambda {
PG.connect
},
:destroy => lambda { |instance|
instance.close
},
:validate => lamda { |instance|
instance.status == CONNECTION_OK
}
)
Set a stack's blocks:
s.create do
# do something
end
s.destroy do |inst|
# do something with inst
end
s.validate do |inst|
# return whether or not inst is valid
end
Use an instance:
res = s.with { |inst| inst.exec("SELECT * FROM users;") }
# res is a PG::Result now, since that's what the block returned
Eager-load instances:
s.fill! 5 # Add 5 objects to the stack
s.fill! -1 # Fill stack to capacity-1
s.fill! # Fill stack to capacity
Clear out instances:
s.reap! # Validates each object using expires and validate
s.clear! # Empties the stack
s.empty? # Checks if the stack is empty
s.available? # Whether or not the pool is full
s.full? # Whether or not the stack is full
- Fork it ( https://github.com/fhsjaagshs/access_stack/fork )
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request