add ability to specify woody-opts on each call (#19)

* move network timeouts to sys.config
* add ability to specify woody-opts on each call
This commit is contained in:
Evgeny Levenets 2018-06-19 14:44:35 +03:00 committed by GitHub
parent 2d12274713
commit 84b2735c0e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 85 additions and 28 deletions

View File

@ -1,5 +1,9 @@
[
{dmt_client, [
{transport_opts, [
{recv_timeout, 60000},
{connect_timeout, 1000}
]},
{cache_update_interval, 5000}, % milliseconds
{max_cache_size, #{
elements => 20,

View File

@ -7,8 +7,13 @@
%% API
-export([checkout/1]).
-export([checkout/2]).
-export([checkout_object/2]).
-export([checkout_object/3]).
-export([commit/2]).
-export([commit/3]).
-export([pull/1]).
-export([pull/2]).
%% Supervisor callbacks
-export([init/1]).
@ -23,6 +28,7 @@
-export_type([commit/0]).
-export_type([object_ref/0]).
-export_type([history/0]).
-export_type([transport_opts/0]).
-include_lib("dmsl/include/dmsl_domain_config_thrift.hrl").
@ -32,12 +38,20 @@
-type commit() :: dmsl_domain_config_thrift:'Commit'().
-type object_ref() :: dmsl_domain_thrift:'Reference'().
-type history() :: dmsl_domain_config_thrift:'History'().
-type transport_opts() :: woody_client_thrift_http_transport:options() | undefined.
%% API
-spec checkout(ref()) -> snapshot() | no_return().
-spec checkout(ref()) ->
snapshot() | no_return().
checkout(Reference) ->
checkout(Reference, undefined).
-spec checkout(ref(), transport_opts()) ->
snapshot() | no_return().
checkout(Reference, Opts) ->
CacheResult = case Reference of
{head, #'Head'{}} ->
dmt_client_cache:get_latest();
@ -48,13 +62,20 @@ checkout(Reference) ->
{ok, Snapshot} ->
Snapshot;
{error, version_not_found} ->
dmt_client_cache:put(dmt_client_api:checkout(Reference))
dmt_client_cache:put(dmt_client_api:checkout(Reference, Opts))
end.
-spec checkout_object(ref(), object_ref()) -> dmsl_domain_config_thrift:'VersionedObject'() | no_return().
-spec checkout_object(ref(), object_ref()) ->
dmsl_domain_config_thrift:'VersionedObject'() | no_return().
checkout_object(Reference, ObjectReference) ->
#'Snapshot'{version = Version, domain = Domain} = checkout(Reference),
checkout_object(Reference, ObjectReference, undefined).
-spec checkout_object(ref(), object_ref(), transport_opts()) ->
dmsl_domain_config_thrift:'VersionedObject'() | no_return().
checkout_object(Reference, ObjectReference, Opts) ->
#'Snapshot'{version = Version, domain = Domain} = checkout(Reference, Opts),
case dmt_domain:get_object(ObjectReference, Domain) of
{ok, Object} ->
#'VersionedObject'{version = Version, object = Object};
@ -62,10 +83,29 @@ checkout_object(Reference, ObjectReference) ->
throw(object_not_found)
end.
-spec commit(version(), commit()) -> version() | no_return().
-spec commit(version(), commit()) ->
version() | no_return().
commit(Version, Commit) ->
dmt_client_api:commit(Version, Commit).
commit(Version, Commit, undefined).
-spec commit(version(), commit(), transport_opts()) ->
version() | no_return().
commit(Version, Commit, Opts) ->
dmt_client_api:commit(Version, Commit, Opts).
-spec pull(version()) ->
history() | no_return().
pull(Version) ->
pull(Version, undefined).
-spec pull(version(), transport_opts()) ->
history() | no_return().
pull(Version, Opts) ->
dmt_client_api:pull(Version, Opts).
%% Supervisor callbacks

View File

@ -1,39 +1,43 @@
-module(dmt_client_api).
-export([commit/2]).
-export([checkout/1]).
-export([pull/1]).
-export([checkout_object/2]).
-export([commit/3]).
-export([checkout/2]).
-export([pull/2]).
-export([checkout_object/3]).
-spec commit(dmt_client:version(), dmt_client:commit()) -> dmt_client:version() | no_return().
-spec commit(dmt_client:version(), dmt_client:commit(), dmt_client:transport_opts()) ->
dmt_client:version() | no_return().
commit(Version, Commit) ->
call('Repository', 'Commit', [Version, Commit]).
commit(Version, Commit, Opts) ->
call('Repository', 'Commit', [Version, Commit], Opts).
-spec checkout(dmt_client:ref()) -> dmt_client:snapshot() | no_return().
-spec checkout(dmt_client:ref(), dmt_client:transport_opts()) ->
dmt_client:snapshot() | no_return().
checkout(Reference) ->
call('Repository', 'Checkout', [Reference]).
checkout(Reference, Opts) ->
call('Repository', 'Checkout', [Reference], Opts).
-spec pull(dmt_client:version()) -> dmt_client:history() | no_return().
-spec pull(dmt_client:version(), dmt_client:transport_opts()) ->
dmt_client:history() | no_return().
pull(Version) ->
call('Repository', 'Pull', [Version]).
pull(Version, Opts) ->
call('Repository', 'Pull', [Version], Opts).
-spec checkout_object(dmt_client:ref(), dmt_client:object_ref()) -> dmsl_domain_thrift:'DomainObject'() | no_return().
-spec checkout_object(dmt_client:ref(), dmt_client:object_ref(), dmt_client:transport_opts()) ->
dmsl_domain_thrift:'DomainObject'() | no_return().
checkout_object(Reference, ObjectReference) ->
call('RepositoryClient', 'checkoutObject', [Reference, ObjectReference]).
checkout_object(Reference, ObjectReference, Opts) ->
call('RepositoryClient', 'checkoutObject', [Reference, ObjectReference], Opts).
call(ServiceName, Function, Args) ->
call(ServiceName, Function, Args, TransportOpts) ->
Url = get_service_url(ServiceName),
Service = get_service_modname(ServiceName),
Call = {Service, Function, Args},
Opts = #{
url => Url,
event_handler => scoper_woody_event_handler,
transport_opts => [{recv_timeout, 60000}, {connect_timeout, 1000}]
transport_opts => ensure_transport_opts(TransportOpts)
},
Context = woody_context:new(),
case woody_client:call(Call, Opts, Context) of
@ -53,3 +57,12 @@ get_service_module('Repository') ->
dmsl_domain_config_thrift;
get_service_module('RepositoryClient') ->
dmsl_domain_config_thrift.
-spec ensure_transport_opts(dmt_client:transport_opts()) ->
woody_client_thrift_http_transport:options().
ensure_transport_opts(Opts) when is_list(Opts) ->
Opts;
ensure_transport_opts(undefined) ->
Default = [{recv_timeout, 60000}, {connect_timeout, 1000}],
genlib_app:env(dmt_client, transport_opts, Default).

View File

@ -166,11 +166,11 @@ update_cache() ->
try
NewHead = case latest_snapshot() of
{ok, OldHead} ->
FreshHistory = dmt_client_api:pull(OldHead#'Snapshot'.version),
FreshHistory = dmt_client_api:pull(OldHead#'Snapshot'.version, undefined),
{ok, Head} = dmt_history:head(FreshHistory, OldHead),
Head;
{error, version_not_found} ->
dmt_client_api:checkout({head, #'Head'{}})
dmt_client_api:checkout({head, #'Head'{}}, undefined)
end,
ok = put_snapshot(NewHead),
{ok, NewHead#'Snapshot'.version}

View File

@ -55,7 +55,7 @@ poll(_C) ->
Ref = fixture_object_ref(1),
object_not_found = (catch dmt_client:checkout_object({head, #'Head'{}}, Ref)),
#'Snapshot'{version = Version1} = dmt_client:checkout({head, #'Head'{}}),
Version2 = dmt_client_api:commit(Version1, #'Commit'{ops = [{insert, #'InsertOp'{object = Object}}]}),
Version2 = dmt_client_api:commit(Version1, #'Commit'{ops = [{insert, #'InsertOp'{object = Object}}]}, undefined),
true = Version1 < Version2,
_ = dmt_client_cache:update(),
#'Snapshot'{version = Version2} = dmt_client:checkout({head, #'Head'{}}),