Skip to content

Commit c6d7c38

Browse files
committed
fix timeout value if using the streaming_hello_monitor.ex, clarify usage
1 parent 44c6dbf commit c6d7c38

File tree

6 files changed

+34
-14
lines changed

6 files changed

+34
-14
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99
* Bugfix
1010
* fix a crash in the streaming hello monitor, if the server sends more than one response at once
1111
* add support for the new hello handshake
12+
* refactor :timeout option (thanks to JD-Robertson for reporting)
1213

1314
## 0.9.1 (2022-05-27)
1415
* Bugfix

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,26 @@ you'll want to add this cipher to your `ssl_opts`:
488488
)
489489
```
490490
491+
## Timeout
492+
493+
The `:timeout` option is only used to specify the timeout for receiving data on the tcp layer. The default value is 15 seconds.
494+
The connection pool defines separate timeout values.
495+
You can use the `:timeout` as a global option to override the default value:
496+
497+
```elixir
498+
# Starts an pooled connection
499+
{:ok, conn} = Mongo.start_link(url: "mongodb://localhost:27017/db-name", timeout: 60_000)
500+
```
501+
502+
Each single connection uses `60_000` (60 seconds) as the timeout value instead of `15_000`. But you can override the default value by
503+
using the `:timeout` option, when running a single command:
504+
505+
```elixr
506+
Mongo.find(conn, "dogs", %{}, timeout: 120_000)
507+
```
508+
509+
Now the driver will use 120 seconds for the timeout.
510+
491511
## Change Streams
492512
493513
Change streams are available in replica set and sharded cluster deployments

lib/mongo.ex

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ defmodule Mongo do
6767
alias Mongo.Events.CommandFailedEvent
6868
alias Mongo.Error
6969

70-
@timeout 15_000
7170
@retry_timeout_seconds 120
7271

7372
@type conn :: DbConnection.Conn
@@ -1504,7 +1503,7 @@ defmodule Mongo do
15041503
{:ok, BSON.document() | nil} | {:error, Mongo.Error.t()}
15051504
def exec_command_session(session, cmd, opts) do
15061505
with {:ok, conn, new_cmd} <- Session.bind_session(session, cmd),
1507-
{:ok, _cmd, response} <- DBConnection.execute(conn, %Query{action: {:command, new_cmd}}, [], defaults(opts)),
1506+
{:ok, _cmd, response} <- DBConnection.execute(conn, %Query{action: {:command, new_cmd}}, [], opts),
15081507
:ok <- Session.update_session(session, response, opts),
15091508
{:ok, {_flags, doc}} <- check_for_error(response, cmd, opts) do
15101509
{:ok, doc}
@@ -1518,25 +1517,25 @@ defmodule Mongo do
15181517
@spec exec_command(GenServer.server(), BSON.document(), Keyword.t()) ::
15191518
{:ok, {any(), BSON.document()} | nil} | {:error, Mongo.Error.t()}
15201519
def exec_command(conn, cmd, opts) do
1521-
with {:ok, _cmd, response} <- DBConnection.execute(conn, %Query{action: {:command, cmd}}, [], defaults(opts)) do
1520+
with {:ok, _cmd, response} <- DBConnection.execute(conn, %Query{action: {:command, cmd}}, [], opts) do
15221521
check_for_error(response, cmd, opts)
15231522
end
15241523
end
15251524

15261525
def exec_hello(conn, opts) do
1527-
with {:ok, _cmd, response} <- DBConnection.execute(conn, %Query{action: {:exec_hello, []}}, [], defaults(opts)) do
1526+
with {:ok, _cmd, response} <- DBConnection.execute(conn, %Query{action: {:exec_hello, []}}, [], opts) do
15281527
check_for_error(response, [hello: 1], opts)
15291528
end
15301529
end
15311530

15321531
def exec_hello(conn, cmd, opts) do
1533-
with {:ok, _cmd, response} <- DBConnection.execute(conn, %Query{action: {:exec_hello, cmd}}, [], defaults(opts)) do
1532+
with {:ok, _cmd, response} <- DBConnection.execute(conn, %Query{action: {:exec_hello, cmd}}, [], opts) do
15341533
check_for_error(response, cmd, opts)
15351534
end
15361535
end
15371536

15381537
def exec_more_to_come(conn, opts) do
1539-
with {:ok, _cmd, response} <- DBConnection.execute(conn, %Query{action: :more_to_come}, [], defaults(opts)) do
1538+
with {:ok, _cmd, response} <- DBConnection.execute(conn, %Query{action: :more_to_come}, [], opts) do
15401539
check_for_error(response, [:more_to_come], opts)
15411540
end
15421541
end
@@ -1747,10 +1746,6 @@ defmodule Mongo do
17471746
defp assert_many_docs!(other),
17481747
do: raise(ArgumentError, "expected list of documents, got: #{inspect(other)}")
17491748

1750-
defp defaults(opts) do
1751-
Keyword.put_new(opts, :timeout, @timeout)
1752-
end
1753-
17541749
## support for logging like Ecto
17551750
defp do_log([:more_to_come], _duration, _opts) do
17561751
:ok

lib/mongo/mongo_db_connection.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ defmodule Mongo.MongoDBConnection do
1414
alias Mongo.Events.MoreToComeEvent
1515
alias Mongo.StableVersion
1616

17-
@timeout 5_000
17+
@timeout 15_000
1818
@find_one_flags ~w(slave_ok exhaust partial)a
1919
@write_concern ~w(w j wtimeout)a
2020
@insecure_cmds [:authenticate, :saslStart, :saslContinue, :getnonce, :createUser, :updateUser, :copydbgetnonce, :copydbsaslstart, :copydb, :isMaster, :ismaster, :hello]

lib/mongo/streaming_hello_monitor.ex

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,10 +151,15 @@ defmodule Mongo.StreamingHelloMonitor do
151151
end
152152

153153
defp hello_command(conn_pid, %{"counter" => counter, "processId" => process_id}, opts) do
154-
opts = Keyword.merge(opts, flags: [:exhaust_allowed])
154+
maxAwaitTimeMS = Keyword.get(opts, :max_await_time_ms, 10_000)
155+
156+
opts =
157+
opts
158+
|> Keyword.merge(flags: [:exhaust_allowed])
159+
|> Keyword.merge(timeout: maxAwaitTimeMS * 2)
155160

156161
cmd = [
157-
maxAwaitTimeMS: Keyword.get(opts, :max_await_time_ms, 10_000),
162+
maxAwaitTimeMS: maxAwaitTimeMS,
158163
topologyVersion: %{
159164
counter: %BSON.LongNumber{value: counter},
160165
processId: process_id

lib/mongo_db_connection/utils.ex

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,6 @@ defmodule Mongo.MongoDBConnection.Utils do
126126
{:ok, id, reply}
127127

128128
:error ->
129-
## todo
130129
case mod.recv(socket, 0, state.timeout) do
131130
{:ok, tail} -> recv_data(header, [data | tail], state)
132131
{:error, reason} -> recv_error(reason, state)

0 commit comments

Comments
 (0)