Skip to content

Commit 4f15b0e

Browse files
committed
Allow ExRets.HttpClient.Httpc to work across nodes
1 parent 7eba36c commit 4f15b0e

2 files changed

Lines changed: 17 additions & 18 deletions

File tree

lib/ex_rets/http_client/httpc.ex

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ defmodule ExRets.HttpClient.Httpc do
4949
@typedoc since: "0.1.0"
5050
@type result :: {status_line(), headers(), body()}
5151

52+
@error_http_client_stopped {:error, :http_client_stopped}
53+
5254
# Interface
5355

5456
@impl HttpClient
@@ -75,9 +77,7 @@ defmodule ExRets.HttpClient.Httpc do
7577
| {:error, ExRets.reason()}
7678
def open_stream(client, %HttpRequest{} = request, http_opts \\ [])
7779
when is_pid(client) and is_list(http_opts) do
78-
with true <- Process.alive?(client),
79-
{:ok, stream} <-
80-
GenServer.start_link(__MODULE__, {client, request, http_opts}),
80+
with {:ok, stream} <- GenServer.start_link(__MODULE__, {client, request, http_opts}),
8181
{:ok, %HttpResponse{status: 200} = response} <-
8282
GenServer.call(stream, :start_stream, :infinity) do
8383
{:ok, response, stream}
@@ -96,11 +96,10 @@ defmodule ExRets.HttpClient.Httpc do
9696
@impl HttpClient
9797
@doc since: "0.1.0"
9898
def close_stream(stream) when is_pid(stream) do
99-
if Process.alive?(stream) do
100-
GenServer.call(stream, :cancel_stream, :infinity)
101-
else
102-
:ok
103-
end
99+
GenServer.call(stream, :cancel_stream, :infinity)
100+
catch
101+
# GenServer doesn't exist
102+
:exit, _e -> :ok
104103
end
105104

106105
@impl HttpClient
@@ -134,8 +133,15 @@ defmodule ExRets.HttpClient.Httpc do
134133
from,
135134
%{client: client, http_opts: http_opts, request: request} = state
136135
) do
137-
{:ok, request_id} = start_async_request(client, request, http_opts)
136+
httpc_request = HttpRequest.to_httpc(request)
137+
http_opts = merge_default_http_opts(http_opts)
138+
139+
{:ok, request_id} =
140+
:httpc.request(request.method, httpc_request, http_opts, httpc_opts(), client)
141+
138142
{:noreply, %{state | from: from, request_id: request_id}}
143+
catch
144+
:exit, _e -> {:stop, :normal, @error_http_client_stopped, state}
139145
end
140146

141147
def handle_call(
@@ -159,6 +165,8 @@ defmodule ExRets.HttpClient.Httpc do
159165
:ok = :httpc.stream_next(httpc_stream_pid)
160166
{:noreply, %{state | from: from}}
161167
end
168+
catch
169+
:exit, _e -> {:stop, :normal, @error_http_client_stopped, state}
162170
end
163171

164172
def handle_call(:cancel_stream, _from, %{client: client, request_id: request_id} = state) do
@@ -213,13 +221,6 @@ defmodule ExRets.HttpClient.Httpc do
213221
end
214222
end
215223

216-
defp start_async_request(client, %HttpRequest{} = request, http_opts) do
217-
httpc_request = HttpRequest.to_httpc(request)
218-
http_opts = merge_default_http_opts(http_opts)
219-
220-
:httpc.request(request.method, httpc_request, http_opts, httpc_opts(), client)
221-
end
222-
223224
defp merge_default_http_opts(http_opts) do
224225
preferred_ciphers = ssl_preferred_ciphers()
225226
ciphers = :ssl.filter_cipher_suites(preferred_ciphers, [])

lib/ex_rets/rets_client.ex

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ defmodule ExRets.RetsClient do
44

55
alias ExRets.Credentials
66
alias ExRets.HttpClient
7-
alias ExRets.HttpClient.Httpc
8-
alias ExRets.HttpClient.Mock
97
alias ExRets.LoginResponse
108
alias ExRets.Middleware
119

0 commit comments

Comments
 (0)