Pool(T)
Inherits Reference / Object
A simple thread-safe generic pool.
A pool can be used to store resources that are expensive to create (like connections). See here for more explanations.
require "pool"
pool = Pool.new { IO::Memory.new }
pool.get do |resource|
# do something
end
Constructors
new(initial_capacity : Int = 0, &block : -> T)
Creates a new Pool.
require "pool"
Pool.new { IO::Memory.new }
Instance methods
get
Gets an resource, yields, then adds it back.
Note that if an exception was raised in the block, the resource won't be added back. This behavior is used to prevent adding an invalid resource, for example a connection causing an IO error.
resize(new_size : Int32, & : T -> ) : Nil
Resizes the pool to match the new total_size, and yields each deleted resource on size shrinking.