-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathclient.ex
More file actions
388 lines (313 loc) · 12.1 KB
/
client.ex
File metadata and controls
388 lines (313 loc) · 12.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
defmodule Thrift.Binary.Framed.Client do
@moduledoc """
A client implementation of Thrift's Binary Framed protocol.
This client is meant to be used with a generated Thrift client. This module
implements framing on top of the `Connection` behaviour.
This module adds `oneway/4` for making "oneway" RPC calls that won't receive
a response.
See `start_link/3` for the various connection options.
{:ok, client} = Client.start_link(
"localhost", 2345,
tcp_opts: [],
ssl_opts: [enabled: true, cacertfile: "cacerts.pem", certfile: "cert.pem", keyfile: "key.pem"],
gen_server_opts: [timeout: 10_000])
"""
alias Thrift.Protocol.Binary
alias Thrift.TApplicationException
alias Thrift.Transport.SSL
@immutable_tcp_opts [active: false, packet: 4, mode: :binary]
@type error :: {:error, atom} | {:error, {:exception, struct}}
@type success :: {:ok, binary}
@type protocol_response :: success | error
@type tcp_option ::
{:timeout, pos_integer}
| {:send_timeout, integer}
@type tcp_opts :: [tcp_option]
@type genserver_call_option :: {:timeout, timeout}
@type genserver_call_options :: [genserver_call_option]
@type option ::
{:tcp_opts, [tcp_option]}
| {:ssl_opts, [SSL.option()]}
| {:gen_server_opts, [genserver_call_option]}
@type options :: [option]
defmodule State do
@moduledoc false
@type t :: %State{
host: String.t(),
port: 1..65_535,
tcp_opts: [Thrift.Binary.Framed.Client.tcp_option()],
ssl_opts: [SSL.option()],
timeout: integer,
sock: {:gen_tcp, :gen_tcp.socket()} | {:ssl, :ssl.sslsocket()},
seq_id: integer
}
defstruct host: nil,
port: nil,
tcp_opts: nil,
ssl_enabled: false,
ssl_opts: nil,
timeout: 5000,
sock: nil,
seq_id: 0
end
require Logger
use Connection
@impl Connection
def init({host, port, opts}) do
tcp_opts = Keyword.get(opts, :tcp_opts, [])
ssl_opts = Keyword.get(opts, :ssl_opts, [])
{timeout, tcp_opts} = Keyword.pop(tcp_opts, :timeout, 5000)
s = %State{
host: to_host(host),
port: port,
tcp_opts: tcp_opts,
ssl_opts: ssl_opts,
timeout: timeout
}
{:connect, :init, s}
end
@doc """
Starts and connects the client.
When called, the client connects to the specified host and establishes a
TCP connection. The following options can be given:
`tcp_opts`: A keyword list that controls how the underlying connection is
handled. All options not handled below are sent to the underlying gen_tcp
(with the exception of the following options, which, if overridden, would
break the framed client: [`active`, `packet`, `mode`]).
- `:timeout`: A positive integer (milliseconds) that governs how long the
gen_tcp connection waits for operations to complete. This timeout is
used when connecting or receiving data.
- `:send_timeout`: A positive integer (milliseconds) that governs how long
our connection waits when sending data.
`ssl_opts`: A keyword list of SSL/TLS options:
- `:enabled`: A boolean indicating whether to upgrade the connection to
the SSL protocol
- `:optional`: A boolean indicating whether to accept both SSL and plain
connections
- `:configure`: A 0-arity function to provide additional SSL options at
runtime
- Additional `:ssl.ssl_option/0` values specifying other `:ssl` options
`gen_server_opts`: A keyword list of options for the GenServer:
- `:timeout`: A positive integer (milliseconds) specifying the amount of
time the client's GenServer should wait for a reply. After this time,
the GenServer will exit with `{:error, :timeout}`.
Additionally, the options `:name`, `:debug`, and `:spawn_opt`, if specified,
will be passed to the underlying `GenServer`. See `GenServer.start_link/3`
for details on these options.
"""
@spec start_link(String.t(), 0..65_535, options) :: GenServer.on_start()
def start_link(host, port, opts) do
{gen_server_opts, client_opts} = Keyword.split(opts, [:debug, :name, :spawn_opt])
Connection.start_link(__MODULE__, {host, port, client_opts}, gen_server_opts)
end
@doc """
Closes the client's underlying connection.
"""
def close(conn), do: Connection.call(conn, :close)
@impl Connection
def connect(_info, %{sock: nil, host: host, port: port, tcp_opts: opts, timeout: timeout} = s) do
opts =
opts
|> Keyword.merge(@immutable_tcp_opts)
|> Keyword.put_new(:send_timeout, 1000)
case :gen_tcp.connect(host, port, opts, timeout) do
{:ok, sock} ->
maybe_ssl_handshake(sock, host, port, s)
{:error, :timeout} = error ->
Logger.error("Failed to connect to #{host}:#{port} due to timeout after #{timeout}ms")
{:stop, error, s}
{:error, reason} = error ->
Logger.error("Failed to connect to #{host}:#{port} due to #{:inet.format_error(reason)}")
{:stop, error, s}
end
end
@impl Connection
def disconnect(info, %{sock: {transport, sock}}) do
:ok = transport.close(sock)
case info do
{:close, from} ->
Connection.reply(from, :ok)
{:stop, :normal, nil}
{:error, :closed} = error ->
Logger.error("Connection closed")
{:stop, error, nil}
{:error, :timeout, timeout} ->
Logger.error("Connection timed out after #{timeout}ms")
{:stop, {:error, :timeout}, nil}
{:error, reason} = error ->
# :ssl formats ssl and posix errors
reason = :ssl.format_error(reason)
Logger.error("Connection error: #{reason}")
{:stop, error, nil}
end
end
@spec oneway(pid, String.t(), struct, options) :: :ok
@doc """
Execute a one way RPC. One way RPC calls do not generate a response,
and as such, this implementation uses `GenServer.cast`.
The argument must be a Thrift struct.
"""
def oneway(conn, rpc_name, args, _opts) do
serialized_args = Thrift.Serializable.serialize(args, %Binary{payload: ""})
:ok = Connection.cast(conn, {:oneway, rpc_name, serialized_args})
end
@spec call(pid, String.t(), struct, struct, options) :: protocol_response
@doc """
Executes a Thrift RPC. The argument and response arguments must be Thrift structs.
The `opts` argument takes the same type of keyword list that `start_link` takes.
"""
def call(conn, rpc_name, args, resp, opts) do
tcp_opts = Keyword.get(opts, :tcp_opts, [])
gen_server_opts = Keyword.get(opts, :gen_server_opts, [])
gen_server_timeout = Keyword.get(gen_server_opts, :timeout, 5000)
serialized_args = Thrift.Serializable.serialize(args, %Binary{payload: ""})
case Connection.call(conn, {:call, rpc_name, serialized_args, tcp_opts}, gen_server_timeout) do
{:ok, data} ->
resp
|> Thrift.Serializable.deserialize(%Binary{payload: data})
|> unpack_response
{:error, _} = err ->
err
end
end
# Unpack a "complex" response that contains a `nil` success value and one or
# more exception fields. We can't differentiate between a "void" result and
# a potential exception without checking each exception field to see if it
# contains a non-`nil` value.
#
# As a small optimization, we only use this function if the response struct
# has at least one exception field (hence the `map_size/1` guard check).
defp unpack_response({%{success: nil} = response, %Binary{payload: ""}}) when map_size(response) > 2 do
exception =
response
|> Map.from_struct()
|> Enum.find_value(fn {_, value} -> value end)
if exception do
{:error, {:exception, exception}}
else
# "void" function result
{:ok, nil}
end
end
defp unpack_response({%{success: result}, %Binary{payload: ""}}), do: {:ok, result}
defp unpack_response({:error, _} = error), do: error
def handle_call(_, _, %{sock: nil} = s) do
{:reply, {:error, :closed}, s}
end
def handle_call(
{:call, rpc_name, %Binary{payload: serialized_args}, tcp_opts},
_,
%{sock: {transport, sock}, seq_id: seq_id, timeout: default_timeout} = s
) do
s = %{s | seq_id: seq_id + 1}
message = Binary.serialize(:message_begin, {:call, seq_id, rpc_name})
timeout = Keyword.get(tcp_opts, :timeout, default_timeout)
with :ok <- transport.send(sock, [message | serialized_args]),
{:ok, message} <- transport.recv(sock, 0, timeout) do
reply = deserialize_message_reply(message, rpc_name, seq_id)
{:reply, reply, s}
else
{:error, :timeout} = error ->
{:disconnect, {:error, :timeout, timeout}, error, s}
{:error, _} = error ->
{:disconnect, error, error, s}
end
end
@impl Connection
def handle_call(:close, from, s) do
{:disconnect, {:close, from}, s}
end
@impl Connection
def handle_cast(_, %{sock: nil} = s) do
{:noreply, s}
end
def handle_cast(
{:oneway, rpc_name, %Binary{payload: serialized_args}},
%{sock: {transport, sock}, seq_id: seq_id} = s
) do
s = %{s | seq_id: seq_id + 1}
message = Binary.serialize(:message_begin, {:oneway, seq_id, rpc_name})
case transport.send(sock, [message | serialized_args]) do
:ok ->
{:noreply, s}
{:error, _} = error ->
{:disconnect, error, s}
end
end
def deserialize_message_reply(message, rpc_name, seq_id) do
handle_message(Binary.deserialize(:message_begin, message), seq_id, rpc_name)
end
defp handle_message({:ok, {:reply, seq_id, rpc_name, serialized_response}}, seq_id, rpc_name) do
{:ok, serialized_response}
end
defp handle_message(
{:ok, {:exception, seq_id, rpc_name, serialized_response}},
seq_id,
rpc_name
) do
{:error, {:exception, deserialize_exception(serialized_response)}}
end
defp handle_message({:ok, {message_type, seq_id, rpc_name, _}}, seq_id, rpc_name) do
exception =
TApplicationException.exception(
type: :invalid_message_type,
message: "The server replied with invalid message type (#{message_type})"
)
{:error, {:exception, exception}}
end
defp handle_message({:ok, {_, seq_id, mismatched_rpc_name, _}}, seq_id, rpc_name) do
exception =
TApplicationException.exception(
type: :wrong_method_name,
message: "The server replied to #{mismatched_rpc_name}, but we sent #{rpc_name}"
)
{:error, {:exception, exception}}
end
defp handle_message({:ok, {_, mismatched_seq_id, _, _}}, seq_id, _) do
exception =
TApplicationException.exception(
type: :bad_sequence_id,
message:
"Invalid sequence id. The client sent #{seq_id}, but the server replied with #{
mismatched_seq_id
}"
)
{:error, {:exception, exception}}
end
defp handle_message({:error, _} = err, _, _) do
err
end
defp deserialize_exception(payload) do
case TApplicationException.SerDe.deserialize(%Binary{payload: payload}) do
{exception, %Binary{payload: ""}} ->
exception
_ ->
TApplicationException.exception(
type: :protocol_error,
message: "Unable to decode exception (#{inspect payload})"
)
end
end
defp to_host(host) when is_bitstring(host) do
String.to_charlist(host)
end
defp to_host(host) when is_list(host), do: host
defp maybe_ssl_handshake(sock, host, port, %{ssl_opts: ssl_opts, timeout: timeout} = s) do
with {optional, ssl_opts} when optional in [:required, :optional] <-
SSL.configuration(ssl_opts),
{:ok, ssl_sock} <- :ssl.connect(sock, ssl_opts, timeout) do
{:ok, %{s | sock: {:ssl, ssl_sock}}}
else
nil ->
{:ok, %{s | sock: {:gen_tcp, sock}}}
{:error, %_exception{} = err} ->
Logger.error("Failed SSL configuration due to: " <> Exception.format(:error, err, []))
{:stop, err, s}
{:error, reason} = error ->
Logger.error(
"Failed SSL handshake to #{host}:#{port} due to #{:ssl.format_error(reason)}"
)
{:stop, error, s}
end
end
end