This is an small Erlang application demostrating some of the features of Erlang/OTP.
I commented the core modules heavily. So hopefully they can be read by those having no prior exposure to Erlang whatsoever.
The recommended order is below:
-
src/coordinator.erl
Implements the coordinator behavior that accepts abstract CPU-intensive jobs, deduplicates them when possible, computes job results limiting maximum parallelism.
-
src/demo_worker.erl
Instantiates the coordinator behavior telling it how to compute Fibonacci numbers (in a way that is deliberately very slow).
An instance of this process will be running on each Erlang node.
-
src/demo.erl
A wrapper module that distributes jobs to
demo_worker
processes an connected nodes using consistent hashing. -
src/demo_rest.erl
Optional (as I didn't leave any comments here). This module exposes the APIs exposed by
demo
via HTTP.
$ rebar3 compile
$ rebar3 release
$ ./start.sh --node [email protected] --cookie secret --port 8080
$ ./start.sh --node [email protected] --cookie secret --bootstrap [email protected] --port 8081
$ curl -s -XGET http://127.0.0.1:8080/stats | jq .
{
"[email protected]": {
"ok": 129,
"failed": 0,
"computed_ok": 88,
"computed_failed": 0,
"accepted": 135
}
}
$ curl -s -XDELETE http://127.0.0.1:8080/stats | jq .
"ok"
$ curl -s -XGET http://127.0.0.1:8080/fibonacci/42 | jq .
{
"result": 433494437,
"n": 42
}