Skip to content

Commit fa107f8

Browse files
authored
Rti 16773 use ehttpc instead of lhttpc (#93)
1 parent 7da245b commit fa107f8

File tree

12 files changed

+117
-111
lines changed

12 files changed

+117
-111
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ to override the context values with configured ones.
1111

1212
You can start an erl with:
1313

14-
$ erl -pa ebin -pa deps/*/ebin -s inets -s crypto -s ssl -s lhttpc -config development -s kinetic
14+
$ erl -pa ebin -pa deps/*/ebin -s inets -s crypto -s ssl -s ehttpc -config development -s kinetic
1515
Erlang R16B03-1 (erts-5.10.4) [source] [64-bit] [smp:4:4] [async-threads:10] [hipe] [kernel-poll:false] [dtrace]
1616

1717
Eshell V5.10.4 (abort with ^G)

include/kinetic.hrl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,5 @@
77
date :: undefined | string(),
88
host :: undefined | string(),
99
url :: undefined | string(),
10-
lhttpc_opts = [] :: [any()],
1110
timeout :: undefined | pos_integer(),
1211
aws_credentials}).

rebar.config

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@
1616
{cover_print_enabled, true}.
1717

1818
{deps,
19-
[{lhttpc, "1.4.0", {pkg, nextroll_lhttpc}},
20-
{jiffy, "1.1.1"},
19+
[{jiffy, "1.1.1"},
2120
{erliam, "1.0.1"},
22-
{b64fast, "0.2.3"}]}.
21+
{b64fast, "0.2.3"},
22+
{ehttpc, {git, "https://github.com/emqx/ehttpc", {tag, "0.7.1"}}}]}.
2323

2424
{xref_checks,
2525
[undefined_function_calls,
@@ -39,7 +39,7 @@
3939

4040
{spellcheck,
4141
[{ignore_regex,
42-
"(eunit|~>|<-|//|=|[|]|[.]hrl|\\d[.]\\d|<<[\"]|[a-z][a-z][-][a-z]|[?][A-Z])"},
42+
"(eunit|~>|<-|//|=|[|]|[.]hrl|\\d[.]\\d|<<[\"]|[a-z][a-z][-][a-z]|[?][A-Z]|\/)"},
4343
{files, ["src/*"]},
4444
{additional_dictionaries, ["nextroll.dict", "kinesis.dict"]}]}.
4545

rebar.lock

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,32 @@
11
{"1.2.0",
22
[{<<"b64fast">>,{pkg,<<"b64fast">>,<<"0.2.3">>},0},
3+
{<<"cowlib">>,{pkg,<<"cowlib">>,<<"2.13.0">>},2},
4+
{<<"ehttpc">>,
5+
{git,"https://github.com/emqx/ehttpc",
6+
{ref,"c18e5efacde5556bb8bf86b1bc1e35a85a67928d"}},
7+
0},
38
{<<"erliam">>,{pkg,<<"erliam">>,<<"1.0.1">>},0},
9+
{<<"gproc">>,
10+
{git,"https://github.com/emqx/gproc",
11+
{ref,"21a5995812498969bb5e47b520b47ea7c514f16b"}},
12+
1},
13+
{<<"gun">>,{pkg,<<"gun">>,<<"2.1.0">>},1},
414
{<<"jiffy">>,{pkg,<<"jiffy">>,<<"1.1.1">>},0},
5-
{<<"lhttpc">>,{pkg,<<"nextroll_lhttpc">>,<<"1.4.0">>},0}]}.
15+
{<<"snabbkaffe">>,
16+
{git,"https://github.com/kafka4beam/snabbkaffe.git",
17+
{ref,"b59298334ed349556f63405d1353184c63c66534"}},
18+
1}]}.
619
[
720
{pkg_hash,[
821
{<<"b64fast">>, <<"07649CF971A0ED088DEFC4F75767A52E08C468CC1D448693F4FB3051092AB987">>},
22+
{<<"cowlib">>, <<"DB8F7505D8332D98EF50A3EF34B34C1AFDDEC7506E4EE4DD4A3A266285D282CA">>},
923
{<<"erliam">>, <<"20E1ECB876AFDEEC2DE07483E2D174B1E3DB38848ED981145DAB9A889E7B55F9">>},
10-
{<<"jiffy">>, <<"ACA10F47AA91697BF24AB9582C74E00E8E95474C7EF9F76D4F1A338D0F5DE21B">>},
11-
{<<"lhttpc">>, <<"45282FF22BC55E6AE751CF87AC42C261DC4FAAFADD9C034E127ECED74E672FAB">>}]},
24+
{<<"gun">>, <<"B4E4CBBF3026D21981C447E9E7CA856766046EFF693720BA43114D7F5DE36E87">>},
25+
{<<"jiffy">>, <<"ACA10F47AA91697BF24AB9582C74E00E8E95474C7EF9F76D4F1A338D0F5DE21B">>}]},
1226
{pkg_hash_ext,[
1327
{<<"b64fast">>, <<"DE874B5302D607840B787E93785628035FF55334EBCFAFB6413B735983913D23">>},
28+
{<<"cowlib">>, <<"E1E1284DC3FC030A64B1AD0D8382AE7E99DA46C3246B815318A4B848873800A4">>},
1429
{<<"erliam">>, <<"2EE375544AC36711BEEB5EC56DB060488447CECC308763BC8B4A4FEE894AAF76">>},
15-
{<<"jiffy">>, <<"62E1F0581C3C19C33A725C781DFA88410D8BFF1BBAFC3885A2552286B4785C4C">>},
16-
{<<"lhttpc">>, <<"57BA3D5720FBD17C75D8563169394B5F6CD160161D64A8A9F96F7E829221C648">>}]}
30+
{<<"gun">>, <<"52FC7FC246BFC3B00E01AEA1C2854C70A366348574AB50C57DFE796D24A0101D">>},
31+
{<<"jiffy">>, <<"62E1F0581C3C19C33A725C781DFA88410D8BFF1BBAFC3885A2552286B4785C4C">>}]}
1732
].

src/kinetic.app.src

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
{vsn, git},
55
{modules, []},
66
{registered, [kinetic_config]},
7-
{applications, [kernel, stdlib, inets, crypto, ssl, jiffy, lhttpc, erliam]},
7+
{applications, [kernel, stdlib, inets, crypto, ssl, jiffy, ehttpc, erliam]},
88
{env, [{args, []}]},
99
{mod, {kinetic, []}},
1010
{licenses, ["BSD-3-Clause"]}]}.

src/kinetic.erl

Lines changed: 40 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,21 @@ stop() ->
3232
application:stop(kinetic).
3333

3434
start(Opts) when is_list(Opts) ->
35+
start_pool(),
3536
kinetic_sup:start_link(Opts).
3637

3738
-spec start(normal | {takeover, node()} | {failover, node()}, any()) -> {ok, pid()}.
3839
start(_, Opts) ->
40+
start_pool(),
3941
kinetic_sup:start_link(Opts).
4042

4143
-spec stop(any()) -> ok.
4244
stop(_) ->
45+
lists:foreach(fun(Region) ->
46+
PoolName = kinetic_utils:pool_name(Region),
47+
ok = ehttpc_sup:stop_pool(PoolName)
48+
end,
49+
kinetic_utils:regions()),
4350
ok.
4451

4552
% Public API
@@ -252,9 +259,7 @@ execute(Operation, Payload, Opts) ->
252259
#kinetic_arguments{aws_credentials = AwsCreds,
253260
region = Region,
254261
date = Date,
255-
url = Url,
256262
host = Host,
257-
lhttpc_opts = LHttpcOpts,
258263
timeout = Timeout} =
259264
kinetic_config:merge_args(Args, Opts),
260265
case kinetic_utils:encode({Payload}) of
@@ -267,23 +272,26 @@ execute(Operation, Payload, Opts) ->
267272
#{"content-type" => "application/x-amz-json-1.1",
268273
"connection" => "keep-alive"},
269274
Headers =
270-
awsv4:headers(AwsCreds,
271-
#{service => "kinesis",
272-
target_api => Target,
273-
method => "POST",
274-
region => Region,
275-
host => Host,
276-
signed_headers => SignedHeaders,
277-
aws_date => Date},
278-
iolist_to_binary(Body)),
279-
280-
case lhttpc:request(Url, post, Headers, Body, Timeout, LHttpcOpts) of
281-
{ok, {{200, _}, _ResponseHeaders, ResponseBody}} ->
275+
[{Key, iolist_to_binary(Value)}
276+
|| {Key, Value}
277+
<- awsv4:headers(AwsCreds,
278+
#{service => "kinesis",
279+
target_api => Target,
280+
method => "POST",
281+
region => Region,
282+
host => Host,
283+
signed_headers => SignedHeaders,
284+
aws_date => Date},
285+
iolist_to_binary(Body))],
286+
PoolName = kinetic_utils:pool_name(Region),
287+
Worker = ehttpc_pool:pick_worker(PoolName),
288+
case ehttpc:request(Worker, post, {"/", Headers, Body}, Timeout) of
289+
{ok, 200, _, ResponseBody} ->
282290
{ok, kinetic_utils:decode(ResponseBody)};
283-
{ok, {{Code, _}, ResponseHeaders, ResponseBody}} ->
284-
{error, {Code, ResponseHeaders, ResponseBody}};
285-
{error, E} ->
286-
{error, E}
291+
{ok, Code, RespHeaders, ResponseBody} ->
292+
{error, {Code, RespHeaders, ResponseBody}};
293+
{error, Error} ->
294+
{error, Error}
287295
end
288296
end
289297
end.
@@ -299,3 +307,17 @@ record_status(Record) ->
299307
get_value(Key, TupleList) ->
300308
{Key, Value} = lists:keyfind(Key, 1, TupleList),
301309
Value.
310+
311+
start_pool() ->
312+
PoolSize = application:get_env(?MODULE, pool_size, 100),
313+
GunOpts = application:get_env(?MODULE, gun_opts, []),
314+
lists:foreach(fun(Region) ->
315+
PoolName = kinetic_utils:pool_name(Region),
316+
Endpoint = kinetic_utils:endpoint(Region),
317+
ehttpc_sup:start_pool(PoolName,
318+
[{host, Endpoint},
319+
{port, 443},
320+
{pool_size, PoolSize},
321+
{gun_opts, GunOpts}])
322+
end,
323+
kinetic_utils:regions()).

src/kinetic_config.erl

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -85,32 +85,18 @@ handle_info(_Info, State) ->
8585

8686
% Internal implementation
8787

88-
region("us-east-1" ++ _R) ->
89-
"us-east-1";
90-
region("us-west-1" ++ _R) ->
91-
"us-west-1";
92-
region("us-west-2" ++ _R) ->
93-
"us-west-2";
94-
region("ap-northeast-1" ++ _R) ->
95-
"ap-northeast-1";
96-
region("ap-southeast-1" ++ _R) ->
97-
"ap-southeast-1";
98-
region("eu-west-1" ++ _R) ->
99-
"eu-west-1".
100-
10188
new_args(Opts) ->
10289
Region =
10390
case proplists:get_value(region, Opts, undefined) of
10491
undefined ->
10592
{ok, Zone} = imds:zone(),
106-
region(Zone);
93+
kinetic_utils:region(Zone);
10794
R ->
10895
R
10996
end,
11097

111-
LHttpcOpts = proplists:get_value(lhttpc_opts, Opts, []),
11298
DefaultTimeout = proplists:get_value(timeout, Opts, 5000),
113-
Host = kinetic_utils:endpoint("kinesis", Region),
99+
Host = kinetic_utils:endpoint(Region),
114100
Url = "https://" ++ Host,
115101

116102
%% erliam should support named profiles for using specific roles or preconfigured
@@ -133,7 +119,6 @@ new_args(Opts) ->
133119
date = awsv4:isonow(),
134120
host = Host,
135121
url = Url,
136-
lhttpc_opts = LHttpcOpts,
137122
timeout = DefaultTimeout,
138123
aws_credentials = erliam:credentials()}.
139124

@@ -143,7 +128,7 @@ new_args(Opts) ->
143128
merge_args(Args, []) ->
144129
Args;
145130
merge_args(Args, [{region, Region} | Rest]) ->
146-
Host = kinetic_utils:endpoint("kinesis", Region),
131+
Host = kinetic_utils:endpoint(Region),
147132
Url = "https://" ++ Host,
148133
merge_args(Args#kinetic_arguments{region = Region,
149134
host = Host,

src/kinetic_utils.erl

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,35 @@
11
-module(kinetic_utils).
22

3-
-export([endpoint/2, decode/1, encode/1]).
3+
-export([pool_name/1, regions/0, region/1, endpoint/1, decode/1, encode/1]).
44

5-
endpoint("kinesis", "us-east-1") ->
5+
pool_name("us-east-1" ++ _R) ->
6+
'kinetic_pool_us-east-1';
7+
pool_name("us-west-2" ++ _R) ->
8+
'kinetic_pool_us-west-2';
9+
pool_name("ap-southeast-1" ++ _R) ->
10+
'kinetic_pool_ap-southeast-1';
11+
pool_name("eu-west-1" ++ _R) ->
12+
'kinetic_pool_eu-west-1'.
13+
14+
regions() ->
15+
["us-east-1", "us-west-2", "ap-southeast-1", "eu-west-1"].
16+
17+
region("us-east-1" ++ _R) ->
18+
"us-east-1";
19+
region("us-west-2" ++ _R) ->
20+
"us-west-2";
21+
region("ap-southeast-1" ++ _R) ->
22+
"ap-southeast-1";
23+
region("eu-west-1" ++ _R) ->
24+
"eu-west-1".
25+
26+
endpoint("us-east-1") ->
627
"kinesis.us-east-1.amazonaws.com";
7-
endpoint("kinesis", "us-west-1") ->
8-
"kinesis.us-west-1.amazonaws.com";
9-
endpoint("kinesis", "us-west-2") ->
28+
endpoint("us-west-2") ->
1029
"kinesis.us-west-2.amazonaws.com";
11-
endpoint("kinesis", "eu-west-1") ->
30+
endpoint("eu-west-1") ->
1231
"kinesis.eu-west-1.amazonaws.com";
13-
endpoint("kinesis", "ap-northeast-1") ->
14-
"kinesis.ap-northeast-1.amazonaws.com";
15-
endpoint("kinesis", "ap-southeast-1") ->
32+
endpoint("ap-southeast-1") ->
1633
"kinesis.ap-southeast-1.amazonaws.com".
1734

1835
decode(<<"">>) ->

test/kinetic_config_tests.erl

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -69,17 +69,11 @@ test_passed_metadata() ->
6969
kinetic_config:start_link([{aws_access_key_id, "whatever"},
7070
{aws_secret_access_key, "secret"}]),
7171
?assert(ets:info(?KINETIC_STREAM) =/= undefined),
72-
{ok,
73-
#kinetic_arguments{aws_credentials = fake_creds,
74-
region = "us-east-1",
75-
lhttpc_opts = []}} =
72+
{ok, #kinetic_arguments{aws_credentials = fake_creds, region = "us-east-1"}} =
7673
kinetic_config:get_args(),
7774
kinetic_config:update_data([{aws_access_key_id, "whatever"},
7875
{aws_secret_access_key, "secret"}]),
79-
{ok,
80-
#kinetic_arguments{aws_credentials = fake_creds,
81-
region = "us-east-1",
82-
lhttpc_opts = []}} =
76+
{ok, #kinetic_arguments{aws_credentials = fake_creds, region = "us-east-1"}} =
8377
kinetic_config:get_args(),
8478
kinetic_config:stop(),
8579
{error, _} = kinetic_config:get_args(),

test/kinetic_sup_tests.erl

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,7 @@ test_supervisor() ->
2525
{ok, Pid} =
2626
kinetic_sup:start_link([{aws_access_key_id, "whatever"},
2727
{aws_secret_access_key, "secret"}]),
28-
{ok,
29-
#kinetic_arguments{aws_credentials = fake_creds,
30-
region = "us-east-1",
31-
lhttpc_opts = []}} =
28+
{ok, #kinetic_arguments{aws_credentials = fake_creds, region = "us-east-1"}} =
3229
kinetic_config:get_args(),
3330

3431
kinetic_sup:stop(Pid),

0 commit comments

Comments
 (0)