Skip to content

Commit 82b7c99

Browse files
committed
Merge branch 'hello_command'
# Conflicts: # lib/mongo/mongo_db_connection.ex
2 parents 91dfef8 + fff14a4 commit 82b7c99

File tree

5 files changed

+28
-16
lines changed

5 files changed

+28
-16
lines changed

lib/mongo.ex

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1504,7 +1504,7 @@ defmodule Mongo do
15041504
{:ok, BSON.document() | nil} | {:error, Mongo.Error.t()}
15051505
def exec_command_session(session, cmd, opts) do
15061506
with {:ok, conn, new_cmd} <- Session.bind_session(session, cmd),
1507-
{:ok, _cmd, response} <- DBConnection.execute(conn, %Query{action: :command}, [new_cmd], defaults(opts)),
1507+
{:ok, _cmd, response} <- DBConnection.execute(conn, %Query{action: {:command, new_cmd}}, [], defaults(opts)),
15081508
:ok <- Session.update_session(session, response, opts),
15091509
{:ok, {_flags, doc}} <- check_for_error(response, cmd, opts) do
15101510
{:ok, doc}
@@ -1518,7 +1518,19 @@ defmodule Mongo do
15181518
@spec exec_command(GenServer.server(), BSON.document(), Keyword.t()) ::
15191519
{:ok, {any(), BSON.document()} | nil} | {:error, Mongo.Error.t()}
15201520
def exec_command(conn, cmd, opts) do
1521-
with {:ok, _cmd, response} <- DBConnection.execute(conn, %Query{action: :command}, [cmd], defaults(opts)) do
1521+
with {:ok, _cmd, response} <- DBConnection.execute(conn, %Query{action: {:command, cmd}}, [], defaults(opts)) do
1522+
check_for_error(response, cmd, opts)
1523+
end
1524+
end
1525+
1526+
def exec_hello(conn, opts) do
1527+
with {:ok, _cmd, response} <- DBConnection.execute(conn, %Query{action: {:exec_hello, []}}, [], defaults(opts)) do
1528+
check_for_error(response, [hello: 1], opts)
1529+
end
1530+
end
1531+
1532+
def exec_hello(conn, cmd, opts) do
1533+
with {:ok, _cmd, response} <- DBConnection.execute(conn, %Query{action: {:exec_hello, cmd}}, [], defaults(opts)) do
15221534
check_for_error(response, cmd, opts)
15231535
end
15241536
end
@@ -1536,7 +1548,7 @@ defmodule Mongo do
15361548
end
15371549

15381550
def exec_more_to_come(conn, opts) do
1539-
with {:ok, _cmd, response} <- DBConnection.execute(conn, %Query{action: :command}, [:more_to_come], defaults(opts)) do
1551+
with {:ok, _cmd, response} <- DBConnection.execute(conn, %Query{action: :more_to_come}, [], defaults(opts)) do
15401552
check_for_error(response, [:more_to_come], opts)
15411553
end
15421554
end

lib/mongo/event_handler.ex

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,13 @@ defmodule Mongo.EventHandler do
2424

2525
def listen(opts) do
2626
receive do
27-
{:broadcast, :commands, %{command_name: cmd} = message} when cmd != :isMaster ->
27+
{:broadcast, :commands, %{command_name: cmd} = message} when cmd != :isMaster and cmd != :hello ->
2828
Logger.info("Received command: " <> inspect(message))
2929
listen(opts)
3030

31-
{:broadcast, :commands, is_master} ->
32-
case opts[:is_master] do
33-
true -> Logger.info("Received is master:" <> inspect(is_master))
31+
{:broadcast, :commands, hello} ->
32+
case opts[:is_master] || opts[:hello] do
33+
true -> Logger.info("Received hello:" <> inspect(hello))
3434
_ -> []
3535
end
3636

lib/mongo/mongo_db_connection.ex

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,10 @@ defmodule Mongo.MongoDBConnection do
9595
end
9696

9797
@impl true
98-
def handle_execute(%Mongo.Query{action: action} = query, params, opts, original_state) do
98+
def handle_execute(%Mongo.Query{action: action} = query, _params, opts, original_state) do
9999
tmp_state = %{original_state | database: Keyword.get(opts, :database, original_state.database)}
100100

101-
with {:ok, reply, tmp_state} <- execute_action(action, params, opts, tmp_state) do
101+
with {:ok, reply, tmp_state} <- send_command(action, opts, tmp_state) do
102102
{:ok, query, reply, Map.put(tmp_state, :database, original_state.database)}
103103
end
104104
end
@@ -264,7 +264,7 @@ defmodule Mongo.MongoDBConnection do
264264
##
265265
# Executes a hello or the legacy hello command
266266
##
267-
defp execute_action({:exec_hello, cmd}, _params, opts, %{use_op_msg: true} = state) do
267+
defp send_command({:exec_hello, cmd}, opts, %{use_op_msg: true} = state) do
268268
db = opts[:database] || state.database
269269
timeout = Keyword.get(opts, :timeout, state.timeout)
270270
flags = Keyword.get(opts, :flags, 0x0)
@@ -297,7 +297,7 @@ defmodule Mongo.MongoDBConnection do
297297
##
298298
# Executes a more to come command
299299
##
300-
defp execute_action(:command, [:more_to_come], opts, %{use_op_msg: true} = state) do
300+
defp send_command(:more_to_come, opts, %{use_op_msg: true} = state) do
301301
event = %MoreToComeEvent{command: :more_to_come, command_name: opts[:command_name] || :more_to_come}
302302

303303
timeout = Keyword.get(opts, :timeout, state.timeout)
@@ -313,7 +313,7 @@ defmodule Mongo.MongoDBConnection do
313313
end
314314
end
315315

316-
defp execute_action(:command, [cmd], opts, %{use_op_msg: true} = state) do
316+
defp send_command({:command, cmd}, opts, %{use_op_msg: true} = state) do
317317
{command_name, data} = provide_cmd_data(cmd)
318318
db = opts[:database] || state.database
319319
cmd = cmd ++ ["$db": db]
@@ -350,7 +350,7 @@ defmodule Mongo.MongoDBConnection do
350350
end
351351
end
352352

353-
defp execute_action(:command, [cmd], opts, state) do
353+
defp send_command({:command, cmd}, opts, state) do
354354
[{command_name, _} | _] = cmd
355355

356356
event = %CommandStartedEvent{
@@ -376,7 +376,7 @@ defmodule Mongo.MongoDBConnection do
376376
end
377377
end
378378

379-
defp execute_action(:error, _query, _opts, state) do
379+
defp send_command(:error, _opts, state) do
380380
exception = Mongo.Error.exception("Test-case")
381381
{:disconnect, exception, state}
382382
end

lib/mongo/monitor.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ defmodule Mongo.Monitor do
55
If the network connection is working, then the monitor process reports this and the topology process starts the
66
connection pool. Per server we get 1 + pool size connections to each server.
77
8-
After waiting for `heartbeat_frequency_ms` milliseconds, the monitor process calls `isMaster` command and
8+
After waiting for `heartbeat_frequency_ms` milliseconds, the monitor process calls `hello` command and
99
reports the result to the topology process.
1010
1111
The result of the hello command is mapped the `ServerDescription` structure and sent to the topology process, which

lib/mongo/streaming_hello_monitor.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ defmodule Mongo.StreamingHelloMonitor do
7878
end
7979

8080
@doc """
81-
Time to update the topology. Calling `isMaster` and updating the server description
81+
Time to update the topology. Calling `hello` and updating the server description
8282
"""
8383
def handle_cast(:update, state) do
8484
handle_info(:update, state)

0 commit comments

Comments
 (0)