Skip to content

Commit

Permalink
Merge branch 'fix-crash-on-connection-close-metric-client' into 'main'
Browse files Browse the repository at this point in the history
fix: Avoid crash when mojito connect is closed for the metrics client

See merge request afontaine/unleash_ex!26
  • Loading branch information
Andrew Fontaine committed May 31, 2022
2 parents 4308ff3 + 671dd2f commit 7946b6c
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .gitmodules
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[submodule "priv/client-specification"]
path = priv/client-specification
url = git://github.com/unleash/client-specification
url = https://github.com/unleash/client-specification
branch = master
11 changes: 8 additions & 3 deletions lib/unleash/client.ex
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,17 @@ defmodule Unleash.Client do

defp handle_feature_response(mojito) do
case mojito do
%Mojito.Response{status_code: 304} -> :cached
%Mojito.Response{status_code: 200} -> pull_out_data(mojito)
%Mojito.Response{status_code: 304} ->
:cached

%Mojito.Response{status_code: 200} ->
pull_out_data(mojito)

resp = %Mojito.Response{status_code: _status} ->
Logger.warn(fn ->
"Unexpected response #{inspect resp}. Using cached features"
"Unexpected response #{inspect(resp)}. Using cached features"
end)

:cached
end
end
Expand Down
5 changes: 5 additions & 0 deletions lib/unleash/metrics.ex
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ defmodule Unleash.Metrics do
{:noreply, send_metrics(state)}
end

def handle_info({:mojito_response, _ref, message}, state) do
Logger.warning("Unexpected response from Mojito #{inspect(message, pretty: true)}")
{:noreply, send_metrics(state)}
end

if Config.test?() do
def handle_call(:send_metrics, _from, state) do
{:reply, :ok, send_metrics(state)}
Expand Down
2 changes: 1 addition & 1 deletion lib/unleash/strategy/flexible_rollout.ex
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ defmodule Unleash.Strategy.FlexibleRollout do
%{
group: group,
percentage: percentage,
sticky_vaule: sticky_value,
sticky_value: sticky_value,
stickiness: Map.get(params, "stickiness")
}}
else
Expand Down
4 changes: 2 additions & 2 deletions test/support/mojito.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ defmodule Mojito.Behavior do
status_code: pos_integer,
headers: headers,
body: String.t(),
complete: boolean,
complete: boolean
}
@type error :: %Mojito.Error{
reason: any,
message: String.t() | nil,
message: String.t() | nil
}

@callback get(String.t(), headers) :: {:ok, response} | {:error, error} | no_return
Expand Down
4 changes: 2 additions & 2 deletions test/unleash_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ defmodule UnleashTest do
Application.put_env(:unleash, Unleash, http_client: MojitoMock)

assert capture_log(fn ->
Unleash.Client.features()
end) =~ ~r/Unexpected response.+Using cached features/
Unleash.Client.features()
end) =~ ~r/Unexpected response.+Using cached features/
end
end

Expand Down

0 comments on commit 7946b6c

Please sign in to comment.