Permalink
Browse files

implemented worker checkin/checkout

  • Loading branch information...
davidsulc committed Jun 24, 2018
1 parent a20e4b0 commit 412b87c76f2b185d2a329c7e532cc1407d6e4732
Showing with 20 additions and 0 deletions.
  1. +20 −0 lib/pool_toy/pool_man.ex
View
@@ -11,11 +11,31 @@ defmodule PoolToy.PoolMan do
GenServer.start_link(__MODULE__, size, name: @name)
end
def checkout() do
GenServer.call(@name, :checkout)
end
def checkin(worker) do
GenServer.cast(@name, {:checkin, worker})
end
def init(size) do
send(self(), :start_workers)
{:ok, %State{size: size}}
end
def handle_call(:checkout, _from, %State{workers: []} = state) do
{:reply, :full, state}
end
def handle_call(:checkout, _from, %State{workers: [worker | rest]} = state) do
{:reply, worker, %{state | workers: rest}}
end
def handle_cast({:checkin, worker}, %State{workers: workers} = state) do
{:noreply, %{state | workers: [worker | workers]}}
end
def handle_info(:start_workers, %State{size: size} = state) do
workers =
for _ <- 1..size do

0 comments on commit 412b87c

Please sign in to comment.