Skip to content

Commit 42a8e5f

Browse files
authored
Add support for directConnection setting in connection string. (#265)
1 parent a50fcf7 commit 42a8e5f

File tree

5 files changed

+30
-5
lines changed

5 files changed

+30
-5
lines changed

lib/mongo/topology.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ defmodule Mongo.Topology do
116116
# see https://github.com/mongodb/specifications/blob/master/source/server-discovery-and-monitoring/server-discovery-and-monitoring.rst#configuration
117117
def init(opts) do
118118
seeds = Keyword.get(opts, :seeds, [seed(opts)])
119-
type = Keyword.get(opts, :type, :unknown)
119+
type = TopologyDescription.get_type(opts)
120120
set_name = Keyword.get(opts, :set_name, nil)
121121
local_threshold_ms = Keyword.get(opts, :local_threshold_ms, 15)
122122

lib/mongo/topology_description.ex

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,13 @@ defmodule Mongo.TopologyDescription do
175175
end
176176
end
177177

178+
def get_type(opts) do
179+
case Keyword.get(opts, :direct_connection, false) do
180+
true -> :single
181+
false -> Keyword.get(opts, :type, :unknown)
182+
end
183+
end
184+
178185
defp mongos_servers(%{:servers => servers}) do
179186
Enum.filter(servers, fn {_, server} -> server.type == :mongos end)
180187
end

lib/mongo/url_parser.ex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ defmodule Mongo.UrlParser do
2424
"database" => :string,
2525
# Query options
2626
"replicaSet" => :string,
27+
"directConnection" => ["true", "false"],
2728
"ssl" => ["true", "false"],
2829
"connectTimeoutMS" => :number,
2930
"socketTimeoutMS" => :number,

test/mongo/topology_description_test.exs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,13 +123,18 @@ defmodule Mongo.TopologyDescriptionTest do
123123
assert Enum.any?(all_hosts, fn sec -> sec == server end)
124124
end
125125

126-
test "Simplified server selection" do
127-
single_server = "localhost:27017"
126+
test "Set topology type to :single when direct_connection option is true" do
127+
opts = [
128+
direct_connection: true
129+
]
130+
131+
assert :single = TopologyDescription.get_type(opts)
128132

129133
opts = [
130-
read_preference: %{mode: :secondary}
134+
type: :unknown,
135+
direct_connection: true
131136
]
132137

133-
assert {:ok, {^single_server, _}} = TopologyDescription.select_servers(single(), :read, opts)
138+
assert :single = TopologyDescription.get_type(opts)
134139
end
135140
end

test/mongo/url_parser_test.exs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,5 +228,17 @@ defmodule Mongo.UrlParserTest do
228228
]
229229
]
230230
end
231+
232+
test "url with directConnection" do
233+
for direct_connection <- ["true", "false"] do
234+
assert UrlParser.parse_url(url: "mongodb://seed1.domain.com:27017/db_name?directConnection=#{direct_connection}") == [
235+
database: "db_name",
236+
direct_connection: String.to_atom(direct_connection),
237+
seeds: [
238+
"seed1.domain.com:27017"
239+
]
240+
]
241+
end
242+
end
231243
end
232244
end

0 commit comments

Comments
 (0)