class Pool(T)

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

Constructors

Instance Method Summary

Constructor Detail

def self.new(initial_capacity : Int = 0, &block : -> T) #

Creates a new Pool.

require "pool"

Pool.new { IO::Memory.new }

[View source]

Instance Method Detail

def add(resource : T) : Nil #

Adds a resource.


[View source]
def get : T #

Gets a resource.


[View source]
def get(& : T -> ) : Nil #

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.


[View source]
def idle_size : Int32 #

Returns the pool's idle size.


[View source]
def resize(new_size : Int32, & : T -> ) : Nil #

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


[View source]
def resize(new_size : Int32) : Nil #

Resizes the pool to match the new size


[View source]
def total_size : Int32 #

Returns the pool's total size.


[View source]
def used_size : Int32 #

Returns the pool's used resources size.


[View source]