+ All Categories

Erlang

Date post: 15-Jul-2015
Category:
Upload: aaron-spiegel
View: 99 times
Download: 4 times
Share this document with a friend
Popular Tags:
16
Let it Crash Based on Prolog Academic Bad for Text RabbitMQ Opscode Chef Not Node.JS Web-scale Riak Parallelism Funny Syntax Concurrent Actor Model Fault Tolerant Functional Hard to Learn Open Telecom Platform Lightweight Light Weight Processes Hot Code Loading Reactor Model CouchDB Whats App Message Passing Supervisors Distributed Elixir
Transcript
Page 1: Erlang

Let it Crash

Based on

Prolog

Academic

Bad for Text

RabbitMQOpscode Chef

Not Node.JS

Web-scale

Riak

Parallelism

Funny

Syntax

Concurrent

Actor

Model

Fault Tolerant

Functional

Hard to Learn

Open Telecom

Platform

Lightweight

Light Weight Processes

Hot Code Loading

Reactor

Model

CouchDB

Whats App

Message

PassingSupervisors

Distributed

Elixir

Page 2: Erlang

ERLANG DESIGN PRINCIPLES

Fault

TolerantFunctional

Distributed SupervisedImmutable

Transparent Concurrent

Page 3: Erlang

Language Primitives

Organized into single namespace of modules

Functions are the primary method of abstraction

Functions have a single arity, and functions of different arities

can share names

Pattern Matching with function headers and guards are primary

conditions

Tail-call optimized — so recurse at will (and please)

Dynamically typed with type annotations and static analysis

Basic types: Atom, Function, Integer, Float, Char, Binary, Tuple,

List, Map, PID, Port, Reference, Union

Page 4: Erlang

Basic Syntax-module(module_name).

-export([public_fun/1, public_fun/2, map/2]).

public_fun(Arg1) ->

io:format("Hello, ~w!~n", [Arg1]).

public_fun(Arg1, Arg2) when is_binary(Arg1) ->

io:format("~w ~w!~n", [Arg1, Arg2]);

public_fun(atom, _Arg2) ->

io:format("atom received, binary here: ~w~n", [<<"This is binary

text">>]).

map(List, Fun) ->

map(List, Fun, []).

map([], _Fun, Acc) ->

lists:reverse(Acc);

map([H|T], Fun, Acc) ->

map(T, Fun, [Fun(H)|Acc]).

Page 5: Erlang

TRADITIONAL SHARED MEMORY CONCURRENCY

Locks protect shared

memory from

corruption

Intrinsically sequential

Failure cases: DIY

Distribution: DIY

Multi-core: Good

luck

Page 6: Erlang

ACTOR MODEL CONCURRENCY

No shared memory

Data is shared by

message-passing

• Copies data

• Need not share

hardware

Break up sequential

code

Actor

Actor

Actor

Page 7: Erlang

Concurrency Features

ProcessesVery lightweight thread implemented in C/Assembly. Can

typically run 200X more than other typical LWPs.

Message PassingProcesses have (mostly) dedicated heap. Memory is

shared exclusively via messages.

Generic Servers Common structure for resilient processes

Links & Monitors Used to setup supervisor hierarchies between processes

Supervisors Common structure for monitoring processes

DistributionProcesses can be run on multiple nodes, and are spread

out explicitly

ApplicationsA set of Erlang modules, and common structure for running

processes.

Page 8: Erlang

ERLANG CONCURRENCY

Supervisor

Worker

Supervisor

Worker

Worker

Based on Erlang

Processes

Erlang messages are very

light weight

Supervisors can revive

processes as needed

Garbage collection done

by process

SMP schedules processes

across cores

Page 9: Erlang

ERLANG CONCURRENCY

Supervisor

Worker

Supervisor

Worker

Worker

LAN attached distribution

don’t require much change

Creates truly fault

tolerance systems

Linear scalability, so long

as there are enough

processes

Page 10: Erlang

ERLANG CONCURRENCYSo, what’s an Erlang

Process?

-module(echo_and_quit).

-export([init/0]).

init() ->

receive

{Pid, Msg} -> Pid ! Msg

end.

-module(echo_server).

-export([init/1]).

init(Count) ->

receive

{Pid, Msg} -> Pid ! Msg,

init(Count + 1)

end.

Page 11: Erlang

ERLANG CONCURRENCYSo, what’s an Erlang

Process?

-module(echo_and_quit).

-export([init/0]).

init() ->

receive

{Pid, Msg} -> Pid ! Msg

end.

-module(echo_server).

-export([init/1]).

init(Count) ->

receive

{Pid, Msg} -> Pid ! Msg,

init(Count + 1)

end.

Page 12: Erlang

supervisor Example-module(erl_sup).

-behaviour(supervisor).

%% API

-export([start_link/0]).

%% Supervisor callbacks

-export([init/1]).

%% Helper macro for declaring children of supervisor

-define(CHILD(I, Type), {I, {I, start_link, []}, permanent, 5000, Type, [I]}).

%% ===================================================================

%% API functions

%% ===================================================================

start_link() ->

supervisor:start_link({local, ?MODULE}, ?MODULE, []).

%% ===================================================================

%% Supervisor callbacks

%% ===================================================================

init([]) ->

{ok, { {one_for_one, 5, 10}, [?CHILD(erl_wrk, worker)]} }.

Page 13: Erlang

gen_server Example

-module(erl_server).

-behaviour(gen_server).

%% API

-export([ start_link/0 ]).

%% gen_server callbacks

-export([init/1, handle_call/3, handle_cast/2, handle_info/2,

terminate/2, code_change/3]).

-define(SERVER, ?MODULE).

-record(state, {}).

%%% API

start_link() ->

gen_server:start_link({local, ?SERVER}, ?MODULE, [], []).

%%% gen_server callbacks

init([]) ->

{ok, #state{}}.

handle_call(_Request, _From, State) ->

Page 14: Erlang

gen_server Example

%%% gen_server callbacks

init([]) ->

{ok, #state{}}.

handle_call(_Request, _From, State) ->

Reply = ok,

{reply, Reply, State}.

handle_cast(_Msg, State) ->

{noreply, State}.

handle_info(_Info, State) ->

{noreply, State}.

terminate(_Reason, _State) ->

ok.

code_change(_OldVsn, State, _Extra) ->

{ok, State}.

%%% Internal functions

Page 15: Erlang

LIVE CODING TIME

Page 16: Erlang

Learn More!

Learn you some Erlang Book (Free)

Erlang Language Site

Elixir Language Site

Mostly Erlang Podcast

Programming Elixir Book

Programming Erlang Book

Seven Languages in Seven Weeks Books

Talk to me!


Recommended