Skip to content

Commit 1cbb762

Browse files
authored
Conforms rpc context w/ woody helper; wraps w/ hg_context loading (#185)
* Conforms rpc context w/ woody helper; wraps w/ `hg_context` loading * Bumps woody@v1.1.1
1 parent 4da01a2 commit 1cbb762

4 files changed

Lines changed: 31 additions & 19 deletions

File tree

apps/hg_progressor/src/hg_progressor.erl

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -117,14 +117,22 @@ cleanup() ->
117117

118118
%% Processor
119119

120-
-spec process({task_t(), encoded_args(), process()}, map(), encoded_ctx()) -> process_result().
121-
process({CallType, BinArgs, Process}, #{ns := NS} = Options, Ctx) ->
122-
_ = set_context(Ctx),
120+
-spec process({task_t(), encoded_args(), process()}, hg_woody_service_wrapper:handler_opts(), encoded_ctx()) ->
121+
process_result().
122+
process({CallType, BinArgs, Process}, #{ns := NS} = Options, BinCtx) ->
123+
{WoodyContext0, OtelCtx} = woody_rpc_helper:decode_rpc_context(marshal(term, BinCtx)),
124+
ok = woody_rpc_helper:attach_otel_context(OtelCtx),
123125
#{last_event_id := LastEventID} = Process,
124126
Machine = marshal(process, Process#{ns => NS}),
125127
Func = marshal(function, CallType),
126128
Args = marshal(args, {CallType, BinArgs, Machine}),
127-
handle_result(hg_machine:handle_function(Func, {Args}, Options), LastEventID).
129+
WoodyContext = hg_woody_service_wrapper:ensure_woody_deadline_set(WoodyContext0, Options),
130+
ok = hg_context:save(hg_woody_service_wrapper:create_context(WoodyContext, Options)),
131+
try
132+
handle_result(hg_machine:handle_function(Func, {Args}, Options), LastEventID)
133+
after
134+
hg_context:cleanup()
135+
end.
128136

129137
%% Internal functions
130138

@@ -188,18 +196,17 @@ handle_exception({exception, Class, Reason}) ->
188196
erlang:raise(Class, Reason, []).
189197

190198
get_context() ->
191-
try hg_context:load() of
192-
Ctx ->
193-
unmarshal(term, Ctx)
194-
catch
195-
_:_ ->
196-
unmarshal(term, <<>>)
197-
end.
198-
199-
set_context(<<>>) ->
200-
hg_context:save(hg_context:create(#{party_client => #{}}));
201-
set_context(BinContext) ->
202-
hg_context:save(marshal(term, BinContext)).
199+
WoodyContext =
200+
try hg_context:load() of
201+
Ctx ->
202+
hg_context:get_woody_context(Ctx)
203+
catch
204+
Class:Reason ->
205+
_ = logger:warning("Failed to load context with error class '~s' and reason: ~p", [Class, Reason]),
206+
_ = logger:info("Creating empty fallback context"),
207+
woody_context:new()
208+
end,
209+
unmarshal(term, woody_rpc_helper:encode_rpc_context(WoodyContext, otel_ctx:get_current())).
203210

204211
%% Marshalling
205212

apps/hg_proto/src/hg_woody_service_wrapper.erl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
-export([handle_function/4]).
99
-export([raise/1]).
1010

11+
%% Utils
12+
-export([create_context/2]).
13+
-export([ensure_woody_deadline_set/2]).
14+
1115
-export_type([handler_opts/0]).
1216

1317
-type handler_opts() :: #{
@@ -47,6 +51,7 @@ handle_function(Func, Args, WoodyContext0, #{handler := Handler} = Opts) ->
4751
raise(Exception) ->
4852
woody_error:raise(business, Exception).
4953

54+
-spec create_context(woody_context:ctx(), handler_opts()) -> hg_context:context().
5055
create_context(WoodyContext, Opts) ->
5156
ContextOptions = #{
5257
woody_context => WoodyContext

rebar.config

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
{cache, "2.3.3"},
3131
{gproc, "0.9.0"},
3232
{genlib, {git, "https://github.com/valitydev/genlib.git", {tag, "v1.1.0"}}},
33-
{woody, {git, "https://github.com/valitydev/woody_erlang.git", {tag, "v1.1.0"}}},
33+
{woody, {git, "https://github.com/valitydev/woody_erlang", {tag, "v1.1.1"}}},
3434
{damsel, {git, "https://github.com/valitydev/damsel.git", {tag, "v2.2.17"}}},
3535
{payproc_errors, {git, "https://github.com/valitydev/payproc-errors-erlang.git", {branch, "master"}}},
3636
{mg_proto, {git, "https://github.com/valitydev/machinegun-proto.git", {branch, "master"}}},

rebar.lock

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,8 @@
126126
1},
127127
{<<"unicode_util_compat">>,{pkg,<<"unicode_util_compat">>,<<"0.7.1">>},2},
128128
{<<"woody">>,
129-
{git,"https://github.com/valitydev/woody_erlang.git",
130-
{ref,"cc983a9423325ba1d6a509775eb6ff7ace721539"}},
129+
{git,"https://github.com/valitydev/woody_erlang",
130+
{ref,"3de9a236b66807d5397b28aed42e2b1c85f36739"}},
131131
0}]}.
132132
[
133133
{pkg_hash,[

0 commit comments

Comments
 (0)