Skip to content

Commit

Permalink
Add support for directConnection setting in connection string. (#265)
Browse files Browse the repository at this point in the history
  • Loading branch information
ColaCheng authored Feb 4, 2025
1 parent a50fcf7 commit 42a8e5f
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 5 deletions.
2 changes: 1 addition & 1 deletion lib/mongo/topology.ex
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ defmodule Mongo.Topology do
# see https://github.com/mongodb/specifications/blob/master/source/server-discovery-and-monitoring/server-discovery-and-monitoring.rst#configuration
def init(opts) do
seeds = Keyword.get(opts, :seeds, [seed(opts)])
type = Keyword.get(opts, :type, :unknown)
type = TopologyDescription.get_type(opts)
set_name = Keyword.get(opts, :set_name, nil)
local_threshold_ms = Keyword.get(opts, :local_threshold_ms, 15)

Expand Down
7 changes: 7 additions & 0 deletions lib/mongo/topology_description.ex
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,13 @@ defmodule Mongo.TopologyDescription do
end
end

def get_type(opts) do
case Keyword.get(opts, :direct_connection, false) do
true -> :single
false -> Keyword.get(opts, :type, :unknown)
end
end

defp mongos_servers(%{:servers => servers}) do
Enum.filter(servers, fn {_, server} -> server.type == :mongos end)
end
Expand Down
1 change: 1 addition & 0 deletions lib/mongo/url_parser.ex
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ defmodule Mongo.UrlParser do
"database" => :string,
# Query options
"replicaSet" => :string,
"directConnection" => ["true", "false"],
"ssl" => ["true", "false"],
"connectTimeoutMS" => :number,
"socketTimeoutMS" => :number,
Expand Down
13 changes: 9 additions & 4 deletions test/mongo/topology_description_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -123,13 +123,18 @@ defmodule Mongo.TopologyDescriptionTest do
assert Enum.any?(all_hosts, fn sec -> sec == server end)
end

test "Simplified server selection" do
single_server = "localhost:27017"
test "Set topology type to :single when direct_connection option is true" do
opts = [
direct_connection: true
]

assert :single = TopologyDescription.get_type(opts)

opts = [
read_preference: %{mode: :secondary}
type: :unknown,
direct_connection: true
]

assert {:ok, {^single_server, _}} = TopologyDescription.select_servers(single(), :read, opts)
assert :single = TopologyDescription.get_type(opts)
end
end
12 changes: 12 additions & 0 deletions test/mongo/url_parser_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -228,5 +228,17 @@ defmodule Mongo.UrlParserTest do
]
]
end

test "url with directConnection" do
for direct_connection <- ["true", "false"] do
assert UrlParser.parse_url(url: "mongodb://seed1.domain.com:27017/db_name?directConnection=#{direct_connection}") == [
database: "db_name",
direct_connection: String.to_atom(direct_connection),
seeds: [
"seed1.domain.com:27017"
]
]
end
end
end
end

0 comments on commit 42a8e5f

Please sign in to comment.