server

package
v0.0.0-...-e4d2572 Latest Latest
Warning

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

Go to latest
Published: Nov 12, 2015 License: MIT Imports: 22 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DoClient

func DoClient(conf DOConfig) *godo.Client

func RemoveDroplet

func RemoveDroplet(conf Config, drop Droplet) error

RemoveDroplet will query DO to remove a droplet. The ID of the droplet is used to identify the droplet. If an error is returned the droplet most likely has not been removed.

Types

type Backend

type Backend interface {
	Transport() http.RoundTripper // Returns a transport for the backend
	ID() string                   // A string identifier of this specific backend
	Name() string                 // A name for this backend
	Host() string                 // Returns the hostname of the backend
	Healthy() bool                // Is the backend healthy?
	Statistics() *Stats           // Returns a copy of the latest statistics. Updated every second.
	Connections() int             // Return the current number of connections
	Close()                       // Close the backend (before shutdown/reload).
}

A Backend is a single running backend instance. It will monitor itself and update health and stats every second.

func NewDropletBackend

func NewDropletBackend(d Droplet, bec BackendConfig) Backend

NewDropletBackend returns a Backend configured with the Droplet information.

type BackendConfig

type BackendConfig struct {
	DialTimeout   Duration `toml:"dial-timeout"`            // Timeout for connecting to a backend.
	LatencyAvg    int      `toml:"latency-average-seconds"` // Measure latency over this many seconds
	HealthTimeout Duration `toml:"health-check-timeout"`    // Timeout for a health check. Should be less than 1 second.
	HostPort      int      `toml:"new-host-port"`           // Host port the proxy should connect to.
	HealthPath    string   `toml:"new-host-health-path"`    // Health path to use.
	HealthHTTPS   bool     `toml:"new-host-health-https"`   // Set to true if the health check on new backs is https.
	DisableHealth bool     `toml:"disable-health-check"`    // Disable health checks.
}

BackendConfig contains configuration for handling backends. This information is mainly used to instantiate and destroy backends on demand.

func (BackendConfig) Validate

func (c BackendConfig) Validate() error

Validate backend configuration. Will return the first error found. FIXME: Check remaining settings.

type Config

type Config struct {
	Bind          string          `toml:"bind"`
	Https         bool            `toml:"https"`
	CertFile      string          `toml:"tls-cert-file"`
	KeyFile       string          `toml:"tls-key-file"`
	AddForwarded  bool            `toml:"add-x-forwarded-for"`
	WatchConfig   bool            `toml:"watch-config"` // Watch the configuration file for changes
	LoadBalancing LBConfig        `toml:"loadbalancing"`
	InventoryFile string          `toml:"inventory-file"`
	Backend       BackendConfig   `toml:"backend"`
	Provision     ProvisionConfig `toml:"provisioning"`
	DO            DOConfig        `toml:"do-provisioner"`
}

Config contains the main server configuration This maps directly to the main config file.

func ReadConfigFile

func ReadConfigFile(file string) (*Config, error)

ReadConfigFile will open the file with the supplied name and return the configuration. The configuration is validated.

func (Config) Validate

func (c Config) Validate() error

Validate if settings in configuration are valid. The function will validate all subobjects as well. Will return an error with the first problem found.

type DOConfig

type DOConfig struct {
	Enable     bool   `toml:"enable"`
	HostPrefix string `toml:"hostname-prefix"`
	Region     string `toml:"region"`
	Size       string `toml:"size"`
	Image      string `toml:"image"`
	UserData   string `toml:"user-data"`
	Backups    bool   `toml:"backups"`
	Token      string `toml:"token"`
	SSHKeyID   []int  `toml:"ssh-key-ids"`
}

DigitalOcean provisioning config

func (DOConfig) Validate

func (c DOConfig) Validate() error

type Droplet

type Droplet struct {
	ID         int       `toml:"id"`
	Name       string    `toml:"name"`
	PublicIP   string    `toml:"public-ip"`
	PrivateIP  string    `toml:"private-ip"`
	ServerHost string    `toml:"server-host"`
	HealthURL  string    `toml:"health-url"`
	Started    time.Time `toml:"started-time"`
}

A Droplet as defined in the inventory file.

func CreateDroplet

func CreateDroplet(conf Config, name string) (*Droplet, error)

CreateDroplet will provision a new droplet as backend with the parameters given in the main configuration file. If no name is given, a random name with the configured prefix and 10 random characters will be generated.

func (Droplet) Delete

func (d Droplet) Delete(conf Config) error

Delete a running droplet

func (Droplet) Reboot

func (d Droplet) Reboot(conf Config) error

Reboot a running droplet. Will wait up to 100 seconds or until the operation has been confirmed before returning.

func (Droplet) String

func (d Droplet) String() string

func (*Droplet) ToBackend

func (d *Droplet) ToBackend(bec BackendConfig) (Backend, error)

type DropletBackend

type DropletBackend struct {
	Droplet Droplet
	// contains filtered or unexported fields
}

dropletBackend is a a backend instance with a DigitalOcean droplet behind it.

func (DropletBackend) Close

func (b DropletBackend) Close()

Close the backend, which will shut down monitoring of the backend.

func (DropletBackend) Connections

func (b DropletBackend) Connections() int

Connections returns the number of currently running requests. Does not include websocket connections.

func (DropletBackend) Healthy

func (b DropletBackend) Healthy() bool

Healthy returns the healthy state of the backend

func (DropletBackend) Host

func (b DropletBackend) Host() string

Host returns the host address of the backend.

func (*DropletBackend) ID

func (d *DropletBackend) ID() string

ID returns a unique ID of this backend

func (*DropletBackend) Name

func (d *DropletBackend) Name() string

ID returns a name of this backend

func (DropletBackend) Statistics

func (b DropletBackend) Statistics() *Stats

Healthy returns the healthy state of the backend

func (DropletBackend) Transport

func (b DropletBackend) Transport() http.RoundTripper

Transport returns a RoundTripper that will collect stats about the backend.

type Droplets

type Droplets struct {
	Droplets []Droplet `toml:"droplet"`
}

Droplets contains all backend droplets.

func ListDroplets

func ListDroplets(conf Config) (*Droplets, error)

ListDroplets list all droplets currently running.

func (Droplets) DropletID

func (d Droplets) DropletID(id int) (drop *Droplet, ok bool)

DropletID returns a Droplet with the specified ID.

type Duration

type Duration time.Duration

Duration is our own time.Duration that fulfills the toml.UnmarshalTOML interface.

func (Duration) String

func (d Duration) String() string

func (*Duration) UnmarshalTOML

func (d *Duration) UnmarshalTOML(data []byte) error

type ErrUnableToDelete

type ErrUnableToDelete struct {
	// contains filtered or unexported fields
}

func (ErrUnableToDelete) Error

func (e ErrUnableToDelete) Error() string

type Inventory

type Inventory struct {
	// contains filtered or unexported fields
}

Inventory contains all backends in your inventory. This is used by the load balancer to select a backend to send incoming requests to.

func NewInventory

func NewInventory(b []Backend, bec BackendConfig) *Inventory

NewInventory will a return a new Inventory with the supplied backends and config.

func ReadInventory

func ReadInventory(file string, bec BackendConfig) (*Inventory, error)

ReadInventory will read an inventory file and return the found items. TODO: Make sure Id is unique

func (*Inventory) AddBackend

func (i *Inventory) AddBackend(be Backend) error

AddBackend will add a backend to the inventory At the moment no checks are performed, but that could happen in the future.

func (*Inventory) BackendID

func (i *Inventory) BackendID(id string) (Backend, bool)

BackendID will return a backend with the specified ID, as well as a boolean indicating if it was found.

func (*Inventory) Close

func (i *Inventory) Close()

Close all backends associated with this inventory. This will stop all stats and monitoring of the backends.

func (*Inventory) IDs

func (i *Inventory) IDs() []string

IDs will return the IDs of all backends

func (*Inventory) Remove

func (i *Inventory) Remove(id string) error

Remove will remove a backend from the inventory If the backend cannot be found an error will be returned.

func (*Inventory) SaveDroplets

func (i *Inventory) SaveDroplets(file string) error

SaveDroplets will save all Doplets in the current inventory to a specified file. If the file exists it will be overwritten.

type LBConfig

type LBConfig struct {
	Type string `toml:"type"`
}

LBConfig contains settings for the load balancer.

func (LBConfig) Validate

func (c LBConfig) Validate() error

Validate if settings in the load balancer configuration are valid.

type LBStats

type LBStats struct {
	HealtyBackends   int
	UnhealtyBackends int
	AvgLatency       time.Duration
	Connections      int
}

type LoadBalancer

type LoadBalancer interface {
	// Return a single backend instance.
	// If none can be found nil will be returned.
	Backend() Backend

	// Close all backends and stop monitoring them
	Close()
}

A LoadBalancer is an interface for algorithms that implement various methods for returning a backend.

func NewLoadBalancer

func NewLoadBalancer(conf LBConfig, i *Inventory) (LoadBalancer, error)

NewLoadBalancer returns a new load balancer described by the supplied configuration and inventory.

type ProvisionConfig

type ProvisionConfig struct {
	Enable bool `toml:"enable"`

	// The minimum number of running backends.
	MinBackends int `toml:"min-backends"`
	// The maximum number of running backends.
	MaxBackends int `toml:"max-backends"`

	// If latency is below this, deprovision one server.
	DownscaleLatency Duration `toml:"downscale-latency"`
	// How long should the latency be below threshold before a server is deprovisioned.
	// This is an Exponentially Weighted Moving Average.
	DownscaleTime Duration `toml:"downscale-time"`
	// How long between a server can be deprovisioned.
	DownscaleEvery Duration `toml:"downscale-every"`

	// If the latency is above this, provision a new server.
	UpscaleLatency Duration `toml:"upscale-latency"`
	// How long should the latency be below threshold before a server is provisioned.
	// This is an Exponentially Weighted Moving Average.
	UpscaleTime Duration `toml:"upscale-time"`
	// How long between a new server can be provisioned.
	UpscaleEvery Duration `toml:"upscale-every"`

	// If a server fails this many health consequtive health checks, it will be deprovisioned.
	// Health checks is performed every second.
	MaxHealthFailures int `toml:"max-health-failures"`
}

ProvisionConfig contains configuration for starting and stopping backends. This information is mainly used to instantiate and destroy backends on demand.

func (ProvisionConfig) Validate

func (c ProvisionConfig) Validate() error

Validate provisioning configuration. Will return the first error found.

type Provisioner

type Provisioner interface {
	Add() error
	Remove() error
}

type ReverseProxy

type ReverseProxy struct {
	// contains filtered or unexported fields
}

func NewReverseProxy

func NewReverseProxy() *ReverseProxy

NewReverseProxy will create a new reverse proxy. You must set the backend and configuration before it is usable.

func NewReverseProxyConfig

func NewReverseProxyConfig(conf Config, lb LoadBalancer) *ReverseProxy

NewReverseProxyConfig will create a new reverse proxy with the supplied configuration and backend.

func (*ReverseProxy) GetBackend

func (h *ReverseProxy) GetBackend() Backend

GetBackend will return a backend from the current load balancer.

func (*ReverseProxy) GetConfig

func (h *ReverseProxy) GetConfig() Config

GetConfig will return a copy of the latest configuration.

func (*ReverseProxy) ServeHTTP

func (h *ReverseProxy) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP handles reverse proxying requests. The function should be able to keep serving request and keep them alive, even if the configuration changes. It is ok to keep using the configuration from when the request was initiated for the rest of the call.

func (*ReverseProxy) SetBackends

func (h *ReverseProxy) SetBackends(balancer LoadBalancer)

SetBackends will replace the current backends with the new ones. Requests currently being served will still go to the old backends, but new ones will go to a new one.

func (*ReverseProxy) SetConfig

func (h *ReverseProxy) SetConfig(conf Config)

Replace the configuration with another one.

type Server

type Server struct {
	Config Config
	// contains filtered or unexported fields
}

Server contains the main server configuration and server-wide information. Since there is no global data, it is possible to run multiple servers at once with different configurations.

func NewServer

func NewServer(config string) (*Server, error)

NewServer will read the supplied config file, and return a new server. A file watcher will be set up to monitor the configuration file and reload settings if changes are detected.

func (*Server) MonitorInventory

func (s *Server) MonitorInventory() error

MonitorInventory will monitor the inventory file and reload the inventory if changes are detected. The monitor can be shut down by sending a channel on (Server).exitMonInv. The monitor will exit and close the supplied channel.

func (*Server) ReadConfig

func (s *Server) ReadConfig(file string, init bool) error

ReadConfig will open the file with the supplied name and read the configuration from that. Use init, to initialize the configuration on startup, if you are reloading the configuration set it to false. If successful, the new config will be applied to the server.

func (*Server) Run

func (s *Server) Run()

Run the server.

func (*Server) UpdateConfig

func (s *Server) UpdateConfig(new Config) (err error)

UpdateConfig will check and apply a revised config. If the new config results in an error, the old config will remain.

type Stats

type Stats struct {
	Healthy     bool
	Latency     ewma.MovingAverage
	FailureRate ewma.MovingAverage
	// contains filtered or unexported fields
}

Stats contain regularly updated statistics about a backend. To access be sure to hold the 'mu' mutex.

Directories

Path Synopsis
Based on: https://gist.github.com/jarcoal/8940980 Modified so you don't need to register each individual URL, but register per method.
Based on: https://gist.github.com/jarcoal/8940980 Modified so you don't need to register each individual URL, but register per method.

Jump to

Keyboard shortcuts

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