class

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 }
Source

Instance methods

add(resource : T) : Nil

Adds a resource.

Source
get

Gets a resource.

Source
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.

Source
idle_size

Returns the pool's idle size.

Source
resize(new_size : Int32, & : T -> ) : Nil

Resizes the pool to match the new total_size, and yields each deleted resource on size shrinking.

Source
resize(new_size : Int32) : Nil

Resizes the pool to match the new size

Source
total_size

Returns the pool's total size.

Source
used_size

Returns the pool's used resources size.

Source