class Pool(T)
- Pool(T)
- Reference
- Object
Overview
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
Defined in:
pool.crConstructors
-
.new(initial_capacity : Int = 0, &block : -> T)
Creates a new Pool.
Instance Method Summary
-
#add(resource : T) : Nil
Adds a resource.
-
#get : T
Gets a resource.
-
#get(& : T -> ) : Nil
Gets an resource, yields, then adds it back.
-
#idle_size : Int32
Returns the pool's idle size.
-
#resize(new_size : Int32, & : T -> ) : Nil
Resizes the pool to match the new
#total_size
, and yields each deleted resource on size shrinking. -
#resize(new_size : Int32) : Nil
Resizes the pool to match the new size
-
#total_size : Int32
Returns the pool's total size.
-
#used_size : Int32
Returns the pool's used resources size.
Constructor Detail
Creates a new Pool.
require "pool"
Pool.new { IO::Memory.new }
Instance Method Detail
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.
Resizes the pool to match the new #total_size
, and yields each deleted resource on size shrinking.