]> git.proxmox.com Git - ceph.git/blob - ceph/src/jaegertracing/thrift/lib/erl/src/thrift_client_util.erl
update source to Ceph Pacific 16.2.2
[ceph.git] / ceph / src / jaegertracing / thrift / lib / erl / src / thrift_client_util.erl
1 %%
2 %% Licensed to the Apache Software Foundation (ASF) under one
3 %% or more contributor license agreements. See the NOTICE file
4 %% distributed with this work for additional information
5 %% regarding copyright ownership. The ASF licenses this file
6 %% to you under the Apache License, Version 2.0 (the
7 %% "License"); you may not use this file except in compliance
8 %% with the License. You may obtain a copy of the License at
9 %%
10 %% http://www.apache.org/licenses/LICENSE-2.0
11 %%
12 %% Unless required by applicable law or agreed to in writing,
13 %% software distributed under the License is distributed on an
14 %% "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 %% KIND, either express or implied. See the License for the
16 %% specific language governing permissions and limitations
17 %% under the License.
18 %%
19
20 -module(thrift_client_util).
21
22 -export([new/4]).
23 -export([new_multiplexed/3, new_multiplexed/4]).
24
25 -type service_name() :: nonempty_string().
26 -type service_module() :: atom().
27 -type multiplexed_service_map() :: [{ServiceName::service_name(), ServiceModule::service_module()}].
28
29 %%
30 %% Splits client options into client, protocol, and transport options
31 %%
32 %% split_options([Options...]) -> {ProtocolOptions, TransportOptions}
33 %%
34 split_options(Options) ->
35 split_options(Options, [], []).
36
37 split_options([], ProtoIn, TransIn) ->
38 {ProtoIn, TransIn};
39
40 split_options([Opt = {OptKey, _} | Rest], ProtoIn, TransIn)
41 when OptKey =:= strict_read;
42 OptKey =:= strict_write;
43 OptKey =:= protocol ->
44 split_options(Rest, [Opt | ProtoIn], TransIn);
45
46 split_options([Opt = {OptKey, _} | Rest], ProtoIn, TransIn)
47 when OptKey =:= framed;
48 OptKey =:= connect_timeout;
49 OptKey =:= recv_timeout;
50 OptKey =:= sockopts;
51 OptKey =:= ssltransport;
52 OptKey =:= ssloptions->
53 split_options(Rest, ProtoIn, [Opt | TransIn]).
54
55
56 %% Client constructor for the common-case of socket transports
57 new(Host, Port, Service, Options)
58 when is_integer(Port), is_atom(Service), is_list(Options) ->
59 {ProtoOpts, TransOpts0} = split_options(Options),
60
61 {TransportModule, TransOpts2} = case lists:keytake(ssltransport, 1, TransOpts0) of
62 {value, {_, true}, TransOpts1} -> {thrift_sslsocket_transport, TransOpts1};
63 false -> {thrift_socket_transport, TransOpts0}
64 end,
65
66 {ProtocolModule, ProtoOpts1} = case lists:keytake(protocol, 1, ProtoOpts) of
67 {value, {_, compact}, Opts} -> {thrift_compact_protocol, Opts};
68 {value, {_, json}, Opts} -> {thrift_json_protocol, Opts};
69 {value, {_, binary}, Opts} -> {thrift_binary_protocol, Opts};
70 false -> {thrift_binary_protocol, ProtoOpts}
71 end,
72 {ok, TransportFactory} =
73 TransportModule:new_transport_factory(Host, Port, TransOpts2),
74
75 {ok, ProtocolFactory} = ProtocolModule:new_protocol_factory(
76 TransportFactory, ProtoOpts1),
77
78 case ProtocolFactory() of
79 {ok, Protocol} ->
80 thrift_client:new(Protocol, Service);
81 {error, Error} ->
82 {error, Error}
83 end.
84
85 -spec new_multiplexed(Host, Port, Services, Options) -> {ok, ServiceThriftClientList} when
86 Host :: nonempty_string(),
87 Port :: non_neg_integer(),
88 Services :: multiplexed_service_map(),
89 Options :: list(),
90 ServiceThriftClientList :: [{ServiceName::list(), ThriftClient::term()}].
91 new_multiplexed(Host, Port, Services, Options) when is_integer(Port),
92 is_list(Services),
93 is_list(Options) ->
94 new_multiplexed(thrift_socket_transport:new_transport_factory(Host, Port, Options), Services, Options).
95
96 -spec new_multiplexed(TransportFactoryTuple, Services, Options) -> {ok, ServiceThriftClientList} when
97 TransportFactoryTuple :: {ok, TransportFactory::term()},
98 Services :: multiplexed_service_map(),
99 Options :: list(),
100 ServiceThriftClientList :: [{ServiceName::service_name(), ThriftClient::term()}].
101 new_multiplexed(TransportFactoryTuple, Services, Options) when is_list(Services),
102 is_list(Options),
103 is_tuple(TransportFactoryTuple) ->
104 {ProtoOpts, _} = split_options(Options),
105
106 {ok, TransportFactory} = TransportFactoryTuple,
107
108 {ok, ProtocolFactory} = thrift_binary_protocol:new_protocol_factory(TransportFactory, ProtoOpts),
109
110 {ok, Protocol} = ProtocolFactory(),
111
112 {ok, [{ServiceName, element(2, thrift_client:new(element(2, thrift_multiplexed_protocol:new(Protocol, ServiceName)), Service))} || {ServiceName, Service} <- Services]}.