[Erlang] decrease memory usage (#9946)

* do not keep generator_state in cowboy match structure

* update generated example
This commit is contained in:
Mikael Lixenstrand
2021-07-16 05:34:18 +02:00
committed by GitHub
parent d99f16edc5
commit 2e030c0c47
6 changed files with 28 additions and 8 deletions

View File

@@ -29,10 +29,12 @@
-spec init(Req :: cowboy_req:req(), Opts :: {{packageName}}_router:init_opts()) ->
{cowboy_rest, Req :: cowboy_req:req(), State :: state()}.
init(Req, {Operations, LogicHandler, ValidatorState}) ->
init(Req, {Operations, LogicHandler, ValidatorMod}) ->
Method = cowboy_req:method(Req),
OperationID = maps:get(Method, Operations, undefined),
ValidatorState = ValidatorMod:get_validator_state(),
error_logger:info_msg("Attempt to process operation: ~p", [OperationID]),
State = #state{

View File

@@ -1,6 +1,6 @@
-module({{packageName}}_router).
-export([get_paths/1]).
-export([get_paths/1, get_validator_state/0]).
-type operations() :: #{
Method :: binary() => {{packageName}}_api:operation_id()
@@ -62,9 +62,15 @@ get_operations() ->
}{{^-last}},{{/-last}}{{/operation}}{{^-last}},{{/-last}}{{/operations}}{{/apis}}{{/apiInfo}}
}.
get_validator_state() ->
persistent_term:get({?MODULE, validator_state}).
prepare_validator() ->
R = jsx:decode(element(2, file:read_file(get_openapi_path()))),
jesse_state:new(R, [{default_schema_ver, <<"http://json-schema.org/draft-04/schema#">>}]).
JesseState = jesse_state:new(R, [{default_schema_ver, <<"http://json-schema.org/draft-04/schema#">>}]),
persistent_term:put({?MODULE, validator_state}, JesseState),
?MODULE.
get_openapi_path() ->

View File

@@ -29,10 +29,12 @@
-spec init(Req :: cowboy_req:req(), Opts :: openapi_router:init_opts()) ->
{cowboy_rest, Req :: cowboy_req:req(), State :: state()}.
init(Req, {Operations, LogicHandler, ValidatorState}) ->
init(Req, {Operations, LogicHandler, ValidatorMod}) ->
Method = cowboy_req:method(Req),
OperationID = maps:get(Method, Operations, undefined),
ValidatorState = ValidatorMod:get_validator_state(),
error_logger:info_msg("Attempt to process operation: ~p", [OperationID]),
State = #state{

View File

@@ -1,6 +1,6 @@
-module(openapi_router).
-export([get_paths/1]).
-export([get_paths/1, get_validator_state/0]).
-type operations() :: #{
Method :: binary() => openapi_api:operation_id()
@@ -157,9 +157,15 @@ get_operations() ->
}
}.
get_validator_state() ->
persistent_term:get({?MODULE, validator_state}).
prepare_validator() ->
R = jsx:decode(element(2, file:read_file(get_openapi_path()))),
jesse_state:new(R, [{default_schema_ver, <<"http://json-schema.org/draft-04/schema#">>}]).
JesseState = jesse_state:new(R, [{default_schema_ver, <<"http://json-schema.org/draft-04/schema#">>}]),
persistent_term:put({?MODULE, validator_state}, JesseState),
?MODULE.
get_openapi_path() ->

View File

@@ -29,10 +29,12 @@
-spec init(Req :: cowboy_req:req(), Opts :: openapi_router:init_opts()) ->
{cowboy_rest, Req :: cowboy_req:req(), State :: state()}.
init(Req, {Operations, LogicHandler, ValidatorState}) ->
init(Req, {Operations, LogicHandler, ValidatorMod}) ->
Method = cowboy_req:method(Req),
OperationID = maps:get(Method, Operations, undefined),
ValidatorState = ValidatorMod:get_validator_state(),
error_logger:info_msg("Attempt to process operation: ~p", [OperationID]),
State = #state{

View File

@@ -29,10 +29,12 @@
-spec init(Req :: cowboy_req:req(), Opts :: openapi_router:init_opts()) ->
{cowboy_rest, Req :: cowboy_req:req(), State :: state()}.
init(Req, {Operations, LogicHandler, ValidatorState}) ->
init(Req, {Operations, LogicHandler, ValidatorMod}) ->
Method = cowboy_req:method(Req),
OperationID = maps:get(Method, Operations, undefined),
ValidatorState = ValidatorMod:get_validator_state(),
error_logger:info_msg("Attempt to process operation: ~p", [OperationID]),
State = #state{