This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# $ elixir httpx.exs | |
# $ curl -v localhost:8080/test | |
defmodule HTTPX do | |
defmodule Request, | |
do: defstruct [:verb, :path, :query, :host, :headers] | |
defmodule Response, | |
do: defstruct [body: "", content_type: "text/plain", code: 200, headers: []] | |
defmodule Server do |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Parallel Checkout | |
# -------------------------------------------------------------- | |
# Example of performance gained by using a parallel checkout in an e-commerce store, | |
# | |
# to run 500 checkouts in series: time elixir checkout.exs 500 serial | |
# to run 500 checkouts in parallel: time elixir checkout.exs 500 parallel | |
# | |
# Typical E-commerce checkout flow uses a bunch of network bound tasks, that are generally | |
# computed synchronously. This wastes time and requires larger server clusters to handle peak times | |
# |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// the main app file | |
import express from "express"; | |
import loadDb from "./loadDb"; // dummy middleware to load db (sets request.db) | |
import authenticate from "./authentication"; // middleware for doing authentication | |
import permit from "./authorization"; // middleware for checking if user's role is permitted to make request | |
const app = express(), | |
api = express.Router(); | |
// first middleware will setup db connection |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Example of Elixir code consuming a Phoenix channel | |
# | |
# 1) Join a room | |
# 2) Send a message | |
# 3) Receive a message | |
# | |
# Add `socket` to your deps in `mix.exs` | |
# {:socket, "~> 0.3"} | |
# | |
# To run: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Stack do | |
use GenServer | |
@name __MODULE__ | |
def start_link, | |
do: GenServer.start_link(__MODULE__, [], name: @name) | |
def pop, | |
do: GenServer.call(@name, :pop) |
This is an example of adding redundancy and reliability to a storage system using Elixir.
The uptime of a system can be increased by storing the data redundantly across multiple nodes. If a node goes down, the entire system isn't effected, as long as one of the nodes remains up - the system will continue to function.
It's pretty easy to accomplish this with a GenServer, instead of using GenServer.call/3
, use GenServer.multi_call/4
. With multi_call
, a call is sent to each node in parallel. It's an easy way to implement redundant writes, similar to RAID1.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Line do | |
def slope({x1, y1}, {x2, y2}), | |
do: delta(y1, y2) / delta(x1, x2) | |
def intercept(point: {x, y}, slope: slope), | |
do: {0, y - (slope * x)} | |
def point(x: x, slope: slope, intercept: intercept), | |
do: {x, (slope * x) + intercept} |