Fly.RPC (Fly RPC v0.3.0) View Source

Performs RPC calls to nodes in Fly.io regions.

Provides features to help Elixir applications more easily take advantage of the features that Fly.io provides.

Configuration

Assumes each node is running the Fly.RPC server in its supervision tree and exports FLY_REGION environment variable to identify the fly region.

Note: anonymous function support only works when the release is identical across all nodes. This can be ensured by including the FLY_IMAGE_REF as part of the node name in your rel/env.sh.eex file:

#!/bin/sh

export ERL_AFLAGS="-proto_dist inet6_tcp"
export RELEASE_DISTRIBUTION=name
export RELEASE_NODE="${FLY_APP_NAME}-${FLY_IMAGE_REF##*-}@${FLY_PRIVATE_IP}"

To run code on a specific region call rpc_region/4. A node found within the given region will be chosen at random. Raises if no nodes exist on the given region.

The special :primary region may be passed to run the rpc against the region identified by the PRIMARY_REGION environment variable.

Examples

> rpc_region("hkg", fn -> String.upcase("fly") end)
"FLY"

> rpc_region("hkg", {String, :upcase, ["fly"]})
"FLY"

> rpc_region(Fly.RPC.primary_region(), {String, :upcase, ["fly"]])
"FLY"

> rpc_region(:primary, {String, :upcase, ["fly"]})
"FLY"

Server

The GenServer's responsibility is just to monitor other nodes as they enter and leave the cluster. It maintains a list of nodes and the Fly.io region where they are deployed in an ETS table that other processes can use to find and initiate their own RPC calls to.

Link to this section Summary

Functions

Returns a specification to start this module under a supervisor.

Callback implementation for GenServer.init/1.

Return if the app instance is running in the primary region or not.

Executes a function on the remote node to determine if the RPC API support is available.

Return the configured current region.

Return the configured primary region.

Asks a node what Fly region it's running in.

Returns the Elixir OTP nodes registered the region. Reads from a local cache.

Executes the function on the remote node and waits for the response.

Execute the MFA on a node in the primary region.

Executes the MFA on an available node in the desired region.

Link to this section Functions

Returns a specification to start this module under a supervisor.

See Supervisor.

Link to this function

handle_continue(atom, state)

View Source

Callback implementation for GenServer.handle_continue/2.

Callback implementation for GenServer.init/1.

Return if the app instance is running in the primary region or not.

Executes a function on the remote node to determine if the RPC API support is available.

Support may not exist on the remote node in a "first roll out" scenario.

Return the configured current region.

Reads the FLY_REGION ENV setting available when deployed on the Fly.io platform. When running on a different platform, that ENV value will not be set. Setting the MY_REGION ENV value instructs the node how to identify what "region" it is in. If not set, it returns "local".

The value itself is not important. If the value matches the value for the PRIMARY_REGION then it behaves as though it is the primary.

Return the configured primary region.

Reads and requires an ENV setting for PRIMARY_REGION. If not set, it returns "local".

Asks a node what Fly region it's running in.

Returns :error if RPC is not supported on remote node.

Link to this function

region_nodes(tab \\ :fly_regions, region)

View Source

Returns the Elixir OTP nodes registered the region. Reads from a local cache.

Link to this function

rpc(node, func, timeout \\ 5000)

View Source

Executes the function on the remote node and waits for the response.

Exits after timeout milliseconds.

Link to this function

rpc_primary(func, opts \\ [])

View Source

Execute the MFA on a node in the primary region.

Link to this function

rpc_region(region, func, opts \\ [])

View Source

Executes the MFA on an available node in the desired region.

If the region is the "primary" region or the "local" region then execute the function immediately. Supports the string name of the region or :primary for the current configured primary region.

Otherwise find an available node and select one at random to execute the function.

Raises ArgumentError when no available nodes.

Example

> RPC.rpc_region("hkg", fn -> 1 + 2 end)
3

> RPC.rpc_region("hkg", {Kernel, :+, [1, 2]})
3

> RPC.rpc_region(:primary, {Kernel, :+, [1, 2]})
3