changed gproc to process dictionary

This commit is contained in:
Артем 2022-08-30 22:43:59 +03:00
parent d6fd847e68
commit a27bd8f298
3 changed files with 104 additions and 13 deletions

View File

@ -14,7 +14,6 @@
cowboy,
woody,
scoper, % should be before any scoper event handler usage
gproc,
dmt_client,
party_client,
bender_client,

View File

@ -0,0 +1,95 @@
-module(hg_container).
-type key() :: term().
-type value() :: term().
-export_type([key/0]).
-export_type([value/0]).
%% API
-export([bind/2]).
-export([maybe_bind/2]).
-export([update/2]).
-export([maybe_update/2]).
-export([maybe_inject/1]).
-export([inject/1]).
-export([unbind/1]).
%% Internal types
-type wrapped_value() :: #{
value := value()
}.
%% API
-spec bind(key(), value()) -> ok.
bind(Key, Value) ->
case put(Key, wrap(Value)) of
undefined ->
ok;
_ ->
%% TODO log warning cause bind we use only onece
ok
end.
-spec maybe_bind(key(), value()) -> ok.
maybe_bind(_Key, undefined) ->
ok;
maybe_bind(Key, Value) ->
bind(Key, Value).
-spec update(key(), value()) -> ok.
update(Key, Value) ->
case put(Key, wrap(Value)) of
undefined ->
%% TODO log warning cause update we use only after bind
ok;
_ ->
ok
end.
-spec maybe_update(key(), value()) -> ok.
maybe_update(_Key, undefined) ->
ok;
maybe_update(Key, Value) ->
update(Key, Value).
-spec maybe_inject(key()) -> value().
maybe_inject(Key) ->
get(Key).
-spec inject(key()) -> value().
inject(Key) ->
case maybe_inject(Key) of
undefined ->
%% TODO log error cause we try to inject unbinded value
undefined;
Value ->
unwrap(Value)
end.
-spec unbind(key()) -> ok.
unbind(Key) ->
case erase(Key) of
undefined ->
%% TODO log warning cause we try to erase unused key
ok;
_ ->
ok
end.
%% Internal
-spec wrap(value()) -> wrapped_value().
wrap(Value) ->
#{
value => Value
}.
-spec unwrap(wrapped_value()) -> value().
unwrap(#{value := Value}) ->
Value.

View File

@ -49,23 +49,20 @@ create(Options0) ->
-spec save(context()) -> ok.
save(Context) ->
true =
try
gproc:reg(?REGISTRY_KEY, Context)
catch
error:badarg ->
gproc:set_value(?REGISTRY_KEY, Context)
end,
ok.
case hg_container:maybe_inject(?REGISTRY_KEY) of
undefined ->
hg_container:bind(?REGISTRY_KEY, Context);
_OldContext ->
hg_container:update(?REGISTRY_KEY, Context)
end.
-spec load() -> context() | no_return().
-spec load() -> context().
load() ->
gproc:get_value(?REGISTRY_KEY).
hg_container:inject(?REGISTRY_KEY).
-spec cleanup() -> ok.
cleanup() ->
true = gproc:unreg(?REGISTRY_KEY),
ok.
hg_container:unbind(?REGISTRY_KEY).
-spec get_woody_context(context()) -> woody_context().
get_woody_context(#{woody_context := WoodyContext}) ->