1.6.8 Module anvl_resource

Resource is a mechanism for limiting paralellism of certain operations.

This feature has a limited use in ANVL, since normally Erlang VM does a great job managing millions of concurrent tasks. Therefore, unlike, say, make ANVL doesn’t have a global -j flag.

Nonetheless, parallelism of certain operations (invoking external processes, downloading files from the net) should be globally limited. ANVL allows to create a resource for each type of such operation.

1.6.8.1 Types

resource()
-type resource() :: term().

Type of tokens identifying the resources.

1.6.8.2 Functions

with/2
-spec with(resource(), fun(() -> A)) -> A.

Run an operation with an aquired resource semaphore.

Warning: don’t abuse this API. It is only useful for calling external commands, like calling gcc or git or heavy computations.

There are some limitations to avoid deadlocks:

  • Condition can hold at most one resource at any given time.
  • While condition holds a resource, it cannot invoke preconditions.
declare(Resource, Max)
-spec declare(resource(), pos_integer()) -> ok.

Declare a new resource type identified by token Resource with initial capacity Max. This function should be called by the plugin in its ‘init/0’ callback.

In order to make the resource capacity configurable by the user, it should be also declared in the plugin’s configuration model as a value of non_neg_integer() type and with anvl_resource metatype. For example:

project_model() ->
 #{git =>
    #{max_jobs =>
       {[value, cli_param, anvl_resource],
           #{ type => non_neg_integer()
            , default => 5
            , cli_operand => "j-git"
            , anvl_resource => git
            }},
     ...

Then the user can adjust the capacity via CLI: anvl --j-git 10 ...

start_link(Resource)
-spec start_link(resource()) -> {ok, pid()}.
tab()

JavaScript license information