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

Allow multiple clients #32

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
16 changes: 12 additions & 4 deletions lib/riffed/client.ex
Original file line number Diff line number Diff line change
Expand Up @@ -114,16 +114,16 @@ defmodule Riffed.Client do
|> Keyword.delete(:host)
|> Keyword.delete(:retries)

if Module.get_attribute(env.module, :auto_import_structs) do
struct_module = quote do
struct_module = if Module.get_attribute(env.module, :auto_import_structs) do
quote do
defmodule unquote(struct_module) do
use Riffed.Struct, unquote(Meta.structs_to_keyword(thrift_metadata))
unquote_splicing(Riffed.Callbacks.reconstitute(env.module))
unquote_splicing(Riffed.Enumeration.reconstitute(env.module))
end
end
else
struct_module = quote do
quote do
end
end

Expand Down Expand Up @@ -161,6 +161,10 @@ defmodule Riffed.Client do
GenServer.start_link(__MODULE__, :ok, name: __MODULE__)
end

def start_link(thrift_client: thrift_client) do
GenServer.start_link(__MODULE__, thrift_client)
end

def start_link(thrift_client) do
GenServer.start_link(__MODULE__, thrift_client, name: __MODULE__)
end
Expand Down Expand Up @@ -240,14 +244,18 @@ defmodule Riffed.Client do
GenServer.call(__MODULE__, {:disconnect, []})
end

def close(pid) do
def close(pid) when is_pid(pid) do
GenServer.call(pid, {:disconnect, []})
end

def reconnect do
GenServer.call(__MODULE__, {:reconnect, []})
end

def reconnect(pid) when is_pid(pid) do
GenServer.call(pid, {:reconnect, []})
end

defp to_host(hostname) when is_list(hostname) do
hostname
end
Expand Down
2 changes: 1 addition & 1 deletion lib/riffed/macro_helpers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ defmodule Riffed.MacroHelpers do
|> Enum.map(&build_arg_cast(function_name, struct_module, &1, overrides, cast_function))
end

defp build_arg({index, _type}=arg) do
defp build_arg({index, _type}) do
Macro.var(:"arg_#{abs(index)}", nil)
end

Expand Down
6 changes: 3 additions & 3 deletions lib/riffed/server.ex
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,8 @@ defmodule Riffed.Server do

structs_keyword = ThriftMeta.Meta.structs_to_keyword(thrift_meta)

if Module.get_attribute(env.module, :auto_import_structs) do
struct_module = quote do
struct_module = if Module.get_attribute(env.module, :auto_import_structs) do
quote do
defmodule unquote(struct_module) do
@build_cast_to_erlang true
use Riffed.Struct, unquote(structs_keyword)
Expand All @@ -190,7 +190,7 @@ defmodule Riffed.Server do
end
end
else
struct_module = quote do
quote do
end
end

Expand Down
7 changes: 4 additions & 3 deletions lib/riffed/struct.ex
Original file line number Diff line number Diff line change
Expand Up @@ -304,9 +304,10 @@ defmodule Riffed.Struct do
callbacks = Riffed.Callbacks.build(env.module)
enums = Riffed.Enumeration.build(env.module)

erlang_casts = []
if build_cast_to_erlang do
erlang_casts = Riffed.Enumeration.build_cast_return_value_to_erlang(env.module)
erlang_casts = if build_cast_to_erlang do
Riffed.Enumeration.build_cast_return_value_to_erlang(env.module)
else
[]
end

quote do
Expand Down
30 changes: 30 additions & 0 deletions test/riffed/multi_client_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
defmodule MultiClientTest do
use ExUnit.Case

defmodule ClientModels do
use Riffed.Struct, account_types: [:Account, :Preferences]
end

defmodule AccountClient do
use Riffed.Client, structs: ClientModels,
client_opts: [host: "localhost",
port: 3112,
framed: true,
retries: 1,
socket_opts: [
recv_timeout: 3000,
keepalive: true]
],
service: :shared_service_thrift,
import: [:getAccount]
end

test "allow multiple clients" do
{:ok, echo} = EchoServer.start_link

{:ok, pid1} = AccountClient.start_link(thrift_client: echo)
{:ok, pid2} = AccountClient.start_link(thrift_client: echo)

refute pid1 == pid2
end
end