Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes/error no members handling #36

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions lib/riak/crdt/counter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,17 @@ defmodule Riak.CRDT.Counter do
@doc """
Increment a `counter` on the `amount` defaulting in 1
"""
def increment(counter, amount \\ 1) when Record.is_record(counter, :counter) do
def increment(counter, amount \\ 1)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the purpose of this additional increment def, and the decrement below?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It prevents from having a warning at compile time on that function because it has default value for the 'amount' param and several clauses.
I believe this warning is issued since elixir 1.2

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense, 👍

def increment(counter, amount) when Record.is_record(counter, :counter) do
:riakc_counter.increment(amount, counter)
end
def increment(nil, _), do: {:error, :nil_object}

@doc """
Decrement a `counter` on the `amount` defaulting in 1
"""
def decrement(counter, amount \\ 1) when Record.is_record(counter, :counter) do
def decrement(counter, amount \\ 1)
def decrement(counter, amount) when Record.is_record(counter, :counter) do
:riakc_counter.increment(-amount, counter)
end
def decrement(nil, _), do: {:error, :nil_object}
Expand Down
17 changes: 16 additions & 1 deletion lib/riak/pool.ex
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,31 @@ defmodule Riak.Pool do
end
end
def unquote(name)(unquote_splicing(other_args)) do
pid = :pooler.take_group_member(:riak)
pid = take_group_member(:riak, 500)
result = unquote(name)(pid, unquote_splicing(other_args))
:pooler.return_group_member(:riak, pid, :ok)
result
end
end
end

def take_group_member(group_name, timeout \\ 100)
def take_group_member(_, timeout) when timeout <= 0 do
:error_no_members
end
def take_group_member(group_name, timeout) do
case :pooler.take_group_member(group_name) do
:error_no_members ->
#should probably log a warning / error here so the operator knows something is wrong
:timer.sleep(100)
take_group_member(group_name, timeout - 100)
pid -> pid
end
end

defp extract_guards({:when, _, [left, right]}), do: {left, extract_or_guards(right)}
defp extract_guards(else_), do: {else_, []}
defp extract_or_guards({:when, _, [left, right]}), do: [left|extract_or_guards(right)];
defp extract_or_guards(term), do: [term]

end