localaddr

package
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 6, 2016 License: MPL-2.0 Imports: 15 Imported by: 0

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

func UsableSubnet() (*net.IPNet, error)

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.

func (*CachedDB) IP

func (db *CachedDB) IP() (net.IP, error)

IP retrieves the IP address.

If it is cached, it will renew and use that address. If it isn't cached, then it will grab a new IP, cache that, and use that.

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)

func (*DB) Next

func (this *DB) Next() (net.IP, error)

Next returns the next IP that is not allocated.

func (*DB) Release

func (this *DB) Release(ip net.IP) error

Release releases the given IP, removing it from the database.

func (*DB) Renew

func (this *DB) Renew(ip net.IP) error

Renew updates the last used time of the given IP to right now.

This should be called whenever a DB-given IP is used to make sure it isn't chosen as the LRU if we run out of IPs.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL