Skip to content

Commit

Permalink
feat: set client calls to infinite timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
tlux committed Mar 28, 2024
1 parent 837ea2b commit bf3097f
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 26 deletions.
49 changes: 25 additions & 24 deletions lib/graphql_ws_client.ex
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,6 @@ defmodule GraphQLWSClient do
State
}

@client_timeout :infinity

@typedoc """
Type for a client process.
"""
Expand Down Expand Up @@ -181,23 +179,25 @@ defmodule GraphQLWSClient do
"""
@spec connected?(client) :: boolean
def connected?(client) do
Connection.call(client, :connected?, @client_timeout)
call_no_timeout(client, :connected?)
end

@doc """
Opens the connection to the websocket.
"""
@spec open(client) :: :ok | {:error, Exception.t()}
def open(client) do
Connection.call(client, :open, @client_timeout)
call_no_timeout(client, :open)
end

@doc """
Opens the connection to the websocket. Raises on error.
"""
@spec open!(client) :: :ok | no_return
def open!(client) do
client |> open() |> bang!()
client
|> open()
|> bang!()
end

@doc """
Expand All @@ -206,7 +206,7 @@ defmodule GraphQLWSClient do
@doc since: "1.0.0"
@spec open_with(client, any) :: :ok | {:error, Exception.t()}
def open_with(client, init_payload) do
Connection.call(client, {:open_with, init_payload}, @client_timeout)
call_no_timeout(client, {:open_with, init_payload})
end

@doc """
Expand All @@ -215,15 +215,17 @@ defmodule GraphQLWSClient do
@doc since: "1.0.0"
@spec open_with!(client, any) :: :ok | no_return
def open_with!(client, init_payload) do
client |> open_with(init_payload) |> bang!()
client
|> open_with(init_payload)
|> bang!()
end

@doc """
Closes the connection to the websocket.
"""
@spec close(client) :: :ok
def close(client) do
Connection.call(client, :close, @client_timeout)
call_no_timeout(client, :close)
end

@doc """
Expand All @@ -241,11 +243,7 @@ defmodule GraphQLWSClient do
@spec query(client, query, variables, nil | timeout) ::
{:ok, any} | {:error, Exception.t()}
def query(client, query, variables \\ %{}, timeout \\ nil) do
Connection.call(
client,
{:query, query, variables, timeout},
@client_timeout
)
call_no_timeout(client, {:query, query, variables, timeout})
end

@doc """
Expand All @@ -261,9 +259,9 @@ defmodule GraphQLWSClient do
...> )
%{"data" => %{"posts" => %{"body" => "Lorem Ipsum"}}}
"""
@spec query!(client, query, variables, nil | timeout) :: any | no_return
def query!(client, query, variables \\ %{}, timeout \\ nil) do
case query(client, query, variables, timeout) do
@spec query!(client, query, variables) :: any | no_return
def query!(client, query, variables \\ %{}) do
case query(client, query, variables) do
{:ok, result} -> result
{:error, error} -> raise error
end
Expand All @@ -289,11 +287,7 @@ defmodule GraphQLWSClient do
@spec subscribe(client, query, variables, pid) ::
{:ok, subscription_id} | {:error, Exception.t()}
def subscribe(client, query, variables \\ %{}, listener \\ self()) do
Connection.call(
client,
{:subscribe, query, variables, listener},
@client_timeout
)
call_no_timeout(client, {:subscribe, query, variables, listener})
end

@doc """
Expand Down Expand Up @@ -330,7 +324,7 @@ defmodule GraphQLWSClient do
"""
@spec unsubscribe(client, subscription_id) :: :ok | {:error, Exception.t()}
def unsubscribe(client, subscription_id) do
Connection.call(client, {:unsubscribe, subscription_id}, @client_timeout)
call_no_timeout(client, {:unsubscribe, subscription_id})
end

@doc """
Expand All @@ -343,7 +337,9 @@ defmodule GraphQLWSClient do
"""
@spec unsubscribe!(client, subscription_id) :: :ok | no_return
def unsubscribe!(client, subscription_id) do
client |> unsubscribe(subscription_id) |> bang!()
client
|> unsubscribe(subscription_id)
|> bang!()
end

@doc """
Expand Down Expand Up @@ -378,7 +374,8 @@ defmodule GraphQLWSClient do
iex> stream |> Stream.take(3) |> Enum.to_list()
"""
@doc since: "2.0.0"
@spec stream!(client, query, variables, Keyword.t()) :: Enumerable.t()
@spec stream!(client, query, variables, Keyword.t()) ::
Enumerable.t() | no_return
def stream!(client, query, variables \\ %{}, opts \\ []) do
Stream.resource(
fn -> Iterator.open!(client, query, variables, opts) end,
Expand All @@ -399,6 +396,10 @@ defmodule GraphQLWSClient do
}
end

defp call_no_timeout(client, req) do
Connection.call(client, req, :infinity)
end

# Callbacks

@impl true
Expand Down
6 changes: 5 additions & 1 deletion lib/graphql_ws_client/iterator.ex
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ defmodule GraphQLWSClient.Iterator do
def open!(client, query, variables \\ %{}, opts \\ []) do
opts =
opts
|> Keyword.merge(client: client, query: query, variables: variables)
|> Keyword.merge(
client: client,
query: query,
variables: variables
)
|> Opts.new()
|> Opts.validate!()

Expand Down
7 changes: 6 additions & 1 deletion lib/graphql_ws_client/iterator/opts.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@ defmodule GraphQLWSClient.Iterator.Opts do

@enforce_keys [:client, :query]

defstruct [:client, :query, buffer_size: 1000, variables: %{}]
defstruct [
:client,
:query,
buffer_size: 1000,
variables: %{}
]

@type t :: %__MODULE__{
buffer_size: pos_integer() | :infinity,
Expand Down
4 changes: 4 additions & 0 deletions test/graphql_ws_client_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,8 @@ defmodule GraphQLWSClientTest do
config = %{@config | query_timeout: 200}
conn = %{@conn | config: config}

stub(MockDriver, :disconnect, fn _ -> :ok end)

MockDriver
|> expect(:connect, fn _ -> {:ok, conn} end)
|> expect(:push_message, fn _, %Message{id: id} ->
Expand Down Expand Up @@ -497,6 +499,8 @@ defmodule GraphQLWSClientTest do
test "timeout" do
test_pid = self()

stub(MockDriver, :disconnect, fn _ -> :ok end)

MockDriver
|> expect(:connect, fn _ -> {:ok, @conn} end)
|> expect(:push_message, fn _, %Message{id: id} ->
Expand Down

0 comments on commit bf3097f

Please sign in to comment.