Skip to content

Commit

Permalink
Added socket_opts to WebSockex.Conn struct with possible keys :connec…
Browse files Browse the repository at this point in the history
…t_timeout and :recv_timeout. Issue #10
  • Loading branch information
h4cc committed Jun 1, 2017
1 parent 72aaad7 commit ddc50b4
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 9 deletions.
29 changes: 23 additions & 6 deletions lib/websockex/conn.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ defmodule WebSockex.Conn do
extra_headers: [],
transport: nil,
socket: nil,
socket_opts: [],
cacerts: nil,
insecure: true

Expand All @@ -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.

Copy link
@Azolo

Azolo Jun 1, 2017

Owner

Maybe add the default times for the timeouts and specify that the timeouts are only used during connecting.

This comment has been minimized.

Copy link
@Azolo

Azolo Jun 1, 2017

Owner

Also, I'm going to go more into this in the issue comments, but can we move these flatly into the Conn struct? Maybe as :socket_connect_timeout and :socket_recv_timeout.

This comment has been minimized.

Copy link
@h4cc

h4cc Jun 6, 2017

Author Contributor

Using a attribute for a single definiton.
Added values to structs, see PR.

[public_key]: http://erlang.org/doc/apps/public_key/using_public_key.html

This comment has been minimized.

Copy link
@Azolo

Azolo Jun 1, 2017

Owner

You don't need this markdown link.

This comment has been minimized.

Copy link
@h4cc

h4cc Jun 6, 2017

Author Contributor

Copy paste error, is gone now.

"""
@type socket_option :: {:connect_timeout, non_neg_integer} |
{:recv_timeout, non_neg_integer}

@type t :: %__MODULE__{conn_mod: :gen_tcp | :ssl,
host: String.t,
Expand All @@ -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.
Expand All @@ -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 """
Expand All @@ -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} ->
Expand All @@ -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} ->
Expand Down Expand Up @@ -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}}
Expand Down
10 changes: 7 additions & 3 deletions test/websockex/conn_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,18 @@ defmodule WebSockex.ConnTest do

test "new" do
regular_uri = URI.parse("ws://localhost/ws")
assert WebSockex.Conn.new(regular_uri, extra_headers: [{"Pineapple", "Cake"}]) ==
assert WebSockex.Conn.new(regular_uri,
extra_headers: [{"Pineapple", "Cake"}, ],
socket_opts: [connection_timeout: 123, recv_timeout: 123]) ==
%WebSockex.Conn{host: "localhost",
port: 80,
path: "/ws",
query: nil,
conn_mod: :gen_tcp,
transport: :tcp,
extra_headers: [{"Pineapple", "Cake"}],
socket: nil}
socket: nil,
socket_opts: [connection_timeout: 123, recv_timeout: 123]}

ssl_uri = URI.parse("wss://localhost/ws")
assert WebSockex.Conn.new(ssl_uri, extra_headers: [{"Pineapple", "Cake"}]) ==
Expand All @@ -36,7 +39,8 @@ defmodule WebSockex.ConnTest do
conn_mod: :ssl,
transport: :ssl,
extra_headers: [{"Pineapple", "Cake"}],
socket: nil}
socket: nil,
socket_opts: []}
end

test "open_socket", context do
Expand Down

0 comments on commit ddc50b4

Please sign in to comment.