controlplane

package
v0.0.0-...-358f329 Latest Latest
Warning

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

Go to latest
Published: Feb 23, 2017 License: MIT Imports: 22 Imported by: 2

README

Control plane

Responsible for knowing how to route Hailo traffic, with two features:

  1. Which region to route to ("app pinning" feature)
  2. Which backend to send traffic to (H1, H2, load shedding/throttling)

Example usage:

control := controlplane.New()

// and then for each request we can yield a router
router := control.Router(httpReq)

// and then use the router to efficiently route according to backend/region
region := router.Region()
rule := router.Route()

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func LoadLastGoodConfig

func LoadLastGoodConfig()

LoadLastGoodConfig loads the last known "good" config from file

Types

type Action

type Action int

Action represents some outcome that is attached to a route, telling us what we should do next

const (
	ActionProxyToH1 Action = 1
	ActionSendToH2  Action = 2
	ActionThrottle  Action = 3
	ActionDeprecate Action = 4
)

func (Action) String

func (a Action) String() string

type ControlPlane

type ControlPlane struct {
	// @TODO: Use atomic.Value in Go >=1.4
	tomb.Tomb
	// contains filtered or unexported fields
}

ControlPlane represents our config-based system for controlling Hailo traffic both in terms of routing of requests to regions (region "pinning") and also routing of traffic to H1 vs H2 backends

func New

func New() *ControlPlane

New initialises a control plane that loads via the config service

func (*ControlPlane) HobModes

func (cp *ControlPlane) HobModes() HobModes

func (*ControlPlane) HobRegions

func (cp *ControlPlane) HobRegions() HobRegions

HobRegions obtains the current hob to region mappings from the control plane

func (*ControlPlane) Regions

func (cp *ControlPlane) Regions() Regions

Regions obtains the current region config from the control plane

func (*ControlPlane) Router

func (cp *ControlPlane) Router(req *http.Request) Router

Router takes a request and prepares us for routing it to a backend and/or region

func (*ControlPlane) Rules

func (cp *ControlPlane) Rules() SortedRules

type Extractor

type Extractor interface {
	Hob() string               // Hob is the city code
	Value(name string) string  // Value is some POST or GET value
	Path() string              // Path is the pathname of the request
	SetHob(string)             // Set the hob on the request parameters
	Source() string            // Source is whether this came from "customer" or "driver" API - expecting return one of these two
	Host() string              // Host of request
	Header(name string) string // Header is some HTTP header
}

Extractor allows us to extract stuff about requests when matching

type HobModes

type HobModes map[string]string

HobModes maps HOBs to modes

func (HobModes) Find

func (hm HobModes) Find(hob string) string

Find locates the mode for a HOB, falling back to "default" if not found and returning "" if that isn't present

type HobRegions

type HobRegions map[string]string

HobRegions maps HOBs to primary regions

func (HobRegions) Find

func (hr HobRegions) Find(hob string) string

Find locates the region ID for a HOB, falling back to "default" if none found and returning "" if that isn't present

type Match

type Match struct {
	Hob        string  `json:"regulatoryArea,omitempty"` // Hob is our city code, worked out either from hostname or explicit city=FOO parameter (POST or GET)
	Path       string  `json:"path,omitempty"`           // Path is the pathname, like /v1/foo/bar
	Source     string  `json:"source,omitempty"`         // Source is either "customer" or "driver" or "", where we work this out from the hostname (contains customer or driver or not)
	Proportion float32 `json:"proportion,omitempty"`     // Proportion is a float from 0 to 1 that gives us sampling
	Sampler    Sampler `json:"sampler,omitempty"`        // Sampler tells us how to sample
}

Match represents some criteria to match an HTTP request against

type Payload

type Payload struct {
	Body       string            `json:"body,omitempty"`
	HttpStatus int               `json:"httpStatus,omitempty"`
	Headers    map[string]string `json:"headers,omitempty"`
}

Payload represents the information we want to use when serving throttled calls

type Region

type Region struct {
	Id       string          `json:"id,omitempty"`       // ID of this region, eg: us-east-1
	Status   string          `json:"status,omitempty"`   // Status is ONLINE or OFFLINE - not a bool to cope with possible future additions
	Failover []string        `json:"failover,omitempty"` // Failover regions (if this one down)
	Apps     map[string]Urls `json:"apps,omitempty"`     // Apps and their URL config for pinning
}

Region defines config for a single AWS region

func (*Region) IsOnline

func (r *Region) IsOnline() bool

IsOnline tells us if this region is configured to accept traffic

func (*Region) Urls

func (r *Region) Urls(app string) Urls

Urls returns URLs for a given app within a region

type Regions

type Regions map[string]*Region

Regions represents a map of all region definitions for app pinning purposes, indexed by ID

func (Regions) Validate

func (rs Regions) Validate() error

Validate checks that the regions list is valid

type Router

type Router interface {
	Route() *Rule
	GetHobMode() string
	SetHob(string)
	Region() (region *Region, version int64)
	CorrectHostname(rw http.ResponseWriter) (err error, isCorrect bool, urls Urls, version int64)
}

A Router routes a request to a backend (H1, H2, or throttle) according to the first matching rule (rules are sorted by specificity)

type Rule

type Rule struct {
	Match   *Match   `json:"match,omitempty"`
	Action  Action   `json:"action,omitempty"`
	Payload *Payload `json:"payload,omitempty"`
	Weight  int      `json:"weight,omitempty"`
}

Rule represents some matching criteria plus an action

func (*Rule) Id

func (r *Rule) Id() string

Id generats a deterministic unique ID for a rule

func (*Rule) Matches

func (r *Rule) Matches(ext Extractor) bool

Matches tests if a route matches a request, wrapped with an extractor

func (*Rule) Specificity

func (r *Rule) Specificity() int

Specificity returns a number that tells us how specific this rule is - in a similar vein to CSS. We will then check the most specific first.

type RuleRouter

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

func (*RuleRouter) CorrectHostname

func (r *RuleRouter) CorrectHostname(rw http.ResponseWriter) (err error, isCorrect bool, urls Urls, version int64)

CorrectHostname tests if the request hostname matches the expected hostname for the active region, and if not, returns the right one

func (*RuleRouter) GetHobMode

func (r *RuleRouter) GetHobMode() string

GetHobMode returns the mode

func (*RuleRouter) Region

func (r *RuleRouter) Region() (region *Region, version int64)

Region identifies the region, and its config, that we should be sending API requests to for a given HTTP request

func (*RuleRouter) Route

func (r *RuleRouter) Route() *Rule

Route routes a request to a backend (H1, H2 or throttle) according to the first matching rule (rules sorted by specificity)

func (*RuleRouter) SetHob

func (r *RuleRouter) SetHob(hob string)

type Rules

type Rules map[string]*Rule

Rules represents a set of unsorted rules, indexed by UID

func (Rules) Add

func (rs Rules) Add(r *Rule) Rules

Add a rule to a map of rules

func (Rules) Sort

func (rs Rules) Sort() SortedRules

Sort rules by specificity

type Sampler

type Sampler int

Sampler defines how we should sample requests for matching

const (
	// RandomSampler samples completely randomly
	RandomSampler Sampler = 0
	// CustomerSampler samples by "customer" POST or GET parameter
	CustomerSampler Sampler = 1
	// DriverSampler samples by "driver" POST or GET parameter
	DriverSampler Sampler = 2
	// DeviceSampler samples by "device" POST or GET parameter
	DeviceSampler Sampler = 3
	// SessionSampler samples by "api_token" or "session_id" POST or GET parameter
	SessionSampler Sampler = 4
)

func (Sampler) String

func (i Sampler) String() string

type SortedRules

type SortedRules []*Rule

SortedRules represents a set of rules, sorted by specificity

func (SortedRules) Len

func (s SortedRules) Len() int

Len is part of sort.Interface

func (SortedRules) Less

func (s SortedRules) Less(i, j int) bool

Less is part of sort.Interface

func (SortedRules) Swap

func (s SortedRules) Swap(i, j int)

Swap is part of sort.Interface

func (SortedRules) Validate

func (s SortedRules) Validate() error

Validate tests if the sorted rules are considered a valid set

type Urls

type Urls map[string]string

Urls defines URLs for named purposes, eg: "hms", "api"

Jump to

Keyboard shortcuts

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