-
Notifications
You must be signed in to change notification settings - Fork 95
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added socket_opts to WebSockex.Conn struct with possible keys :connec…
…t_timeout and :recv_timeout. Issue #10
- Loading branch information
Showing
2 changed files
with
30 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,7 @@ defmodule WebSockex.Conn do | |
extra_headers: [], | ||
transport: nil, | ||
socket: nil, | ||
socket_opts: [], | ||
cacerts: nil, | ||
insecure: true | ||
|
||
|
@@ -35,12 +36,25 @@ defmodule WebSockex.Conn do | |
`:insecure` option is `false` (has no effect when `:insecure is true`). | ||
These certifications need a list of decoded binaries. See the | ||
[Erlang `:public_key` module][public_key] for more information. | ||
- `:socket_opts` - Keyword list of options for the socket. | ||
[public_key]: http://erlang.org/doc/apps/public_key/using_public_key.html | ||
""" | ||
@type connection_option :: {:extra_headers, [header]} | | ||
{:cacerts, [certification]} | | ||
{:insecure, boolean} | ||
{:insecure, boolean} | | ||
{:socket_opts, [socket_option]} | ||
|
||
@typedoc """ | ||
Options used when interacting with the socket. | ||
- `:connect_timeout` - Timeout in ms for creating a connection. | ||
- `:recv_timeout` - Timeout in ms for receiving from socket. | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
Azolo
Owner
|
||
[public_key]: http://erlang.org/doc/apps/public_key/using_public_key.html | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong. |
||
""" | ||
@type socket_option :: {:connect_timeout, non_neg_integer} | | ||
{:recv_timeout, non_neg_integer} | ||
|
||
@type t :: %__MODULE__{conn_mod: :gen_tcp | :ssl, | ||
host: String.t, | ||
|
@@ -49,7 +63,8 @@ defmodule WebSockex.Conn do | |
query: String.t | nil, | ||
extra_headers: [header], | ||
transport: transport, | ||
socket: socket} | ||
socket: socket, | ||
socket_opts: [socket_option]} | ||
|
||
@doc """ | ||
Returns a new `WebSockex.Conn` struct from a uri and options. | ||
|
@@ -66,7 +81,8 @@ defmodule WebSockex.Conn do | |
transport: transport(mod), | ||
extra_headers: Keyword.get(opts, :extra_headers, []), | ||
cacerts: Keyword.get(opts, :cacerts, nil), | ||
insecure: Keyword.get(opts, :insecure, true)} | ||
insecure: Keyword.get(opts, :insecure, true), | ||
socket_opts: Keyword.get(opts, :socket_opts, [])} | ||
end | ||
|
||
@doc """ | ||
|
@@ -89,7 +105,7 @@ defmodule WebSockex.Conn do | |
case :gen_tcp.connect(String.to_charlist(conn.host), | ||
conn.port, | ||
[:binary, active: false, packet: 0], | ||
6000) do | ||
Keyword.get(conn.socket_opts, :connect_timeout, 6000)) do | ||
{:ok, socket} -> | ||
{:ok, Map.put(conn, :socket, socket)} | ||
{:error, error} -> | ||
|
@@ -100,7 +116,7 @@ defmodule WebSockex.Conn do | |
case :ssl.connect(String.to_charlist(conn.host), | ||
conn.port, | ||
ssl_connection_options(conn), | ||
6000) do | ||
Keyword.get(conn.socket_opts, :connect_timeout, 6000)) do | ||
{:ok, socket} -> | ||
{:ok, Map.put(conn, :socket, socket)} | ||
{:error, error} -> | ||
|
@@ -182,10 +198,11 @@ defmodule WebSockex.Conn do | |
defp conn_module("wss"), do: :ssl | ||
|
||
defp wait_for_response(conn, buffer \\ "") do | ||
recv_timeout = Keyword.get(conn.socket_opts, :recv_timeout, 5000) | ||
case Regex.match?(~r/\r\n\r\n/, buffer) do | ||
true -> {:ok, buffer} | ||
false -> | ||
with {:ok, data} <- conn.conn_mod.recv(conn.socket, 0, 5000) do | ||
with {:ok, data} <- conn.conn_mod.recv(conn.socket, 0, recv_timeout) do | ||
wait_for_response(conn, buffer <> data) | ||
else | ||
{:error, reason} -> {:error, %WebSockex.ConnError{original: reason}} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Maybe add the default times for the timeouts and specify that the timeouts are only used during connecting.