Documentation ¶
Overview ¶
localaddr is a helper library for allocating local IP addresses.
localaddr does its best to ensure that the IP addresses allocated are both predictable (if possible) and on a subnet that isn't in use.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func UsableSubnet ¶
UsableSubnet returns a /24 CIDR block of usable network addresses in the RFC private address space that also isn't in use by any network interface on this machine currently.
Types ¶
type CachedDB ¶
type CachedDB struct { // DB is the underlying DB to use DB *DB // CachePath is the path to a file that will store the cached IP. CachePath string }
CachedDB wraps a DB and caches the result of Next() into a local file, reusing that for the duration that it exists (and automatically renewing it).
This is useful if you're consuming a single IP address and want a consistent IP address across runs.
type DB ¶
type DB struct { // Path is the path to the IP database. This file doesn't need to // exist but needs to be a writable path. The parent directory will // be made. Path string }
DB is a database of local addresses, and provides operations to find the next available address, release an address, etc.
DB will act as an LRU: if there are no available IP addresses, it will find the oldest IP address and give that to you. This is to combat the fact that the things that use IP addresses can often be killed outside of our control, and the oldest one is most likely to be stale. This should be an edge case.
The first time DB is used, it will find a usable subnet space and allocate that as its own. After it allocates that space, it will use that for the duration of this DBs existence. The usable subnet space is randomized to try to make it unlikely to have a collision.
DB uses a /24 so the entire space of available IP addresses is only 256, but these IPs are meant to be local, so they shouldn't overflow (it would mean more than 256 VMs are up... or that each of those VMs has a lot of network interfaces. Both cases are unlikely in Otto).
FUTURE TODO:
- Allocate additional subnets once we run out of IP space (vs. LRU)