Skip to content

Commit

Permalink
Add notes regarding closing sessions when working with streams (#252)
Browse files Browse the repository at this point in the history
  • Loading branch information
rossgeesman authored Sep 20, 2024
1 parent c6c3c95 commit 6854323
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
14 changes: 13 additions & 1 deletion lib/mongo.ex
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,19 @@ defmodule Mongo do
end

@doc """
Performs aggregation operation using the aggregation pipeline.
Performs aggregation operation using the aggregation pipeline and returns a Mongo.Stream.
It should be noted that code that uses the paginated query results without engaging Mongo.Streams Enumerable behavior
can result in the sessions hanging around and causing resource exhaustion.
Example:
# Results in an open session
%Mongo.Stream{docs: docs} = Mongo.aggregate(@topology, collection, pipeline, opts)
docs |> Enum.map(fn elem -> elem end)
# Results in a closed session via the Enumerable protocol
Mongo.aggregate(@topology, collection, pipeline, opts)
|> Enum.map(fn elem -> elem end)
For all options see [Options](https://docs.mongodb.com/manual/reference/command/aggregate/#aggregate)
Expand Down
2 changes: 2 additions & 0 deletions lib/mongo/session.ex
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ defmodule Mongo.Session do
For more information about causal consistency see the [officially documentation](https://docs.mongodb.com/manual/core/read-isolation-consistency-recency/#causal-consistency).
Note that Mongo.Stream implements the Enumerable protocol and the reduce/3 function calls Mongo.Stream.checkin_session/3 after the stream is exhausted.
If you want to use transaction, then you need to create a session as well:
alias Mongo.Session
Expand Down
5 changes: 4 additions & 1 deletion lib/mongo/stream.ex
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
defmodule Mongo.Stream do
@moduledoc false
@moduledoc """
This module provides a stream implementation that automatically checks out a session when the stream is started and an Enumerable
protocol that checks it back in when the stream has been consumed.
"""

alias Mongo.Session
alias Mongo.Error
Expand Down

0 comments on commit 6854323

Please sign in to comment.