Skip to content

Commit

Permalink
Remove open_direct from Connection module as it doesn't work with
Browse files Browse the repository at this point in the history
the latest rabbit_client. This commit can be reverted once the bug
is fixed.

#73
  • Loading branch information
ono committed Feb 7, 2018
1 parent 2cd2bbf commit 836978f
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 115 deletions.
66 changes: 0 additions & 66 deletions lib/amqp/connection.ex
Original file line number Diff line number Diff line change
Expand Up @@ -101,72 +101,6 @@ defmodule AMQP.Connection do
end
end

@doc """
Opens an new direct Connection to an AMQP broker.
Direct connection is the special type of connection that is
supported by RabbitMQ broker, where Erlang distribution protocol is
used to communicate with broker. It's a bit faster than the regular
AMQP protocol, as there is no need to serialize and deserialize AMQP
frames (especially when we are using this library at the same BEAM
node where the RabbitMQ runs). But it's less secure, as giving
direct access to a client means it has full control over RabbitMQ
node.
The connections created by this function are not restaretd
automatically, see open/1 for more details.
The connection parameters are passed as a keyword list with the
following options available:
# Options
* `:username` - The name of a user registered with the broker (defaults to `:none`);
* `:password` - The password of the user (defaults to `:none`);
* `:virtual_host` - The name of a virtual host in the broker (defaults to \"/\");
* `:node` - Erlang node name to connect to (defaults to the current node);
* `:client_properties` - A list of extra client properties to be sent to the server, defaults to `[]`;
# Adapter options
Additional details can be provided when a direct connection is used
to provide connectivity for some non-AMQP protocol (like it happens
in STOMP and MQTT plugins for RabbitMQ). We assume that you know
what you are doing in this case, here is the options that maps to
corresponding fields of `#amqp_adapter_info{}` record:
`:adapter_host`, `:adapter_port`, `:adapter_peer_host`,
`:adapter_peer_port`, `:adapter_name`, `:adapter_protocol`,
`:adapter_additional_info`.
## Examples
AMQP.Connection.open_direct node: :rabbit@localhost
{:ok, %AMQP.Connection{}}
"""
@spec open_direct(keyword) :: {:ok, t} | {:error, atom}
def open_direct(options \\ [])

def open_direct(options) when is_list(options) do
adapter_info = amqp_adapter_info(
host: Keyword.get(options, :adapter_host, :unknown),
port: Keyword.get(options, :adapter_port, :unknown),
peer_host: Keyword.get(options, :adapter_peer_host, :unknown),
peer_port: Keyword.get(options, :adapter_peer_port, :unknown),
name: Keyword.get(options, :adapter_name, :unknown),
protocol: Keyword.get(options, :adapter_protocol, :unknown),
additional_info: Keyword.get(options, :adapter_additional_info, []))

amqp_params = amqp_params_direct(
username: Keyword.get(options, :username, :none),
password: Keyword.get(options, :password, :none),
virtual_host: Keyword.get(options, :virtual_host, "/"),
node: Keyword.get(options, :node, node()),
adapter_info: adapter_info,
client_properties: Keyword.get(options, :client_properties, []))

do_open(amqp_params)
end

@doc """
Closes an open Connection.
"""
Expand Down
49 changes: 0 additions & 49 deletions test/connection_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,6 @@ defmodule ConnectionTest do

alias AMQP.Connection

@tag :skip_ci
# This test assumes that we have a running RabbitMQ with short
# nodename 'rabbit@localhost', and that it uses the same Erlang
# cookie as we do. And it's better to have RabbitMQ of version
# equal to that of amqp_client library used. We can achieve this
# with following sequence of commands under the same user that
# will run the test-suite:
#
# wget https://github.com/rabbitmq/rabbitmq-server/releases/download/rabbitmq_v3_6_9/rabbitmq-server-generic-unix-3.6.9.tar.xz
# tar xJvf rabbitmq-server-generic-unix-3.6.9.tar.xz
# cd rabbitmq_server-3.6.9
# RABBITMQ_NODENAME=rabbit@localhost ./sbin/rabbitmq-server
test "open direct connection" do
:ok = ensure_distribution()
assert {:ok, conn} = Connection.open_direct node: :rabbit@localhost
assert :ok = Connection.close(conn)
end

test "open connection with default settings" do
assert {:ok, conn} = Connection.open
assert :ok = Connection.close(conn)
Expand All @@ -40,35 +22,4 @@ defmodule ConnectionTest do
assert {:ok, conn} = Connection.open "amqp://localhost"
assert :ok = Connection.close(conn)
end

defp ensure_distribution() do
# Attempt to start distribution with a name randomly selected from
# a small pool of names (to prevent atom table exhausting in
# servers we'll connect to). Several attemps are made in case we
# are running some tests in parallel. This is taken almost
# directly from rabbit_cli.erl (skipping a bizzare FQDN magic).
case node() do
:nonode@nohost -> start_distribution_anon(10, :undefined)
_ -> :ok
end
end

defp start_distribution_anon(0, last_error) do
{:error, last_error}
end
defp start_distribution_anon(tries_left, _) do
name_candidate = generate_node_name()
case :net_kernel.start([name_candidate, :shortnames]) do
{:ok, _} -> :ok
{:error, reason} ->
start_distribution_anon(tries_left - 1, reason)
end
end

defp generate_node_name() do
:io_lib.format("amqp-mix-test-~2..0b", [:rand.uniform(100)])
|> :lists.flatten
|> :erlang.list_to_atom
end

end

0 comments on commit 836978f

Please sign in to comment.