Skip to content

Commit 6854323

Browse files
authored
Add notes regarding closing sessions when working with streams (#252)
1 parent c6c3c95 commit 6854323

File tree

3 files changed

+19
-2
lines changed

3 files changed

+19
-2
lines changed

lib/mongo.ex

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,19 @@ defmodule Mongo do
418418
end
419419

420420
@doc """
421-
Performs aggregation operation using the aggregation pipeline.
421+
Performs aggregation operation using the aggregation pipeline and returns a Mongo.Stream.
422+
It should be noted that code that uses the paginated query results without engaging Mongo.Streams Enumerable behavior
423+
can result in the sessions hanging around and causing resource exhaustion.
424+
425+
Example:
426+
# Results in an open session
427+
%Mongo.Stream{docs: docs} = Mongo.aggregate(@topology, collection, pipeline, opts)
428+
docs |> Enum.map(fn elem -> elem end)
429+
430+
# Results in a closed session via the Enumerable protocol
431+
Mongo.aggregate(@topology, collection, pipeline, opts)
432+
|> Enum.map(fn elem -> elem end)
433+
422434
423435
For all options see [Options](https://docs.mongodb.com/manual/reference/command/aggregate/#aggregate)
424436

lib/mongo/session.ex

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ defmodule Mongo.Session do
6262
6363
For more information about causal consistency see the [officially documentation](https://docs.mongodb.com/manual/core/read-isolation-consistency-recency/#causal-consistency).
6464
65+
Note that Mongo.Stream implements the Enumerable protocol and the reduce/3 function calls Mongo.Stream.checkin_session/3 after the stream is exhausted.
66+
6567
If you want to use transaction, then you need to create a session as well:
6668
6769
alias Mongo.Session

lib/mongo/stream.ex

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
defmodule Mongo.Stream do
2-
@moduledoc false
2+
@moduledoc """
3+
This module provides a stream implementation that automatically checks out a session when the stream is started and an Enumerable
4+
protocol that checks it back in when the stream has been consumed.
5+
"""
36

47
alias Mongo.Session
58
alias Mongo.Error

0 commit comments

Comments
 (0)