feat: Add deadline opt for woody_client (#156)

This commit is contained in:
Jarosław Rogov 2021-11-12 15:45:55 +03:00 committed by GitHub
parent 2d7efff936
commit 6f818c57e3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 2 deletions

View File

@ -35,13 +35,14 @@
%% Thrift %% Thrift
-type service_name() :: atom(). -type service_name() :: atom().
-type service() :: {module(), service_name()}. -type service() :: {module(), service_name()}.
-type context() :: woody_context:ctx().
-type func() :: atom(). -type func() :: atom().
-type args() :: tuple(). -type args() :: tuple().
-type request() :: {service(), func(), args()}. -type request() :: {service(), func(), args()}.
-type result() :: _. -type result() :: _.
-type th_handler() :: {service(), handler(options())}. -type th_handler() :: {service(), handler(options())}.
-export_type([request/0, result/0, service/0, service_name/0, func/0, args/0, th_handler/0]). -export_type([request/0, result/0, service/0, context/0, service_name/0, func/0, args/0, th_handler/0]).
-type rpc_type() :: call | cast. -type rpc_type() :: call | cast.

View File

@ -16,6 +16,7 @@
event_handler := woody:ev_handlers(), event_handler := woody:ev_handlers(),
protocol => thrift, protocol => thrift,
transport => http, transport => http,
deadline => woody:deadline(),
%% Set to override protocol handler module selection, useful for test purposes, rarely %% Set to override protocol handler module selection, useful for test purposes, rarely
%% if ever needed otherwise. %% if ever needed otherwise.
protocol_handler_override => module(), protocol_handler_override => module(),
@ -51,7 +52,8 @@ call(Request, Options) ->
| {exception, woody_error:business_error()} | {exception, woody_error:business_error()}
| no_return(). | no_return().
call(Request, Options = #{event_handler := EvHandler}, Context) -> call(Request, Options = #{event_handler := EvHandler}, Context) ->
Child = woody_context:new_child(Context), Deadline = maps:get(deadline, Context, undefined),
Child = attach_deadline(Deadline, woody_context:new_child(Context)),
WoodyState = woody_state:new(client, Child, EvHandler), WoodyState = woody_state:new(client, Child, EvHandler),
case call_safe(Request, Options, WoodyState) of case call_safe(Request, Options, WoodyState) of
Result = {ok, _} -> Result = {ok, _} ->
@ -92,3 +94,8 @@ handle_client_error(Class, Error, Stacktrace, WoodyState) ->
final => false final => false
}), }),
{error, {system, {internal, result_unexpected, <<"client error: ", Details/binary>>}}}. {error, {system, {internal, result_unexpected, <<"client error: ", Details/binary>>}}}.
attach_deadline(undefined, Context) ->
Context;
attach_deadline(Deadline, Context) ->
woody_context:set_deadline(Deadline, Context).