Skip to content

Commit

Permalink
Bump :gun to 2.0.0-rc.1.
Browse files Browse the repository at this point in the history
This prevents dependency problems when trying to use nostrum with e.g. Phoenix,
as outlined in #236. It also sets the foundation for using `:gun` as our one
and only HTTP client, instead of relying on an extra library here.

Fixes #236.
  • Loading branch information
jchristgit committed May 18, 2021
1 parent 2adbb30 commit c3e2d99
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 15 deletions.
15 changes: 8 additions & 7 deletions lib/nostrum/shard/session.ex
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ defmodule Nostrum.Shard.Session do
conn_pid: self(),
conn: worker,
shard_num: shard_num,
stream: stream,
gateway: gateway <> @gateway_qs,
last_heartbeat_ack: DateTime.utc_now(),
heartbeat_ack: true,
Expand Down Expand Up @@ -101,7 +102,7 @@ defmodule Nostrum.Shard.Session do
end
end

def handle_info({:gun_ws, _worker, _stream, {:binary, frame}}, state) do
def handle_info({:gun_ws, _worker, stream, {:binary, frame}}, state) do
payload =
state.zlib_ctx
|> :zlib.inflate(frame)
Expand All @@ -117,7 +118,7 @@ defmodule Nostrum.Shard.Session do

case from_handle do
{new_state, reply} ->
:ok = :gun.ws_send(state.conn, {:binary, reply})
:ok = :gun.ws_send(state.conn, stream, {:binary, reply})
{:noreply, new_state}

new_state ->
Expand Down Expand Up @@ -149,24 +150,24 @@ defmodule Nostrum.Shard.Session do
end

def handle_cast({:status_update, payload}, state) do
:ok = :gun.ws_send(state.conn, {:binary, payload})
:ok = :gun.ws_send(state.conn, state.stream, {:binary, payload})
{:noreply, state}
end

def handle_cast({:update_voice_state, payload}, state) do
:ok = :gun.ws_send(state.conn, {:binary, payload})
:ok = :gun.ws_send(state.conn, state.stream, {:binary, payload})
{:noreply, state}
end

def handle_cast({:request_guild_members, payload}, state) do
:ok = :gun.ws_send(state.conn, {:binary, payload})
:ok = :gun.ws_send(state.conn, state.stream, {:binary, payload})
{:noreply, state}
end

def handle_cast(:heartbeat, %{heartbeat_ack: false, heartbeat_ref: timer_ref} = state) do
Logger.warn("heartbeat_ack not received in time, disconnecting")
{:ok, :cancel} = :timer.cancel(timer_ref)
:gun.ws_send(state.conn, :close)
:gun.ws_send(state.conn, state.stream, :close)
{:noreply, state}
end

Expand All @@ -177,7 +178,7 @@ defmodule Nostrum.Shard.Session do
:heartbeat
])

:ok = :gun.ws_send(state.conn, {:binary, Payload.heartbeat_payload(state.seq)})
:ok = :gun.ws_send(state.conn, state.stream, {:binary, Payload.heartbeat_payload(state.seq)})

{:noreply,
%{state | heartbeat_ref: ref, heartbeat_ack: false, last_heartbeat_send: DateTime.utc_now()}}
Expand Down
1 change: 1 addition & 0 deletions lib/nostrum/struct/voice_ws_state.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ defmodule Nostrum.Struct.VoiceWSState do
:token,
:conn,
:conn_pid,
:stream,
:gateway,
:identified,
:last_heartbeat_send,
Expand Down
5 changes: 5 additions & 0 deletions lib/nostrum/struct/ws_state.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ defmodule Nostrum.Struct.WSState do
:shard_pid,
:conn,
:conn_pid,
:stream,
:gateway,
:last_heartbeat_send,
:last_heartbeat_ack,
Expand Down Expand Up @@ -37,6 +38,9 @@ defmodule Nostrum.Struct.WSState do
@typedoc "PID of the connection process"
@type conn_pid :: pid

@typedoc "Stream reference for `:gun`"
@type stream :: reference()

@typedoc "Gateway URL"
@type gateway :: String.t()

Expand Down Expand Up @@ -71,6 +75,7 @@ defmodule Nostrum.Struct.WSState do
shard_pid: shard_pid,
conn: conn,
conn_pid: conn_pid,
stream: stream,
gateway: gateway,
last_heartbeat_send: last_heartbeat_send,
last_heartbeat_ack: last_heartbeat_ack,
Expand Down
11 changes: 6 additions & 5 deletions lib/nostrum/voice/session.ex
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ defmodule Nostrum.Voice.Session do
session: voice.session,
token: voice.token,
gateway: voice.gateway,
stream: stream,
last_heartbeat_ack: DateTime.utc_now(),
heartbeat_ack: true
}
Expand Down Expand Up @@ -85,7 +86,7 @@ defmodule Nostrum.Voice.Session do
GenServer.cast(pid, {:speaking, speaking, timed_out})
end

def handle_info({:gun_ws, _worker, _stream, {:text, frame}}, state) do
def handle_info({:gun_ws, _worker, stream, {:text, frame}}, state) do
payload =
frame
|> :erlang.iolist_to_binary()
Expand All @@ -99,7 +100,7 @@ defmodule Nostrum.Voice.Session do

case from_handle do
{new_state, reply} ->
:ok = :gun.ws_send(state.conn, {:text, reply})
:ok = :gun.ws_send(state.conn, stream, {:text, reply})
{:noreply, new_state}

new_state ->
Expand Down Expand Up @@ -140,7 +141,7 @@ defmodule Nostrum.Voice.Session do
def handle_cast(:heartbeat, %{heartbeat_ack: false, heartbeat_ref: timer_ref} = state) do
Logger.warn("heartbeat_ack not received in time, disconnecting")
{:ok, :cancel} = :timer.cancel(timer_ref)
:gun.ws_send(state.conn, :close)
:gun.ws_send(state.conn, state.stream, :close)
{:noreply, state}
end

Expand All @@ -151,7 +152,7 @@ defmodule Nostrum.Voice.Session do
:heartbeat
])

:ok = :gun.ws_send(state.conn, {:text, Payload.heartbeat_payload()})
:ok = :gun.ws_send(state.conn, state.stream, {:text, Payload.heartbeat_payload()})

{:noreply,
%{state | heartbeat_ref: ref, heartbeat_ack: false, last_heartbeat_send: DateTime.utc_now()}}
Expand All @@ -164,7 +165,7 @@ defmodule Nostrum.Voice.Session do

Producer.notify(Producer, speaking_update, state)

:ok = :gun.ws_send(state.conn, {:text, payload})
:ok = :gun.ws_send(state.conn, state.stream, {:text, payload})
{:noreply, state}
end

Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ defmodule Nostrum.Mixfile do
[
{:httpoison, "~> 1.7"},
{:poison, "~> 3.0"},
{:gun, "~> 1.3"},
{:gun, github: "ninenines/gun", tag: "2.0.0-rc.1"},
{:kcl, "~> 1.3"},
{:porcelain, "~> 2.0"},
{:ex_doc, "~> 0.14", only: :dev},
Expand Down
4 changes: 2 additions & 2 deletions mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"bunt": {:hex, :bunt, "0.2.0", "951c6e801e8b1d2cbe58ebbd3e616a869061ddadcc4863d0a2182541acae9a38", [:mix], [], "hexpm", "7af5c7e09fe1d40f76c8e4f9dd2be7cebd83909f31fee7cd0e9eadc567da8353"},
"certifi": {:hex, :certifi, "2.5.2", "b7cfeae9d2ed395695dd8201c57a2d019c0c43ecaf8b8bcb9320b40d6662f340", [:rebar3], [{:parse_trans, "~>3.3", [hex: :parse_trans, repo: "hexpm", optional: false]}], "hexpm", "3b3b5f36493004ac3455966991eaf6e768ce9884693d9968055aeeeb1e575040"},
"chacha20": {:hex, :chacha20, "1.0.2", "73c5e96eba5e94917603a43c5c7c6b049436a5d71d9d3182781c345d87d28c8b", [:mix], [], "hexpm", "549b89314cbffa0893ef923d999d625c227cab5f1301133598793225f02a3963"},
"cowlib": {:hex, :cowlib, "2.7.3", "a7ffcd0917e6d50b4d5fb28e9e2085a0ceb3c97dea310505f7460ff5ed764ce9", [:rebar3], [], "hexpm", "1e1a3d176d52daebbecbbcdfd27c27726076567905c2a9d7398c54da9d225761"},
"cowlib": {:git, "https://github.com/ninenines/cowlib", "d06fcad11a00be1c56d0a76e1c46c45b1ff71edd", [ref: "2.10.1"]},
"credo": {:hex, :credo, "1.5.4", "9914180105b438e378e94a844ec3a5088ae5875626fc945b7c1462b41afc3198", [:mix], [{:bunt, "~> 0.2.0", [hex: :bunt, repo: "hexpm", optional: false]}, {:file_system, "~> 0.2.8", [hex: :file_system, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "cf51af45eadc0a3f39ba13b56fdac415c91b34f7b7533a13dc13550277141bc4"},
"curve25519": {:hex, :curve25519, "1.0.3", "101248eeb2ed28f1b8d2d423cf560bfc99f5e22bae5a2bb113fd24efad05fae1", [:mix], [], "hexpm", "aa936079df2bcefb5cffa68840dc048f222fb7a5cb5827c735dacddba30e3b23"},
"dialyxir": {:hex, :dialyxir, "1.0.0", "6a1fa629f7881a9f5aaf3a78f094b2a51a0357c843871b8bc98824e7342d00a5", [:mix], [{:erlex, ">= 0.2.6", [hex: :erlex, repo: "hexpm", optional: false]}], "hexpm", "aeb06588145fac14ca08d8061a142d52753dbc2cf7f0d00fc1013f53f8654654"},
Expand All @@ -14,7 +14,7 @@
"ex_doc": {:hex, :ex_doc, "0.23.0", "a069bc9b0bf8efe323ecde8c0d62afc13d308b1fa3d228b65bca5cf8703a529d", [:mix], [{:earmark_parser, "~> 1.4.0", [hex: :earmark_parser, repo: "hexpm", optional: false]}, {:makeup_elixir, "~> 0.14", [hex: :makeup_elixir, repo: "hexpm", optional: false]}], "hexpm", "f5e2c4702468b2fd11b10d39416ddadd2fcdd173ba2a0285ebd92c39827a5a16"},
"file_system": {:hex, :file_system, "0.2.10", "fb082005a9cd1711c05b5248710f8826b02d7d1784e7c3451f9c1231d4fc162d", [:mix], [], "hexpm", "41195edbfb562a593726eda3b3e8b103a309b733ad25f3d642ba49696bf715dc"},
"gen_stage": {:hex, :gen_stage, "1.0.0", "51c8ae56ff54f9a2a604ca583798c210ad245f415115453b773b621c49776df5", [:mix], [], "hexpm", "1d9fc978db5305ac54e6f5fec7adf80cd893b1000cf78271564c516aa2af7706"},
"gun": {:hex, :gun, "1.3.3", "cf8b51beb36c22b9c8df1921e3f2bc4d2b1f68b49ad4fbc64e91875aa14e16b4", [:rebar3], [{:cowlib, "~> 2.7.0", [hex: :cowlib, repo: "hexpm", optional: false]}], "hexpm", "3106ce167f9c9723f849e4fb54ea4a4d814e3996ae243a1c828b256e749041e0"},
"gun": {:git, "https://github.com/ninenines/gun.git", "3a3e56fb66edaaa1a7093744a0fd8303b993b3c8", [tag: "2.0.0-rc.1"]},
"hackney": {:hex, :hackney, "1.16.0", "5096ac8e823e3a441477b2d187e30dd3fff1a82991a806b2003845ce72ce2d84", [:rebar3], [{:certifi, "2.5.2", [hex: :certifi, repo: "hexpm", optional: false]}, {:idna, "6.0.1", [hex: :idna, repo: "hexpm", optional: false]}, {:metrics, "1.0.1", [hex: :metrics, repo: "hexpm", optional: false]}, {:mimerl, "~>1.1", [hex: :mimerl, repo: "hexpm", optional: false]}, {:parse_trans, "3.3.0", [hex: :parse_trans, repo: "hexpm", optional: false]}, {:ssl_verify_fun, "1.1.6", [hex: :ssl_verify_fun, repo: "hexpm", optional: false]}], "hexpm", "3bf0bebbd5d3092a3543b783bf065165fa5d3ad4b899b836810e513064134e18"},
"httpoison": {:hex, :httpoison, "1.7.0", "abba7d086233c2d8574726227b6c2c4f6e53c4deae7fe5f6de531162ce9929a0", [:mix], [{:hackney, "~> 1.16", [hex: :hackney, repo: "hexpm", optional: false]}], "hexpm", "975cc87c845a103d3d1ea1ccfd68a2700c211a434d8428b10c323dc95dc5b980"},
"idna": {:hex, :idna, "6.0.1", "1d038fb2e7668ce41fbf681d2c45902e52b3cb9e9c77b55334353b222c2ee50c", [:rebar3], [{:unicode_util_compat, "0.5.0", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm", "a02c8a1c4fd601215bb0b0324c8a6986749f807ce35f25449ec9e69758708122"},
Expand Down

0 comments on commit c3e2d99

Please sign in to comment.