elixir-meetup-0217-genstage



elixir-meetup-0217-genstage

0 0


elixir-meetup-0217-genstage

Slides from my Erlang & Elixir SF meetup on supervised GenStage applications.

On Github sillypog / elixir-meetup-0217-genstage

Supervised GenStage Applications

Erlang & Elixir SF - February 22nd 2017

About Me

  • Peter Hastie
  •     @sillypog
  • Bleacher Report since 2013
    • Frontend
    • Data engineering
    • Consumer services
  • Elixir since 2016

Genstage

  • Added in 2016
  • Elixir specific OTP behaviour
  • Exchange events with back-pressure between Elixir processes

http://elixir-lang.org/blog/2016/07/14/announcing-genstage/

Synchronous
Asynchronous
Enum
Lazy Evaluation
Stream
Concurrent Evaluation
GenStage

Processes and back‑pressure

image/svg+xmlProducerConsumerMailbox

Without back‑pressure

Processes and back‑pressure

image/svg+xmlProducerConsumerMailboxDemand

With back‑pressure

Example Pipeline

Producer
Generates numbers
Producer / Consumer
Doubles numbers
Consumer
Prints numbers
Consumer
Prints numbers
Consumer
Prints numbers

Increasing Capacity

  
{:ok, a} = GenStage.start_link(A, 0)     # starting from zero
{:ok, b} = GenStage.start_link(B, 2)     # multiply by 2

{:ok, c1} = GenStage.start_link(C, 1000) # sleep for a second
{:ok, c2} = GenStage.start_link(C, 1000) # sleep for a second
{:ok, c3} = GenStage.start_link(C, 1000) # sleep for a second
{:ok, c4} = GenStage.start_link(C, 1000) # sleep for a second

GenStage.sync_subscribe(c1, to: b)
GenStage.sync_subscribe(c2, to: b)
GenStage.sync_subscribe(c3, to: b)
GenStage.sync_subscribe(c4, to: b)
GenStage.sync_subscribe(b, to: a)

# Sleep so we see events printed.
Process.sleep(:infinity)
  

...without increasing code

Erlang & Elixir Factory SF Bay Area 2017

23rd - 24th March

http://www.erlang-factory.com/sfbay2017

https://github.com/sillypog/supervised-genstage-app

Thank you!

Supervised GenStage Applications Erlang & Elixir SF - February 22nd 2017