Skip to content

Commit aa67841

Browse files
committed
fixes #10
1 parent bbb3fe6 commit aa67841

File tree

7 files changed

+67
-15
lines changed

7 files changed

+67
-15
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
* Enhancements
44
* The driver provides now client metadata
5+
* Added support for connecting via UNIX sockets (`:socket` and `:socket_dir`)
56

67
## v0.5.3
78

lib/mongo.ex

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,15 @@ defmodule Mongo do
8383
* `:database` - The database to use (required)
8484
* `:hostname` - The host to connect to (require)
8585
* `:port` - The port to connect to your server (default: 27017)
86-
* `:url` - A mongo connection url. Can be used in place of `:hostname` and
87-
`:database` (optional)
86+
* `:url` - A mongo connection url. Can be used in place of `:hostname` and `:database` (optional)
87+
* `:socket_dir` - Connect to MongoDB via UNIX sockets in the given directory.
88+
The socket name is derived based on the port. This is the preferred method
89+
for configuring sockets and it takes precedence over the hostname. If you
90+
are connecting to a socket outside of the MongoDB convection, use
91+
`:socket` instead.
92+
* `:socket` - Connect to MongoDB via UNIX sockets in the given path.
93+
This option takes precedence over `:hostname` and `:socket_dir`.
94+
* `:database` (optional)
8895
* `:seeds` - A list of host names in the cluster. Can be used in place of
8996
`:hostname` (optional)
9097
* `:username` - The User to connect with (optional)

lib/mongo/mongo_db_connection.ex

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -95,15 +95,17 @@ defmodule Mongo.MongoDBConnection do
9595
end
9696
end
9797

98-
defp tcp_connect(opts, s) do
99-
host = (opts[:hostname] || "localhost") |> to_charlist
100-
port = opts[:port] || 27017
101-
sock_opts = [:binary, active: false, packet: :raw, nodelay: true]
102-
++ (opts[:socket_options] || [])
98+
defp tcp_connect(opts, state) do
10399

104-
s = Map.put(s, :host, "#{host}:#{port}")
100+
{host, port} = Utils.hostname_port(opts)
101+
sock_opts = [:binary, active: false, packet: :raw, nodelay: true] ++ (opts[:socket_options] || [])
105102

106-
case :gen_tcp.connect(host, port, sock_opts, s.connect_timeout) do
103+
s = case host do
104+
{:local, socket} -> Map.put(state, :host, socket)
105+
hostname -> Map.put(state, :host, "#{hostname}:#{port}")
106+
end
107+
108+
case :gen_tcp.connect(host, port, sock_opts, state.connect_timeout) do
107109
{:ok, socket} ->
108110
# A suitable :buffer is only set if :recbuf is included in
109111
# :socket_options.
@@ -113,7 +115,7 @@ defmodule Mongo.MongoDBConnection do
113115

114116
{:ok, %{s | connection: {:gen_tcp, socket}}}
115117

116-
{:error, reason} -> {:error, Mongo.Error.exception(tag: :tcp, action: "connect", reason: reason, host: s.host)}
118+
{:error, reason} -> {:error, Mongo.Error.exception(tag: :tcp, action: "connect", reason: reason, host: state.host)}
117119
end
118120
end
119121

lib/mongo/topology.ex

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,10 @@ defmodule Mongo.Topology do
6262
# see https://github.com/mongodb/specifications/blob/master/source/server-discovery-and-monitoring/server-discovery-and-monitoring.rst#configuration
6363
@doc false
6464
def init(opts) do
65-
seeds = Keyword.get(opts, :seeds, [
66-
Keyword.get(opts, :hostname, "localhost") <> ":" <> to_string(Keyword.get(opts, :port, 27017))
67-
])
65+
#seeds = Keyword.get(opts, :seeds, [
66+
# Keyword.get(opts, :hostname, "localhost") <> ":" <> to_string(Keyword.get(opts, :port, 27017))
67+
#])
68+
seeds = Keyword.get(opts, :seeds, [seed(opts)])
6869
type = Keyword.get(opts, :type, :unknown)
6970
set_name = Keyword.get(opts, :set_name, nil)
7071
local_threshold_ms = Keyword.get(opts, :local_threshold_ms, 15)
@@ -99,6 +100,13 @@ defmodule Mongo.Topology do
99100
end
100101
end
101102

103+
defp seed(opts) do
104+
case Mongo.MongoDBConnection.Utils.hostname_port(opts) do
105+
{{:local, socket}, 0} -> socket
106+
{hostname, port} -> "#{hostname}:#{port}"
107+
end
108+
end
109+
102110
def terminate(_reason, state) do
103111
Enum.each(state.connection_pools, fn {_address, pid} -> GenServer.stop(pid) end)
104112
Enum.each(state.monitors, fn {_address, pid} -> GenServer.stop(pid) end)

lib/mongo_db_connection/utils.ex

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,4 +136,16 @@ defmodule Mongo.MongoDBConnection.Utils do
136136
def digest_password(_username, password, :sha256) do
137137
password
138138
end
139+
140+
def hostname_port(opts) do
141+
port = opts[:port] || 27017
142+
case Keyword.fetch(opts, :socket) do
143+
{:ok, socket} -> {{:local, socket}, 0}
144+
:error ->
145+
case Keyword.fetch(opts, :socket_dir) do
146+
{:ok, dir} -> {{:local, "#{dir}/mongodb-#{port}.sock"}, 0}
147+
:error -> {(opts[:hostname] || "localhost") |> to_charlist, port}
148+
end
149+
end
150+
end
139151
end

test/mongo/connection_test.exs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,31 @@ defmodule Mongo.ConnectionTest do
4747
end)
4848
end
4949

50+
defp connect_socket_dir do
51+
assert {:ok, pid} = Mongo.start_link(socket_dir: "/tmp", database: "mongodb_test")
52+
pid
53+
end
54+
55+
defp connect_socket do
56+
assert {:ok, pid} = Mongo.start_link(socket: "/tmp/mongodb-27017.sock", database: "mongodb_test")
57+
pid
58+
end
59+
60+
@tag :socket
61+
test "connect socket_dir" do
62+
pid = connect_socket_dir()
63+
assert {:ok, %{"ok" => 1.0}} = Mongo.ping(pid)
64+
end
65+
66+
@tag :socket
67+
test "connect socket" do
68+
pid = connect_socket()
69+
assert {:ok, %{"ok" => 1.0}} = Mongo.ping(pid)
70+
end
71+
5072
test "connect and ping" do
5173
pid = connect()
52-
assert {:ok, %{"ok" => 1.0}} = Mongo.ping(pid)
74+
assert {:ok, %{"ok" => 1.0}} = Mongo.ping(pid)
5375
end
5476

5577
@tag :ssl

test/test_helper.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ version =
1111
|> List.to_tuple
1212

1313
options = []
14-
options = if System.get_env("CI") do [ssl: true] ++ options else options end
14+
options = if System.get_env("CI") do [ssl: true, socket: true] ++ options else options end
1515
options = if version < {3, 4, 0} do [mongo_3_4: true] ++ options else options end
1616
options = if version < {3, 6, 0} do [change_streams: true] ++ options else options end
1717

0 commit comments

Comments
 (0)