Skip to content

Instantly share code, notes, and snippets.

@kafaichoi
Created August 16, 2017 05:10
Show Gist options
  • Save kafaichoi/643c12f53c9195d8111c4714f3403874 to your computer and use it in GitHub Desktop.
Save kafaichoi/643c12f53c9195d8111c4714f3403874 to your computer and use it in GitHub Desktop.
Note on Elixir New OTP Abstraction

Process

Process is the concurrency primitive in ErlangVM To have State, we need server process which basically a recursive function call to keep it alive.

Agent

Simple wrapper around state. Keep state.

{:ok, agent} = Agent.start_link fn -> [] end
Agent.update(agent, fn list -> ["eggs" | list] end)
:ok
Agent.get(agent, fn list -> list end)
["eggs"]
Agent.stop(agent)
:ok

Task

async unit of computations. Tasks spawned with async can be awaited on by its caller above. (implemented y spawing a process that send a mesage to the caller once the given computation is done)

await (The Task process send a message back with the result) it use Process.monitor to get notified if the task process crash

No GenServer is used for task

GenServer

Generic Server

GenState

A specification and computational flow for Elixir. for us to define a pipeline of work to be carried out by independent steps (or stages) in a seprate process.

  • producer-consumer flow
  • exchanging events with bak-pressure between Elixir processes.
  • abstract away dispatching data and back-pressure. so we focus on producing, manipulated and cponsuming events a stage can be producer, consumer or both.
  • :producer source. producer wait for demand from consumers and respond with the requested events.
  • :producer_consumer both a source and a sink. it respond to demand from other consumers as well as request events from producers
  • :consumer a sink. request and receives data from producers.

Use Cases:

  • Data Transformation Pipieline. e.g produce events from db, Kafka and then we process, sort, catalog or store metrics

  • Work Queue

  • Event Processing

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment