overview.edoc 3.4 KB

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