@@ -16,6 +16,7 @@ defmodule Mongo.MongoDBConnection do
16
16
@ timeout 5_000
17
17
@ find_one_flags ~w( slave_ok exhaust partial) a
18
18
@ write_concern ~w( w j wtimeout) a
19
+ @ insecure_cmds [ :authenticate , :saslStart , :saslContinue , :getnonce , :createUser , :updateUser , :copydbgetnonce , :copydbsaslstart , :copydb , :isMaster , :ismaster ]
19
20
20
21
@ impl true
21
22
def connect ( opts ) do
@@ -40,17 +41,6 @@ defmodule Mongo.MongoDBConnection do
40
41
connect ( opts , state )
41
42
end
42
43
43
- @ impl true
44
- def disconnect ( _error , % { connection: { mod , socket } } = state ) do
45
- notify_disconnect ( state )
46
- mod . close ( socket )
47
- :ok
48
- end
49
-
50
- defp notify_disconnect ( % { connection_type: type , topology_pid: pid , host: host } ) do
51
- GenServer . cast ( pid , { :disconnect , type , host } )
52
- end
53
-
54
44
defp connect ( opts , state ) do
55
45
56
46
result =
@@ -127,24 +117,27 @@ defmodule Mongo.MongoDBConnection do
127
117
end
128
118
end
129
119
130
- defp wire_version ( state , client ) do
131
- # wire version
132
- # https://github.com/mongodb/mongo/blob/master/src/mongo/db/wire_version.h
133
-
120
+ defp post_hello_command ( state , client ) do
134
121
cmd = [ ismaster: 1 , client: client ]
135
122
136
123
case Utils . command ( - 1 , cmd , state ) do
137
- { :ok , _flags , % { "ok" => ok , "maxWireVersion" => version } } when ok == 1 -> { :ok , % { state | wire_version: version } }
138
- { :ok , _flags , % { "ok" => ok } } when ok == 1 -> { :ok , % { state | wire_version: 0 } }
124
+ { :ok , _flags , % { "ok" => ok , "maxWireVersion" => version } } when ok == 1 ->
125
+ { :ok , % { state | wire_version: version } }
126
+
127
+ { :ok , _flags , % { "ok" => ok } } when ok == 1 ->
128
+ { :ok , % { state | wire_version: 0 } }
129
+
139
130
{ :ok , _flags , % { "ok" => ok , "errmsg" => msg , "code" => code } } when ok == 0 ->
140
131
err = Mongo.Error . exception ( message: msg , code: code )
141
132
{ :disconnect , err , state }
142
- { :disconnect , _ , _ } = error -> error
133
+
134
+ { :disconnect , _ , _ } = error ->
135
+ error
143
136
end
144
137
end
145
138
146
139
defp hand_shake ( opts , state ) do
147
- wire_version ( state , driver ( opts [ :appname ] || "My killer app" ) )
140
+ post_hello_command ( state , driver ( opts [ :appname ] || "My killer app" ) )
148
141
end
149
142
150
143
defp driver ( appname ) do
@@ -194,6 +187,13 @@ defmodule Mongo.MongoDBConnection do
194
187
defp pretty_name ( "apple" ) , do: "Mac OS X"
195
188
defp pretty_name ( name ) , do: name
196
189
190
+ @ impl true
191
+ def disconnect ( _error , % { connection: { mod , socket } , connection_type: type , topology_pid: pid , host: host } ) do
192
+ GenServer . cast ( pid , { :disconnect , type , host } )
193
+ mod . close ( socket )
194
+ :ok
195
+ end
196
+
197
197
@ impl true
198
198
def checkout ( state ) , do: { :ok , state }
199
199
@ impl true
@@ -219,23 +219,23 @@ defmodule Mongo.MongoDBConnection do
219
219
def handle_status ( _opts , state ) , do: { :idle , state }
220
220
221
221
@ impl true
222
- def ping ( % { wire_version: _wire_version } = state ) do
223
- ###with {:ok, %{wire_version: ^wire_version}} <- wire_version(state), do: {:ok, state}
224
- ## todo Logger.info("Ignoring ping")
225
- { :ok , state }
226
- end
222
+ def ping ( % { connection_type: :client } = state ) do
223
+ cmd = [ ping: 1 ]
224
+ case Utils . command ( - 1 , cmd , state ) do
225
+ { :ok , _flags , % { "ok" => ok } } when ok == 1 ->
226
+ { :ok , state }
227
227
228
- @ doc """
229
- Execute a query prepared by `c:handle_prepare/3`. Return
230
- `{:ok, query, result, state}` to return altered query `query` and result
231
- `result` and continue, `{:error, exception, state}` to return an error and
232
- continue or `{:disconnect, exception, state}` to return an error and
233
- disconnect.
228
+ { :ok , _flags , % { "ok" => ok , "errmsg" => msg , "code" => code } } when ok == 0 ->
229
+ err = Mongo.Error . exception ( message: msg , code: code )
230
+ { :disconnect , err , state }
234
231
235
- This callback is called in the client process.
236
- """
237
- def handle_execute_close ( query , params , opts , s ) do
238
- handle_execute ( query , params , opts , s )
232
+ { :disconnect , _ , _ } = error ->
233
+ error
234
+ end
235
+ end
236
+ @ impl true
237
+ def ping ( state ) do
238
+ { :ok , state }
239
239
end
240
240
241
241
@ impl true
@@ -246,7 +246,7 @@ defmodule Mongo.MongoDBConnection do
246
246
end
247
247
end
248
248
249
- @ insecure_cmds [ :authenticate , :saslStart , :saslContinue , :getnonce , :createUser , :updateUser , :copydbgetnonce , :copydbsaslstart , :copydb , :isMaster , :ismaster ]
249
+
250
250
defp provide_cmd_data ( [ { command_name , _ } | _ ] = cmd ) do
251
251
case Enum . member? ( @ insecure_cmds , command_name ) do
252
252
true -> { command_name , % { } }
0 commit comments