Skip to content

Commit

Permalink
Downgrade gun to 2.0.x
Browse files Browse the repository at this point in the history
  • Loading branch information
burmajam committed Jun 19, 2024
1 parent d5f1a6a commit 44e822f
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 11 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# 1.8.2

## Bugfix

- Use patched gun

# 1.8.1

## Enhancements
Expand Down
30 changes: 22 additions & 8 deletions lib/graph_conn/ws.ex
Original file line number Diff line number Diff line change
Expand Up @@ -111,20 +111,27 @@ defmodule GraphConn.WS do
}
)

response = _async_response(conn_pid, stream_ref)
success? = response == :ok
{success?, response} =
conn_pid
|> _async_response(stream_ref)
|> case do
{:ok, stream_ref} -> {true, {:ok, stream_ref}}
error -> {false, error}
end

Instrumenter.execute(
:ws_upgrade,
%{time: DateTime.utc_now(), duration: Instrumenter.duration(mono_start)},
%{node: Node.self(), success: success?}
)

if response == :ok,
do: {:ok, stream_ref},
else: response
response
end

@spec ping(pid(), reference()) :: :ok
def ping(conn_pid, stream_ref),
do: :ok = :gun.ws_send(conn_pid, stream_ref, :ping)

@spec push(pid(), reference(), String.t()) :: :ok
def push(conn_pid, stream_ref, body) do
Logger.debug(fn -> "[GraphConn.WS] Pushing #{inspect(body)}" end)
Expand All @@ -143,7 +150,10 @@ defmodule GraphConn.WS do
)
end

@spec _async_response(pid(), reference()) :: Response.t() | :ok | {:error, any()}
@spec _async_response(pid(), reference() | [reference()]) ::
Response.t()
| {:ok, reference() | [reference()]}
| {:error, any()}
defp _async_response(conn_pid, stream_ref) do
Logger.debug("Waiting for response")

Expand All @@ -160,8 +170,8 @@ defmodule GraphConn.WS do
{:error, reason}
end

{:gun_upgrade, ^conn_pid, ^stream_ref, ["websocket"], _response_headers} ->
:ok
{:gun_upgrade, ^conn_pid, stream_refs, ["websocket"], _response_headers} ->
{:ok, stream_refs}

{:gun_error, ^conn_pid, ^stream_ref, reason} ->
{:error, reason}
Expand All @@ -174,6 +184,10 @@ defmodule GraphConn.WS do

{:DOWN, _monitor_ref, :process, ^conn_pid, reason} ->
{:error, reason}

{:gun_tunnel_up, ^conn_pid, new_stream_ref, :http} ->
refs = List.flatten([new_stream_ref, stream_ref])
_async_response(conn_pid, refs)
after
5_000 ->
{:error, :recv_timeout}
Expand Down
14 changes: 14 additions & 0 deletions lib/graph_conn/ws_connection.ex
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,14 @@ defmodule GraphConn.WsConnection do
{:noreply, %{state | last_pong: DateTime.utc_now()}}
end

def handle_info(:send_ping, %State{} = state) do
Logger.debug("[WsConnection] Sending ping")

WS.ping(state.conn_pid, state.stream_ref)
Process.send_after(self(), :send_ping, Keyword.get(state.ws_ping, :interval_in_ms))
{:noreply, state}
end

def handle_info(:check_last_pong, %State{} = state) do
Logger.debug("[WsConnection] checking last pong")

Expand Down Expand Up @@ -226,6 +234,12 @@ defmodule GraphConn.WsConnection do
Logger.info("Upgrading connection...")
{:ok, stream_ref} = WS.ws_upgrade(conn_pid, path, subprotocol, token)
Logger.info("WebSocket upgrade succeeded.")

if Application.get_env(:graph_conn, :proxy) do
# Something's wrong with sending pings when connected via proxy so we need to do that on our own.
Process.send_after(self(), :send_ping, Keyword.get(state.ws_ping, :interval_in_ms))
end

Process.send_after(self(), :check_last_pong, Keyword.get(state.ws_ping, :interval_in_ms))
%State{state | stream_ref: stream_ref}
end
Expand Down
4 changes: 2 additions & 2 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ defmodule GraphConn.MixProject do
defp _deps do
[
{:elixir_uuid, "~> 1.2"},
# {:gun, "~> 2.0.1"},
{:gun, github: "ninenines/gun", tag: "2.1.0"},
# {:gun, "~> 2.1.0"},
{:gun, github: "burmajam/gun", branch: "fix-proxy-problem"},
{:finch, "~> 0.10"},
{:ssl_verify_fun, "~> 1.1"},
{:certifi, "~> 2.12"},
Expand Down
2 changes: 1 addition & 1 deletion mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"ex_doc": {:hex, :ex_doc, "0.33.0", "690562b153153c7e4d455dc21dab86e445f66ceba718defe64b0ef6f0bd83ba0", [:mix], [{:earmark_parser, "~> 1.4.39", [hex: :earmark_parser, repo: "hexpm", optional: false]}, {:makeup_c, ">= 0.1.0", [hex: :makeup_c, repo: "hexpm", optional: true]}, {:makeup_elixir, "~> 0.14 or ~> 1.0", [hex: :makeup_elixir, repo: "hexpm", optional: false]}, {:makeup_erlang, "~> 0.1 or ~> 1.0", [hex: :makeup_erlang, repo: "hexpm", optional: false]}, {:makeup_html, ">= 0.1.0", [hex: :makeup_html, repo: "hexpm", optional: true]}], "hexpm", "3f69adc28274cb51be37d09b03e4565232862a4b10288a3894587b0131412124"},
"excoveralls": {:hex, :excoveralls, "0.18.1", "a6f547570c6b24ec13f122a5634833a063aec49218f6fff27de9df693a15588c", [:mix], [{:castore, "~> 1.0", [hex: :castore, repo: "hexpm", optional: true]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "d65f79db146bb20399f23046015974de0079668b9abb2f5aac074d078da60b8d"},
"finch": {:hex, :finch, "0.18.0", "944ac7d34d0bd2ac8998f79f7a811b21d87d911e77a786bc5810adb75632ada4", [:mix], [{:castore, "~> 0.1 or ~> 1.0", [hex: :castore, repo: "hexpm", optional: false]}, {:mime, "~> 1.0 or ~> 2.0", [hex: :mime, repo: "hexpm", optional: false]}, {:mint, "~> 1.3", [hex: :mint, repo: "hexpm", optional: false]}, {:nimble_options, "~> 0.4 or ~> 1.0", [hex: :nimble_options, repo: "hexpm", optional: false]}, {:nimble_pool, "~> 0.2.6 or ~> 1.0", [hex: :nimble_pool, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "69f5045b042e531e53edc2574f15e25e735b522c37e2ddb766e15b979e03aa65"},
"gun": {:git, "https://github.com/ninenines/gun.git", "5dff085cd14923fb77eda9849accd3d7b9bd8ff7", [tag: "2.1.0"]},
"gun": {:git, "https://github.com/burmajam/gun.git", "12115018ec09bc80a336a8b7bf54c94dd697317c", [branch: "fix-proxy-problem"]},
"hpax": {:hex, :hpax, "0.2.0", "5a58219adcb75977b2edce5eb22051de9362f08236220c9e859a47111c194ff5", [:mix], [], "hexpm", "bea06558cdae85bed075e6c036993d43cd54d447f76d8190a8db0dc5893fa2f1"},
"jason": {:hex, :jason, "1.4.1", "af1504e35f629ddcdd6addb3513c3853991f694921b1b9368b0bd32beb9f1b63", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "fbb01ecdfd565b56261302f7e1fcc27c4fb8f32d56eab74db621fc154604a7a1"},
"jumper": {:hex, :jumper, "1.0.2", "68cdcd84472a00ac596b4e6459a41b3062d4427cbd4f1e8c8793c5b54f1406a7", [:mix], [], "hexpm", "9b7782409021e01ab3c08270e26f36eb62976a38c1aa64b2eaf6348422f165e1"},
Expand Down

0 comments on commit 44e822f

Please sign in to comment.