View Source Wayfarer.Router (wayfarer v0.6.1)

Wayfarer's routing is implemented on top of an ETS table to allow for fast querying and easy mutation.

This module provides a standardised interface to interact with a routing table.

Summary

Types

The algorithm used to select which target to forward requests to (when there is more than one matching target).

The current health status of the target.

A fully qualified hostname optionally with a leading wildcard.

Host patterns are built by splitting hostnames into segments and storing them as tuples. Hosts with a leading wildcard (*) segment will have that segment replaced with a :_.

Uniquely identifies a listener.

The row format for each route in the table.

Uniquely identifies a request target, either a remote http(s) server or a local Plug.

Like target/0 except that it can contain user input for the address portion.

Functions

Add new route to the routing table.

Find healthy targets for a given listener and hostname.

Add a number of routes into the routing table.

Create a new, empty routing table.

Remove a listener from the routing table.

Remove a target from the routing table.

Change a target's health state.

Types

@type algorithm() :: Wayfarer.Target.Selector.algorithm()

The algorithm used to select which target to forward requests to (when there is more than one matching target).

@type health() :: :initial | :healthy | :unhealthy | :draining

The current health status of the target.

@type host_name() :: String.t()

A fully qualified hostname optionally with a leading wildcard.

@type host_pattern() :: tuple()

Host patterns are built by splitting hostnames into segments and storing them as tuples. Hosts with a leading wildcard (*) segment will have that segment replaced with a :_.

Examples

  • www.example.com becomes {"com", "example", "www"}
  • *.example.com becomes {"com", "example", :_}
@type listener() :: {scheme(), :inet.ip_address(), :socket.port_number()}

Uniquely identifies a listener.

@type route() :: {listener(), host_pattern(), target(), algorithm(), health()}

The row format for each route in the table.

@type scheme() :: :http | :https
@type target() ::
  {:http | :https | :ws | :wss, :inet.ip_address(), :socket.port_number(),
   :http1 | :http2 | :auto}
  | {:plug, module()}
  | {:plug, {module(), any()}}

Uniquely identifies a request target, either a remote http(s) server or a local Plug.

@type target_input() ::
  {:http | :https | :ws | :wss, Wayfarer.Utils.address_input(),
   :socket.port_number(), :http1 | :http2 | :auto}
  | {:plug, module()}
  | {:plug, {module(), any()}}

Like target/0 except that it can contain user input for the address portion.

Functions

Link to this function

add_route(table, listener, target, host_names, algorithm)

View Source
@spec add_route(:ets.tid(), listener(), target_input(), [host_name()], algorithm()) ::
  :ok | {:error, any()}

Add new route to the routing table.

A route will be added for each host name with it's health state set to :initial.

This should only ever be called by Wayfarer.Server directly.

Link to this function

find_healthy_targets(table, listener, hostname)

View Source
@spec find_healthy_targets(:ets.tid(), listener(), String.t()) ::
  {:ok, [{target(), algorithm()}]} | {:error, any()}

Find healthy targets for a given listener and hostname.

Link to this function

import_routes(table, routes)

View Source
@spec import_routes(:ets.tid(), [
  {listener(), target_input(), [host_name()], algorithm()}
]) :: :ok

Add a number of routes into the routing table.

@spec init(module()) :: {:ok, :ets.tid()} | {:error, any()}

Create a new, empty routing table.

This table is protected and owned by the calling process.

Link to this function

remove_listener(table, listener)

View Source
@spec remove_listener(:ets.tid(), listener()) :: :ok

Remove a listener from the routing table.

This should only ever be done by Wayfarer.Server after it has finished draining connections.

Link to this function

remove_target(table, target)

View Source
@spec remove_target(:ets.tid(), target()) :: :ok

Remove a target from the routing table.

This should only ever be done by Wayfarer.Server after it has finished draining connections.

Link to this function

update_target_health_status(table, arg, status)

View Source
@spec update_target_health_status(:ets.tid(), target(), health()) :: :ok

Change a target's health state.