data_pool v1.0.1 DataPool
Provides a blocking data storage and retrival data pool. The basic idea behind DataPool is to allow producers to fill the pool up and block on adding more items once it’s limit is reached. On the flip side, consumers of the data block when the pool is empty.
Summary
Functions
The current status of the pool, can be one of :ok
, :done
, or :halt
Returns the maximum amount of items that can be added to the pool before
calls to push
are blocked
Returns an item out of the pool. If the pool is empty this operation blocks
and waits for an item to become available. A normal return is in the form of a
tuple looking like {:ok, item}
, otherwise it will return the status of :done
or :halt
Add an item to the pool to be processed by a consumer. If the pool is at it’s
max limit this operation will block and wait until there is room available. Push
should always return the the status of the pool, :ok
, :done
, or :halt
.
Items are not added when the state is anything but :ok
Returns the amount of items in the pool
Returns the tuple {:ok, %DataPool{}}
with a live pid queue that
mantains the queue state
Stops the pool, any outstanding push or pops from the pool are canceled
Dynamically changes the maximum size the pool will hold before producers are blocked
Updates the pool with a new status
Types
max_timeout :: pos_integer | :infinity
t :: %DataPool{default_timeout: max_timeout, pid: pid}
Functions
Specs
get_status(t) :: DataPool.State.status
The current status of the pool, can be one of :ok
, :done
, or :halt
Examples
iex> {:ok, pool} = DataPool.start_link
iex> DataPool.get_status(pool)
:ok
Specs
max_size(t) :: pos_integer
Returns the maximum amount of items that can be added to the pool before
calls to push
are blocked
Example
iex> {:ok, pid} = DataPool.start_link
iex> DataPool.max_size(pid)
20
Specs
pop(t, max_timeout) :: {:ok, any} | :done | :halt
Returns an item out of the pool. If the pool is empty this operation blocks
and waits for an item to become available. A normal return is in the form of a
tuple looking like {:ok, item}
, otherwise it will return the status of :done
or :halt
Examples
iex> {:ok, pid} = DataPool.start_link
iex> task = Task.async fn ->
...> DataPool.pop(pid)
...> end
iex> Task.yield(task, 100)
nil
iex> {:ok, pid} = DataPool.start_link
iex> DataPool.push(pid, :it)
iex> DataPool.pop(pid)
{:ok, :it}
Specs
push(t, any, max_timeout) :: DataPool.State.status
Add an item to the pool to be processed by a consumer. If the pool is at it’s
max limit this operation will block and wait until there is room available. Push
should always return the the status of the pool, :ok
, :done
, or :halt
.
Items are not added when the state is anything but :ok
Examples
iex> {:ok, pid} = DataPool.start_link
iex> DataPool.push(pid, :it)
:ok
iex> {:ok, pid} = DataPool.start_link
iex> task = Task.async fn ->
...> 1..100 |> Enum.map(fn x -> DataPool.push(pid, x) end)
...> end
iex> Task.yield(task, 100)
nil
iex> {:ok, pid} = DataPool.start_link
iex> task = Task.async fn ->
...> 1..5 |> Enum.map(fn x -> DataPool.push(pid, x) end)
...> end
iex> Task.yield(task, 100)
{:ok, [:ok, :ok, :ok, :ok, :ok]}
Specs
size(t) :: pos_integer
Returns the amount of items in the pool
Example
iex> {:ok, pid} = DataPool.start_link
iex> DataPool.push(pid, :it)
iex> DataPool.size(pid)
1
iex> {:ok, pid} = DataPool.start_link
iex> DataPool.size(pid)
0
Specs
start_link :: {:ok, t}
Returns the tuple {:ok, %DataPool{}}
with a live pid queue that
mantains the queue state
Example
iex> {:ok, pool} = DataPool.start_link
iex> %DataPool{pid: pid} = pool
iex> is_pid(pid)
true
Specs
stop(t) :: :ok
Stops the pool, any outstanding push or pops from the pool are canceled
Example
iex> {:ok, pid} = DataPool.start_link
iex> DataPool.stop(pid)
:ok
Specs
update_max_size(t, pos_integer) :: :ok
Dynamically changes the maximum size the pool will hold before producers are blocked
Examples
iex> {:ok, pid} = DataPool.start_link
iex> DataPool.update_max_size(pid, 243)
iex> DataPool.max_size(pid)
243
Specs
update_status(t, DataPool.State.status) :: DataPool.State.t
Updates the pool with a new status
Examples
iex> {:ok, pool} = DataPool.start_link
iex> DataPool.update_status(pool, :halt)
iex> DataPool.get_status(pool)
:halt