simplepool
simplepool
Simple thread-safe pool for anything that is not thread-safe.
Installation
-
Add the dependency to your
shard.yml
:dependencies: simplepool: github: konovod/simplepool
-
Run
shards install
Usage
require "simplepool"
# Note that `MyConnection.new` will be protected by mutex, so can be not thread-safe
pool = SimplePool(MyConnection).new { MyConnection.new(address, port) }
10.times do
spawn do
# get object from pool
conn = pool.get
work_with(conn)
# don't forget to return it
pool.return(conn)
end
end
10.times do
spawn do
# safe version as you don't need to explicitely return it
pool.use do |conn|
work_with conn
raise "error" # object will be returned to pool even after raise
end
end
end
Docs on API are available on https://konovod.github.io/simplepool
Contributing
- Fork it (https://github.com/konovod/simplepool/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
Contributors
- konovod - creator and maintainer