overview.edoc 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. @author Ulf Wiger <ulf@wiger.net>
  2. @author Joseph Wayne Norton <norton@geminimobile.com>
  3. @doc Extended process dictionary
  4. [![Build Status](https://github.com/uwiger/gproc/actions/workflows/ci.yml/badge.svg?branch=master)](https://github.com/uwiger/gproc/actions/workflows/ci.yml)
  5. [![Hex pm](http://img.shields.io/hexpm/v/gproc.svg?style=flat)](https://hex.pm/packages/gproc)
  6. <h2>Note</h2>
  7. Gproc has two dependencies: `gen_leader' and `edown'. Since most people don't
  8. actively use either, they are no longer fetched by default.
  9. <ul>
  10. <li>To enable fetching of `gen_leader', export the OS environment variable
  11. `GPROC_DIST=true' (this can be done e.g. from a GNU Makefile)</li>
  12. <li>`edown' is fetched on-demand whenever `rebar get-deps doc' is called (which
  13. happens when you call `make doc')</li>
  14. </ul>
  15. <h2>Installation</h2>
  16. You can get `gproc' from the <a href="https://hex.pm/packages/gproc">Hex package manager</a>
  17. That means declaring dependency on `{gproc, "0.5.0"}' in your `rebar3'-based applications or `{:gproc, "~> 0.5.0"}' in your `mix' based applications.
  18. <h2>Introduction</h2>
  19. Gproc is a process dictionary for Erlang, which provides a number of useful features beyond what the built-in dictionary has:
  20. <ul>
  21. <li>Use any term as a process alias</li>
  22. <li>Register a process under several aliases</li>
  23. <li>Non-unique properties can be registered simultaneously by many processes</li>
  24. <li>QLC and match specification interface for efficient queries on the
  25. dictionary</li>
  26. <li>Await registration, let's you wait until a process registers itself</li>
  27. <li>Atomically give away registered names and properties to another process</li>
  28. <li>Counters, and aggregated counters, which automatically maintain the
  29. total of all counters with a given name</li>
  30. <li>Global registry, with all the above functions applied to a network of nodes</li>
  31. </ul>
  32. <h3>Use case: System inspection</h3>
  33. Gproc was designed to work as a central index for "process metadata", i.e.
  34. properties that describe the role and characteristics of each process. Having
  35. a single registry that is flexible enough to hold important types of property
  36. makes it easier to (a) find processes of a certain type, and (b) query and
  37. browse key data in a running system.
  38. <h3>Use case: Pub/Sub patterns</h3>
  39. An interesting application of gproc is building publish/subscribe patterns.
  40. Example:
  41. <pre lang="erlang">
  42. subscribe(EventType) ->
  43. %% Gproc notation: {p, l, Name} means {(p)roperty, (l)ocal, Name}
  44. gproc:reg({p, l, {?MODULE, EventType}}).
  45. notify(EventType, Msg) ->
  46. Key = {?MODULE, EventType},
  47. gproc:send({p, l, Key}, {self(), Key, Msg}).
  48. </pre>
  49. <h3>Use case: Environment handling</h3>
  50. Gproc provides a set of functions to read environment variables, possibly from
  51. alternative sources, and cache them for efficient lookup. Caching also provides
  52. a way to see which processes rely on certain configuration values, as well as
  53. which values they actually ended up using.
  54. See {@link gproc:get_env/4}, {@link gproc:get_set_env/4} and
  55. {@link gproc:set_env/5} for details.
  56. <h2>Testing</h2>
  57. Gproc has a QuickCheck test suite, covering a fairly large part of the local
  58. gproc functionality, although none of the global registry. It requires a
  59. commercial EQC license, but rebar is smart enough to detect whether EQC is
  60. available, and if it isn't, the code in gproc_eqc.erl will be "defined away".
  61. There is also an eunit suite, covering the basic operations for local and
  62. global gproc.
  63. <h2>Building Edoc</h2>
  64. By default, `./rebar doc` generates Github-flavored Markdown files.
  65. If you want to change this, remove the `edoc_opts' line from `rebar.config'.
  66. Gproc was first introduced at the ACM SIGPLAN Erlang Workshop in
  67. Freiburg 2007 (<a href="erlang07-wiger.pdf">Paper available here</a>).
  68. @end