Skip to content

Commit

Permalink
erlang impl with grpcbox
Browse files Browse the repository at this point in the history
Signed-off-by: Pierre Fenoll <[email protected]>
  • Loading branch information
fenollp committed May 10, 2021
1 parent cc8c28e commit 46c0525
Show file tree
Hide file tree
Showing 13 changed files with 1,020 additions and 0 deletions.
1 change: 1 addition & 0 deletions erlang_grpcbox_bench/.dockerignore
19 changes: 19 additions & 0 deletions erlang_grpcbox_bench/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
.rebar3
_*
.eunit
*.o
*.beam
*.plt
*.swp
*.swo
.erlang.cookie
ebin
log
erl_crash.dump
.rebar
logs
_build
.idea
*.iml
rebar3.crashdump
*~
51 changes: 51 additions & 0 deletions erlang_grpcbox_bench/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
FROM erlang:23-alpine as builder

WORKDIR /src/app
COPY erlang_grpcbox_bench/rebar.* .

RUN \
--mount=type=cache,target=/root/.cache/rebar3 \
--mount=type=cache,target=/var/cache/apk ln -vs /var/cache/apk /etc/apk/cache && \
set -x \
&& apk update && apk upgrade \
&& apk add git emacs-nox \
&& git version \
&& rebar3 --version \
&& rebar3 do get-deps, compile

COPY proto proto
COPY erlang_grpcbox_bench/config config
COPY erlang_grpcbox_bench/src src

RUN \
--mount=type=cache,target=/root/.cache/rebar3 \
set -x \
&& git init \
&& git add -A . \
&& rebar3 do grpc gen, fmt \
&& git --no-pager diff && [[ 0 -eq $(git --no-pager diff --name-only | wc -l) ]]

RUN \
--mount=type=cache,target=/root/.cache/rebar3 \
set -x \
&& rebar3 as prod tar \
&& mkdir -p /opt/rel \
&& tar -zxf $PWD/_build/prod/rel/*/*.tar.gz -C /opt/rel

# ---

FROM alpine:3.13

WORKDIR /app
COPY --from=builder /opt/rel .

RUN set -x \
&& apk update && apk upgrade \
&& apk add openssl-dev ncurses \
&& rm -r /var/cache/apk/* \
&& ln -s $PWD/bin/erlang_grpcbox_bench $PWD/bin/rel

EXPOSE 50051

ENTRYPOINT ["/app/bin/rel"]
CMD ["foreground"]
25 changes: 25 additions & 0 deletions erlang_grpcbox_bench/config/sys.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
[{sasl, [{utc_log, true}
]}

,{grpcbox, [{servers, [#{grpc_opts => #{service_protos => [helloworld_pb
]
,services => #{'helloworld.Greeter' => egb_handler
}
}
,transport_opts => #{ssl => false}
,listen_opts => #{port => 50051
,ip => {0,0,0,0}
}
}]}
]}

,{kernel, [{logger_level, debug}
,{logger, [{handler, default, logger_std_h, #{formatter => {flatlog, #{map_depth => 3
,term_depth => 50
,colored => true
}}
}}
]}
]}

].
52 changes: 52 additions & 0 deletions erlang_grpcbox_bench/rebar.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
{profiles, [{prod, [{relx, [{dev_mode, false}
,{include_erts, true}
,{include_src, false}
]}
,{grpc, [{protos, "./proto"}
]}
]}
]}.

{erl_opts, [debug_info
,warnings_as_errors
,warn_unused_vars
,{i, "src"}
]}.

{deps, [grpcbox
,flatlog
]}.

{grpc, [{protos, "../proto/helloworld"}
,{gpb_opts, [{module_name_suffix, "_pb"}
,strings_as_binaries
,type_specs
,{verify, always}
,report
]}
]}.

{plugins, [grpcbox_plugin
,rebar3_fmt
]}.

{shell, [{apps, [erlang_grpcbox_bench]}
,{config, "config/sys.config"}
]}.

{relx, [{release, {erlang_grpcbox_bench, "1.0.0"}, [erlang_grpcbox_bench]}
,{dev_mode, true}
,{include_erts, false}
%% ,{vm_args, "config/vm.args"}
,{sys_config, "config/sys.config"}
]}.

{xref_checks, [undefined_function_calls
,undefined_functions
%% ,exports_not_used
,locals_not_used
,deprecated_function_calls
,deprecated_functions
]}.
{xref_ignores, [helloworld_pb
]}.
26 changes: 26 additions & 0 deletions erlang_grpcbox_bench/rebar.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{"1.2.0",
[{<<"acceptor_pool">>,{pkg,<<"acceptor_pool">>,<<"1.0.0">>},1},
{<<"chatterbox">>,{pkg,<<"ts_chatterbox">>,<<"0.11.0">>},1},
{<<"ctx">>,{pkg,<<"ctx">>,<<"0.6.0">>},1},
{<<"flatlog">>,{pkg,<<"flatlog">>,<<"0.1.1">>},0},
{<<"gproc">>,{pkg,<<"gproc">>,<<"0.8.0">>},1},
{<<"grpcbox">>,{pkg,<<"grpcbox">>,<<"0.14.0">>},0},
{<<"hpack">>,{pkg,<<"hpack_erl">>,<<"0.2.3">>},2}]}.
[
{pkg_hash,[
{<<"acceptor_pool">>, <<"43C20D2ACAE35F0C2BCD64F9D2BDE267E459F0F3FD23DAB26485BF518C281B21">>},
{<<"chatterbox">>, <<"B8F372C706023EB0DE5BF2976764EDB27C70FE67052C88C1F6A66B3A5626847F">>},
{<<"ctx">>, <<"8FF88B70E6400C4DF90142E7F130625B82086077A45364A78D208ED3ED53C7FE">>},
{<<"flatlog">>, <<"4A181C1B7596A0181C727C06858CBF4BA8C273643D0A2A2FB434EAD961584575">>},
{<<"gproc">>, <<"CEA02C578589C61E5341FCE149EA36CCEF236CC2ECAC8691FBA408E7EA77EC2F">>},
{<<"grpcbox">>, <<"3EB321BCD2275BAF8B54CF381FEB7B0559A50C02544DE28FDA039C7F2F9D1A7A">>},
{<<"hpack">>, <<"17670F83FF984AE6CD74B1C456EDDE906D27FF013740EE4D9EFAA4F1BF999633">>}]},
{pkg_hash_ext,[
{<<"acceptor_pool">>, <<"0CBCD83FDC8B9AD2EEE2067EF8B91A14858A5883CB7CD800E6FCD5803E158788">>},
{<<"chatterbox">>, <<"722FE2BAD52913AB7E87D849FC6370375F0C961FFB2F0B5E6D647C9170C382A6">>},
{<<"ctx">>, <<"A14ED2D1B67723DBEBBE423B28D7615EB0BDCBA6FF28F2D1F1B0A7E1D4AA5FC2">>},
{<<"flatlog">>, <<"7F1DB0E971A42D5929D5C297D3D8D17340BF2DACE2AFFF292B2AFBD4AD6B8452">>},
{<<"gproc">>, <<"580ADAFA56463B75263EF5A5DF4C86AF321F68694E7786CB057FD805D1E2A7DE">>},
{<<"grpcbox">>, <<"E24159B7B6D3F9869BBE528845C0125FED2259366BA908FD04A1F45FE81D0660">>},
{<<"hpack">>, <<"06F580167C4B8B8A6429040DF36CC93BBA6D571FAEAEC1B28816523379CBB23A">>}]}
].
28 changes: 28 additions & 0 deletions erlang_grpcbox_bench/src/egb_handler.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
%% Copyright © 2021 Pierre Fenoll ‹[email protected]
%% -*- coding: utf-8 -*-
-module(egb_handler).
-behavior(helloworld_greeter_bhvr).

-include_lib("kernel/include/logger.hrl").

-export([say_hello/2]).

-define(NOW(), erlang:monotonic_time()).
-define(SINCE(T), erlang:convert_time_unit(?NOW()-T, native, microsecond)).

%% @doc Unary RPC
-spec say_hello(ctx:ctx(), helloworld_pb:hello_request()) ->
{ok, helloworld_pb:hello_reply(), ctx:ctx()} | grpcbox_stream:grpc_error_response().

say_hello(Ctx, Req) ->
StartTime = erlang:monotonic_time(),
?LOG_DEBUG(#{status=>handling, req=>Req}),

#{name := Name
} = Req,

Rep = #{message => Name
},

?LOG_INFO(#{status=>handled, rep=>Rep, in_us=>?SINCE(StartTime)}),
{ok, Rep, Ctx}.
18 changes: 18 additions & 0 deletions erlang_grpcbox_bench/src/erlang_grpcbox_bench.app.src
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{application, erlang_grpcbox_bench,
[{description, "An OTP app for https://github.com/LesnyRumcajs/grpc_bench"}
,{vsn, "0.1.0"}
,{modules, []}
,{registered, []}
,{env, []}
,{mod, {erlang_grpcbox_bench_app,[]}}
,{applications, [kernel
,stdlib
,sasl
,flatlog
,ctx
,grpcbox
]}

,{licenses, ["MIT"]}
,{links, []}
]}.
16 changes: 16 additions & 0 deletions erlang_grpcbox_bench/src/erlang_grpcbox_bench_app.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
%%%-------------------------------------------------------------------
%% @doc erlang_grpcbox_bench public API
%% @end
%%%-------------------------------------------------------------------
-module(erlang_grpcbox_bench_app).
-behaviour(application).

-export([start/2, stop/1]).

start(_StartType, _StartArgs) ->
erlang_grpcbox_bench_sup:start_link().

stop(_State) ->
ok.

%% internal functions
32 changes: 32 additions & 0 deletions erlang_grpcbox_bench/src/erlang_grpcbox_bench_sup.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
%%%-------------------------------------------------------------------
%% @doc erlang_grpcbox_bench top level supervisor.
%% @end
%%%-------------------------------------------------------------------
-module(erlang_grpcbox_bench_sup).
-behaviour(supervisor).

-export([start_link/0]).
-export([init/1]).

-define(SERVER, ?MODULE).

start_link() ->
supervisor:start_link({local, ?SERVER}, ?MODULE, []).

%% sup_flags() = #{strategy => strategy(), % optional
%% intensity => non_neg_integer(), % optional
%% period => pos_integer()} % optional
%% child_spec() = #{id => child_id(), % mandatory
%% start => mfargs(), % mandatory
%% restart => restart(), % optional
%% shutdown => shutdown(), % optional
%% type => worker(), % optional
%% modules => modules()} % optional
init([]) ->
SupFlags = #{strategy => one_for_all,
intensity => 0,
period => 1},
ChildSpecs = [],
{ok, {SupFlags, ChildSpecs}}.

%% internal functions
13 changes: 13 additions & 0 deletions erlang_grpcbox_bench/src/helloworld_greeter_bhvr.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
%%%-------------------------------------------------------------------
%% @doc Behaviour to implement for grpc service helloworld.Greeter.
%% @end
%%%-------------------------------------------------------------------

%% this module was generated on 2021-05-02T13:23:03+00:00 and should not be modified manually

-module(helloworld_greeter_bhvr).

%% @doc Unary RPC
-callback say_hello(ctx:ctx(), helloworld_pb:hello_request()) ->
{ok, helloworld_pb:hello_reply(), ctx:ctx()} | grpcbox_stream:grpc_error_response().

43 changes: 43 additions & 0 deletions erlang_grpcbox_bench/src/helloworld_greeter_client.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
%%%-------------------------------------------------------------------
%% @doc Client module for grpc service helloworld.Greeter.
%% @end
%%%-------------------------------------------------------------------

%% this module was generated on 2021-05-02T13:23:03+00:00 and should not be modified manually

-module(helloworld_greeter_client).

-compile(export_all).
-compile(nowarn_export_all).

-include_lib("grpcbox/include/grpcbox.hrl").

-define(is_ctx(Ctx), is_tuple(Ctx) andalso element(1, Ctx) =:= ctx).

-define(SERVICE, 'helloworld.Greeter').
-define(PROTO_MODULE, 'helloworld_pb').
-define(MARSHAL_FUN(T), fun(I) -> ?PROTO_MODULE:encode_msg(I, T) end).
-define(UNMARSHAL_FUN(T), fun(I) -> ?PROTO_MODULE:decode_msg(I, T) end).
-define(DEF(Input, Output, MessageType), #grpcbox_def{service=?SERVICE,
message_type=MessageType,
marshal_fun=?MARSHAL_FUN(Input),
unmarshal_fun=?UNMARSHAL_FUN(Output)}).

%% @doc Unary RPC
-spec say_hello(helloworld_pb:hello_request()) ->
{ok, helloworld_pb:hello_reply(), grpcbox:metadata()} | grpcbox_stream:grpc_error_response() | {error, any()}.
say_hello(Input) ->
say_hello(ctx:new(), Input, #{}).

-spec say_hello(ctx:t() | helloworld_pb:hello_request(), helloworld_pb:hello_request() | grpcbox_client:options()) ->
{ok, helloworld_pb:hello_reply(), grpcbox:metadata()} | grpcbox_stream:grpc_error_response() | {error, any()}.
say_hello(Ctx, Input) when ?is_ctx(Ctx) ->
say_hello(Ctx, Input, #{});
say_hello(Input, Options) ->
say_hello(ctx:new(), Input, Options).

-spec say_hello(ctx:t(), helloworld_pb:hello_request(), grpcbox_client:options()) ->
{ok, helloworld_pb:hello_reply(), grpcbox:metadata()} | grpcbox_stream:grpc_error_response() | {error, any()}.
say_hello(Ctx, Input, Options) ->
grpcbox_client:unary(Ctx, <<"/helloworld.Greeter/SayHello">>, Input, ?DEF(hello_request, hello_reply, <<"helloworld.HelloRequest">>), Options).

Loading

0 comments on commit 46c0525

Please sign in to comment.