MSPF-532: get rid of rfc3339 library (#18)

This commit is contained in:
Sergei Shuvatov 2020-02-19 19:40:12 +03:00 committed by GitHub
parent f2d6e2e00f
commit d1b3d96f3d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 44 additions and 24 deletions

View File

@ -15,10 +15,11 @@ services:
condition: service_healthy
machinegun:
image: dr.rbkmoney.com/rbkmoney/machinegun:5e26162266a3bcf857852cb7844e5626fb0ebf7a
image: dr2.rbkmoney.com/rbkmoney/machinegun:3d89232e20e4f52129d7355b209a9a5a54ad5dbf
command: /opt/machinegun/bin/machinegun foreground
volumes:
- ./test/machinegun/config.yaml:/opt/machinegun/etc/config.yaml
- ./test/machinegun/cookie:/opt/machinegun/etc/cookie
healthcheck:
test: "curl http://localhost:8022/"
interval: 5s

View File

@ -31,9 +31,6 @@
{genlib,
{git, "https://github.com/rbkmoney/genlib.git", {branch, "master"}}
},
{rfc3339,
"0.2.2"
},
{woody,
{git, "https://github.com/rbkmoney/woody_erlang.git", {branch, "master"}}
},

View File

@ -6,8 +6,8 @@
{git,"https://github.com/rbkmoney/cg_mon.git",
{ref,"5a87a37694e42b6592d3b4164ae54e0e87e24e18"}},
2},
{<<"cowboy">>,{pkg,<<"cowboy">>,<<"2.5.0">>},1},
{<<"cowlib">>,{pkg,<<"cowlib">>,<<"2.6.0">>},2},
{<<"cowboy">>,{pkg,<<"cowboy">>,<<"2.7.0">>},1},
{<<"cowlib">>,{pkg,<<"cowlib">>,<<"2.8.0">>},2},
{<<"folsom">>,
{git,"https://github.com/folsom-project/folsom.git",
{ref,"eeb1cc467eb64bd94075b95b8963e80d8b4df3df"}},
@ -30,8 +30,7 @@
0},
{<<"mimerl">>,{pkg,<<"mimerl">>,<<"1.2.0">>},2},
{<<"parse_trans">>,{pkg,<<"parse_trans">>,<<"3.3.0">>},3},
{<<"ranch">>,{pkg,<<"ranch">>,<<"1.6.2">>},2},
{<<"rfc3339">>,{pkg,<<"rfc3339">>,<<"0.2.2">>},0},
{<<"ranch">>,{pkg,<<"ranch">>,<<"1.7.1">>},2},
{<<"snowflake">>,
{git,"https://github.com/rbkmoney/snowflake.git",
{ref,"0a598108f6582affe3b4ae550fc5b9f2062e318a"}},
@ -44,23 +43,22 @@
{<<"unicode_util_compat">>,{pkg,<<"unicode_util_compat">>,<<"0.4.1">>},3},
{<<"woody">>,
{git,"https://github.com/rbkmoney/woody_erlang.git",
{ref,"e46a3b6303721d3c096d3cb65359f9b8b7652506"}},
{ref,"8b8c0e27796a6fc8bed4f474313e4c3487e10c82"}},
0}]}.
[
{pkg_hash,[
{<<"bear">>, <<"16264309AE5D005D03718A5C82641FCC259C9E8F09ADEB6FD79CA4271168656F">>},
{<<"cache">>, <<"3C11DBF4CD8FCD5787C95A5FB2A04038E3729CFCA0386016EEA8C953AB48A5AB">>},
{<<"certifi">>, <<"867CE347F7C7D78563450A18A6A28A8090331E77FA02380B4A21962A65D36EE5">>},
{<<"cowboy">>, <<"4EF3AE066EE10FE01EA3272EDC8F024347A0D3EB95F6FBB9AED556DACBFC1337">>},
{<<"cowlib">>, <<"8AA629F81A0FC189F261DC98A42243FA842625FEEA3C7EC56C48F4CCDB55490F">>},
{<<"cowboy">>, <<"91ED100138A764355F43316B1D23D7FF6BDB0DE4EA618CB5D8677C93A7A2F115">>},
{<<"cowlib">>, <<"FD0FF1787DB84AC415B8211573E9A30A3EBE71B5CBFF7F720089972B2319C8A4">>},
{<<"gproc">>, <<"CEA02C578589C61E5341FCE149EA36CCEF236CC2ECAC8691FBA408E7EA77EC2F">>},
{<<"hackney">>, <<"07E33C794F8F8964EE86CEBEC1A8ED88DB5070E52E904B8F12209773C1036085">>},
{<<"idna">>, <<"689C46CBCDF3524C44D5F3DDE8001F364CD7608A99556D8FBD8239A5798D4C10">>},
{<<"metrics">>, <<"25F094DEA2CDA98213CECC3AEFF09E940299D950904393B2A29D191C346A8486">>},
{<<"mimerl">>, <<"67E2D3F571088D5CFD3E550C383094B47159F3EEE8FFA08E64106CDF5E981BE3">>},
{<<"parse_trans">>, <<"09765507A3C7590A784615CFD421D101AEC25098D50B89D7AA1D66646BC571C1">>},
{<<"ranch">>, <<"6DB93C78F411EE033DBB18BA8234C5574883ACB9A75AF0FB90A9B82EA46AFA00">>},
{<<"rfc3339">>, <<"1552DF616ACA368D982E9F085A0E933B6688A3F4938A671798978EC2C0C58730">>},
{<<"ranch">>, <<"6B1FAB51B49196860B733A49C07604465A47BDB78AA10C1C16A3D199F7F8C881">>},
{<<"ssl_verify_fun">>, <<"6EAF7AD16CB568BB01753DBBD7A95FF8B91C7979482B95F38443FE2C8852A79B">>},
{<<"unicode_util_compat">>, <<"D869E4C68901DD9531385BB0C8C40444EBF624E60B6962D95952775CAC5E90CD">>}]}
].

View File

@ -21,6 +21,8 @@
schema := machinery_mg_schema:schema()
).
-define(MICROS_PER_SEC, (1000 * 1000)).
%% Server types
-type backend_config() :: #{
?BACKEND_CORE_OPTS
@ -361,11 +363,10 @@ marshal({schema, Schema, T}, V) ->
% Marshal properly
machinery_mg_schema:marshal(Schema, T, V);
marshal(timestamp, {{Date, Time}, USec} = V) ->
{ok, Result} = rfc3339:format({Date, Time, USec, 0}),
% ensure that Result is actually a binary
{true, _} = {is_binary(Result), V},
Result;
marshal(timestamp, {DateTime, USec}) ->
Ts = genlib_time:daytime_to_unixtime(DateTime) * ?MICROS_PER_SEC + USec,
Str = calendar:system_time_to_rfc3339(Ts, [{unit, microsecond}, {offset, "Z"}]),
erlang:list_to_binary(Str);
marshal({list, T}, V) when is_list(V) ->
[marshal(T, E) || E <- V];
@ -518,12 +519,14 @@ unmarshal({schema, Schema, T}, V) ->
machinery_mg_schema:unmarshal(Schema, T, V);
unmarshal(timestamp, V) when is_binary(V) ->
case rfc3339:parse(V) of
{ok, {Date, Time, USec, TZOffset}} when TZOffset == undefined orelse TZOffset == 0 ->
{{Date, Time}, USec};
{ok, _} ->
erlang:error(badarg, [timestamp, V, badoffset]);
{error, Reason} ->
ok = assert_is_utc(V),
Str = erlang:binary_to_list(V),
try
Micros = calendar:rfc3339_to_system_time(Str, [{unit, microsecond}]),
Datetime = calendar:system_time_to_universal_time(Micros, microsecond),
{Datetime, Micros rem ?MICROS_PER_SEC}
catch
error:Reason ->
erlang:error(badarg, [timestamp, V, Reason])
end;
@ -555,3 +558,20 @@ unmarshal(integer, V) when is_integer(V) ->
unmarshal(T, V) ->
erlang:error(badarg, [T, V]).
-spec assert_is_utc(binary()) ->
ok | no_return().
assert_is_utc(Rfc3339) ->
Size0 = erlang:byte_size(Rfc3339),
Size1 = Size0 - 1,
Size6 = Size0 - 6,
case Rfc3339 of
<<_:Size1/bytes, "Z">> ->
ok;
<<_:Size6/bytes, "+00:00">> ->
ok;
<<_:Size6/bytes, "-00:00">> ->
ok;
_ ->
erlang:error(badarg, [timestamp, Rfc3339, badoffset])
end.

View File

@ -1,5 +1,8 @@
service_name: machinegun
erlang:
secret_cookie_file: "/opt/machinegun/etc/cookie"
namespaces:
payproc/tags:
processor:

1
test/machinegun/cookie Normal file
View File

@ -0,0 +1 @@
9e6245a7a6e15f75769a4d87183b090a