sconf

package
v0.0.0-...-d286924 Latest Latest
Warning

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

Go to latest
Published: Mar 5, 2015 License: Apache-2.0 Imports: 23 Imported by: 0

Documentation

Index

Constants

View Source
const DefaultRouterQueueSize = 1 << 8

DefaultRouterQueueSize represents the number of events that the router can buffer before forcing the batch processing of events.

Variables

View Source
var DefaultComponentMonitor = &metric.Monitor{
	Name: "default",
}

DefaultComponentMonitor defines a default metric monitor that outputs to stdout.

View Source
var DefaultComponentReporter = &report.Reporter{
	Name: "default",
}

DefaultComponentReporter defines a default reporter that outputs to stdout.

View Source
var DefaultHTTPEndpointPath = "/v1/configs"

DefaultHTTPEndpointPath contains the default path prefix used by HTTPEndpoint.

View Source
var ErrCorruptedAOF = errors.New("CorruptedAOF")

ErrCorruptedAOF is the error returned by AOFConfigDB when a corrupted database is encountered.

Functions

func NewConfig

func NewConfig(name string) (interface{}, error)

NewConfig creates a new config object for the given config type name registered via the RegisterType function.

func RegisterClient

func RegisterClient(name string, factory ClientFactory)

RegisterClient registers a factory function with the given URL scheme. Once registered, Client objects can be created via the NewClient function.

func RegisterType

func RegisterType(name string, typ reflect.Type)

RegisterType associates the config type name with the given runtime reflected type. This is used to unmarshal config object based on the type name.

Types

type AOFConfigDB

type AOFConfigDB struct {
	Component

	// File indicates the file path where the AOF database should be
	// stored. Either File or AOF should be set prior to calling Init and can't
	// be changed afterwrads.
	File string

	// AOF indicates the file to use as the AOF database. Either File or AOF
	// must be set prior to calling Init and can't be changed afterwards.
	AOF *os.File
	// contains filtered or unexported fields
}

AOFConfigDB implements a configuration database as a append-only file. New entries are added to the database via the NewConfig and DeadConfig functions and the database can be read via the Load function.

Note that corruptions are detected and reported but will not stop the loading process.

\todo This struct is currently not go-routine safe.

func (*AOFConfigDB) Close

func (db *AOFConfigDB) Close() error

Close closes the database flushing any pending writes. Returns an error if the AOF could not be closed properly.

func (*AOFConfigDB) DeadConfig

func (db *AOFConfigDB) DeadConfig(tombstone *Tombstone)

DeadConfig adds the given config tombstone to the database.

func (*AOFConfigDB) Init

func (db *AOFConfigDB) Init()

Init initializes the object.

func (*AOFConfigDB) Load

func (db *AOFConfigDB) Load() (*Configs, error)

Load returns a copy of the database and a CorruptedAOF error if a corruption was detected while loading the database.

func (*AOFConfigDB) NewConfig

func (db *AOFConfigDB) NewConfig(config *Config)

NewConfig adds the given config to the database.

type Client

type Client interface {
	Handler
	PushConfigs(*Configs)
	PullConfigs() *Configs
}

Client represents a configuration client which can be used to communicate with a config endpoint. The client needs to be able to notify the endpoint via the NewConfig and DeadConfig options as well as perform basic syncing operations via the PushConfigs and PullConfigs functions.

func NewClient

func NewClient(rawURL string) (client Client, err error)

NewClient creates a new Client object from the given URL string or returns an error if the no ClientFacotry was registered for the URL's scheme.

func NewHTTPClient

func NewHTTPClient(rawURL string) (Client, error)

NewHTTPClient creates a new Client that can be used to communicate with an HTTP config endpoing located at the given URL.

func NewNullClient

func NewNullClient(URL string) (Client, error)

NewNullClient creates a new noop config client.

type ClientFactory

type ClientFactory func(URL string) (Client, error)

ClientFactory defines a function type used to create new Client objects from a given URL string. Factories should be registered with the RegisterClient function.

type Component

type Component struct {
	// Name contains the friendly name of the component.
	Name string
	// Reporter is used to publish events and errors with or without associated binary data. Will use DefaultComponentReporter if nil.
	Reporter *report.Reporter
	// Monitor is used to record metrics for this component. Will use DefaultComponentMonitor if nil.
	Monitor *metric.Monitor
}

Component defines a set of basic operations that are useful for most components.

func (*Component) Error

func (component *Component) Error(err error, data ...report.Data)

Error posts a report based on the supplied error and data to the attached reporter service.

func (*Component) Log

func (component *Component) Log(text string, data ...report.Data)

Log posts a report based on the supplied text and data to the attached reporter service.

func (*Component) RecordMetrics

func (component *Component) RecordMetrics(data interface{})

RecordMetrics posts a set of metrics to the attached monitor service.

type Config

type Config struct {
	Type    string      `json:"type"`
	ID      string      `json:"id"`
	Version uint64      `json:"ver"`
	Data    interface{} `json:"data,omitempty"`
}

Config associates configuration data to a configuration ID, type and version. Configs are assumed to be immutable by the configuration system.

func (*Config) String

func (config *Config) String() string

String returns a string representation of the config suitable for debugging.

func (*Config) Tombstone

func (config *Config) Tombstone() *Tombstone

Tombstone returns a Tombstone that will kill the config object.

func (*Config) UnmarshalJSON

func (config *Config) UnmarshalJSON(body []byte) (err error)

UnmarshalJSON deserializes the given json blob as a Config object. Makes use of the config type registry to deserialize the config object and returns an error if the type was not registered with the config type registry.

type ConfigDB

type ConfigDB interface {
	Handler
	Load() (*Configs, error)
	Close() error
}

ConfigDB defines a simple persistance layer for config files. Entries are added via the NewConfig and DeadConfig functions and the database can be loaded via the Load function.

type ConfigList

type ConfigList map[string]*TypeConfigList

ConfigList contains the ID to version mapping of all configs and tombstones grouped by typed.

type ConfigResult

type ConfigResult struct {

	// Config is non-nil if the config is active. At most Config or
	// Tombstone will be set.
	Config *Config `json:"live,omitempty"`

	// Tombstone is non-nil if the config was killed. At most Config or
	// tombstone will be set.
	Tombstone *Tombstone `json:"dead,omitempty"`
}

ConfigResult is a convenience struct used to return the result of a query for the config ID. Both Config and Tombstone can be nil in the case where the config ID was never seen.

type Configs

type Configs struct {

	// Types contains the set of configs and tombstones associated with a given
	// tombstones.
	Types map[string]*TypeConfigs
}

Configs maintains a set of configs and tombstones indexed by types. All configs and tombstones held by this container are assumed to be immutable.

func (*Configs) ConfigArray

func (configs *Configs) ConfigArray() (result []*Config)

ConfigArray returns an array of all the configs in this container.

func (*Configs) Copy

func (configs *Configs) Copy() (other *Configs)

Copy performs a deep copy of the object.

func (*Configs) DeadConfig

func (configs *Configs) DeadConfig(tombstone *Tombstone) (oldConfig *Config, isNew bool)

DeadConfig adds the config tombstones and returns a boolean to indicate whether the config is new. A tombstone is new if its version is superior or equal to an existing config of the same type and ID or is strictly greater then the version of an existing tombstone. If the tombstone is new and it killed an live config then the config being replaced is returned.

func (*Configs) Diff

func (configs *Configs) Diff(other *Configs) (newConfigs []*Config, deadConfigs []*Tombstone)

Diff returns the configs and tombstones that would be added if invoked by any of the mutating functions. This does not modify the object and the Data field is never looked at in the Config objects.

func (*Configs) Get

func (configs *Configs) Get(typ, ID string) (ConfigResult, bool)

Get returns the config or tombstone associated with the given type and ID and a bool indicating whether the ID is present in the container for the given type.

func (*Configs) Len

func (configs *Configs) Len() (size int)

Len returns the number of configs and tombstones for all types.

func (*Configs) List

func (configs *Configs) List() ConfigList

List returns the ID to version mapping of all configs and tombstones.

func (*Configs) Merge

func (configs *Configs) Merge(other *Configs) (newConfigs []*Config, deadConfigs []*Tombstone)

Merge invokes NewConfig on each config of other and invokes DeadConfig on each tombstone of other. This operation is commutative. Returns the list of configs that were added successfully by the calls to NewConfig and the list of configs that were replaced or killed by the calls to deadConfig. This function does not return the configs that were replaced by the calls to NewConfig.

func (*Configs) NewConfig

func (configs *Configs) NewConfig(config *Config) (oldConfig *Config, isNew bool)

NewConfig adds the config and returns a boolean to indicate whether the config is new. A config is new if its version is strictly superior to the version of an existing config or tombstone of the same type and ID. If the config is new and it replaces an existing config then the old config being replaced is returned.

func (*Configs) String

func (configs *Configs) String() string

String returns a string representation of the container suitable for debugging.

func (*Configs) TombstoneArray

func (configs *Configs) TombstoneArray() (result []*Tombstone)

TombstoneArray returns an array of all the tombstones in this container.

type Configurable

type Configurable interface {

	// Copy returns a copy of the object which will be used by subsequent calls
	// to NewConfig and DeadConfig to mutate the object without data-races.
	Copy() Configurable

	// NewConfig mutates the object's state to include the new configuration.
	NewConfig(newConfig *Config) error

	// DeadConfig mutates the object's state to remove the old configuration.
	DeadConfig(oldConfig *Config) error
}

Configurable represents an object that can be mutated via configuration events through a copy-on-write mechanism.

type ConfigurableHandler

type ConfigurableHandler interface {

	// RegisterState creates and registers the handler's state with the given
	// router.
	RegisterState(*Router)
}

ConfigurableHandler represents a handler that holds a Configurable state. This is used a convenience interface to chain calls to RegisterState.

type HTTPClient

type HTTPClient struct {
	Component

	// URL indicates the URL to forward the event to.
	URL string

	// HTTPClient can optionally be used to set the http.Client object used for
	// communication.
	HTTPClient *http.Client

	RESTClient *rest.Client
	// contains filtered or unexported fields
}

HTTPClient is used to handle config events over HTTP.

func (*HTTPClient) DeadConfig

func (client *HTTPClient) DeadConfig(tombstone *Tombstone)

DeadConfig sends a config tombstone to the config endpoint.

func (*HTTPClient) Init

func (client *HTTPClient) Init()

Init initializes the object.

func (*HTTPClient) NewConfig

func (client *HTTPClient) NewConfig(config *Config)

NewConfig sends a new config to the config endpoint.

func (*HTTPClient) PullConfigs

func (client *HTTPClient) PullConfigs() *Configs

PullConfigs retrieves the set of configs and tombstones from the config endpoint.

func (*HTTPClient) PushConfigs

func (client *HTTPClient) PushConfigs(configs *Configs)

PushConfigs sends the given set of configs and tombstones to the config endpoint.

type HTTPClientMetrics

type HTTPClientMetrics struct {

	// Request indicates that an HTTP request made to a config endpoint.
	Request bool

	// NewConfig indicates that a new config was sent.
	NewConfig bool

	// DeadConfig indicates that a config tombstone was sent.
	DeadConfig bool

	// PushConfigs indicates that a set of configs and tombstones was sent.
	PushConfigs bool

	// PullConfigs indicates that a request was made to retrieve a set of
	// configs and tombstones.
	PullConfigs bool

	// Error indicates the outcome of the request.
	Error rest.ErrorType

	Latency time.Duration
}

HTTPClientMetrics contains the result of an HTTP config event sent by an HTTPClient.

type HTTPEndpoint

type HTTPEndpoint struct {
	Name string

	// PathPrefix is the HTTP path used to reach this endpoint. Defaults to
	// DefaultHTTPEndpointPath.
	PathPrefix string

	// Router will be used to process config events received by this endpoint.
	Router *Router
	// contains filtered or unexported fields
}

HTTPEndpoint is an HTTP endpoint used to process various config related events. The endpoint uses a Router to access the list of existing configs and to push new configs or tombstones.

func (*HTTPEndpoint) DeadConfig

func (endpoint *HTTPEndpoint) DeadConfig(tombstone *Tombstone)

DeadConfig adds the given tombstone to the configs managed by this endpoint.

func (*HTTPEndpoint) GetConfig

func (endpoint *HTTPEndpoint) GetConfig(typ, ID string) (result ConfigResult, err error)

GetConfig returns the config associated by the given ID and type managed by this endpoint. Returns a 404 REST error if the config doesn't exist.

func (*HTTPEndpoint) Init

func (endpoint *HTTPEndpoint) Init()

func (*HTTPEndpoint) ListConfigs

func (endpoint *HTTPEndpoint) ListConfigs() ConfigList

ListConfigs returns a mappiong of config IDs to config version managed by this endpoint.

func (*HTTPEndpoint) NewConfig

func (endpoint *HTTPEndpoint) NewConfig(config *Config)

NewConfig adds the given config to the configs managed by this endpoint.

func (*HTTPEndpoint) PullConfigs

func (endpoint *HTTPEndpoint) PullConfigs() *Configs

PullConfigs returns all the configs and tombstones managed by this endpoint.

func (*HTTPEndpoint) PushConfigs

func (endpoint *HTTPEndpoint) PushConfigs(configs *Configs)

PushConfigs merges the given configs with the configs managed by the endpoint.

func (*HTTPEndpoint) RESTRoutes

func (endpoint *HTTPEndpoint) RESTRoutes() rest.Routes

RESTRoutes returns the REST routes for the config endpoint.

type Handler

type Handler interface {
	NewConfig(*Config)
	DeadConfig(*Tombstone)
}

Handler represents an object that is interested in receiving notifications of new configs or of killed configs.

type MemoryConfigDB

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

MemoryConfigDB defines an in-memory config database which is not persisted.

func (*MemoryConfigDB) DeadConfig

func (db *MemoryConfigDB) DeadConfig(tombstone *Tombstone)

DeadConfig adds the given config tombstone to the database.

func (*MemoryConfigDB) Load

func (db *MemoryConfigDB) Load() (state *Configs, err error)

Load returns a copy of the database.

func (*MemoryConfigDB) NewConfig

func (db *MemoryConfigDB) NewConfig(config *Config)

NewConfig adds the given config to the database.

type NullClient

type NullClient struct{}

NullClient is a noop config client.

func (*NullClient) DeadConfig

func (*NullClient) DeadConfig(*Tombstone)

DeadConfig does nothing.

func (*NullClient) NewConfig

func (*NullClient) NewConfig(*Config)

NewConfig does nothing.

func (*NullClient) PullConfigs

func (*NullClient) PullConfigs() (configs *Configs)

PullConfigs returns an nil Configs object.

func (*NullClient) PushConfigs

func (*NullClient) PushConfigs(*Configs)

PushConfigs does nothing.

type NullConfigDB

type NullConfigDB struct{}

NullConfigDB defines a noop configuration database.

func (*NullConfigDB) Close

func (db *NullConfigDB) Close() (err error)

Close does nothing.

func (*NullConfigDB) DeadConfig

func (db *NullConfigDB) DeadConfig(_ *Tombstone)

DeadConfig does nothing.

func (*NullConfigDB) Load

func (db *NullConfigDB) Load() (*Configs, error)

Load returns an empty config object.

func (*NullConfigDB) NewConfig

func (db *NullConfigDB) NewConfig(_ *Config)

NewConfig does nothing.

type Poller

type Poller struct {

	// Local indicates the Router object which will act as the config
	// container and notification handler while polling. Must be set before
	// calling Init and can't be modified afterwards.
	Local Client

	// URL indicates where the config endpoint can be reached. It is used to
	// create a new Client using the NewClient function. Either URL
	// or Client must be set before calling Init and can't be changed
	// afterwards.
	URL string

	// Remote is used to forward the config notifications to a config
	// endpoint. Either URL or Client must be set before calling Init and can't
	// be changed afterwards.
	Remote Client

	// Push indicates that the poller will push all of Router's configs into
	// the configuration endpoint. At least one of Push or Pull must be set
	// before calling Init and can't be changed afterwards.
	Push bool

	// Pull indicates that the poller will pull all the configs from the
	// configuration endpoint and push them into the Router accordingly. At
	// least one of Push or Pull must be set before calling Init and can't be
	// changed afterwards.
	Pull bool

	// Rate indicates the frequency at which the configuration endpoint should
	// be pulled. Defaults to once every hour.
	Rate time.Duration
	// contains filtered or unexported fields
}

Poller periodically polls a configuration endpoint via a Client to either push or pull a set configs into a Router.

func (*Poller) Init

func (poller *Poller) Init()

Init initializes the object.

func (*Poller) Start

func (poller *Poller) Start()

Start begins the periodic polling process. Polling is done in a background goroutine.

func (*Poller) Stop

func (poller *Poller) Stop()

Stop ends the periodic polling process and kills the background goroutine.

type Routable

type Routable interface {

	// AllowedConfigTypes returns the list of configuration types that should be
	// routed to the handler/object. Returning an empty list indicates that all
	// configuration types are allowed.
	AllowedConfigTypes() []string
}

Routable allows an object to indicate which type of configs it is interested in receiving.

type Router

type Router struct {
	Name string

	// Configs is used to initialize the list of configurations for this
	// router. States will be updated to include these configs but no handlers
	// will be invoked. Can be set during construction but can't be changed
	// afterwards. It is not updated as new configuration events are processed.
	Configs *Configs

	// States is used to initialize the list of configurable objects for this
	// router. Can be set during construction but can only be changed via the
	// RegisterState function. It is not updated as new configuration events are
	// processed.
	States map[string]Configurable

	// Handlers is the list of handlers that will be executed for each new
	// configuration events. Can be set during construction but can't be changed
	// afterwards.
	Handlers []Handler

	// QueueSize indicates the number of events that can be buffered before
	// forcing the batch processing of events.
	QueueSize int
	// contains filtered or unexported fields
}

Router routes configuration events to handlers and objects. If an object or a handler implements the Routable interface then only the configuration events for the desired types will be routed to that handler/object.

Configuration events are first merged into the internal Configs object and only new events are forwarded to the handlers and objects. All configuration event notifications are defered to the router's goroutine where all event processing takes place.

func (*Router) Close

func (router *Router) Close()

Close terminates the router's goroutine.

func (*Router) DeadConfig

func (router *Router) DeadConfig(tombstone *Tombstone)

DeadConfig pushes the given configuration tombstones into the router and generates the required events if the tombstone is new.

func (*Router) Init

func (router *Router) Init()

Init initializes the router. Note that calling this function explicitly is optional.

func (*Router) NewConfig

func (router *Router) NewConfig(config *Config)

NewConfig pushes a given configuration into the router and generates the required events if the configuration is new.

func (*Router) PullConfigs

func (router *Router) PullConfigs() *Configs

PullConfigs returns the current list of active configs managed by the router. The returned object should not be modified.

func (*Router) PushConfigs

func (router *Router) PushConfigs(configs *Configs)

PushConfigs adds a configs object to the router and generates the required events for all new configurations or tombstones.

func (*Router) Register

func (router *Router) Register(handler interface{})

Register is a convenience function which checks whether the given handler implements the ConfigurableHandler interface and calls RegisterState if it does.

func (*Router) RegisterState

func (router *Router) RegisterState(key string, state Configurable)

RegisterState registers the given Configurable object with the given key which must be unique. Once called, the object will start receiving configuration event. If the object implements the Routable interface then only the desired configuration types will be routed to the object.

func (*Router) State

func (router *Router) State() RouterState

State returns the current state of the router. The state is read atomically and is guaranteed to be consistent.

func (*Router) UnregisterState

func (router *Router) UnregisterState(key string)

UnregisterState removes the Configurable object associated with the given key.

type RouterState

type RouterState struct {

	// Configs contains the list of configs currently managed by the router.
	Configs *Configs

	// States contains the list of states currently managed by the router.
	States map[string]Configurable
}

RouterState holds the current consistent state of the router.

type Tombstone

type Tombstone struct {
	Type    string `json:"type"`
	ID      string `json:"id"`
	Version uint64 `json:"ver"`
}

Tombstone indicates that a config of the same ID and type was killed at a given version. Tombstones are assumed to be immutable by the configuration system.

func (*Tombstone) String

func (tombstone *Tombstone) String() string

String returns a string representation of a tombstone suitable for debugging.

type TypeConfigList

type TypeConfigList struct {
	Configs    map[string]uint64 `json:"live,omitempty"`
	Tombstones map[string]uint64 `json:"dead,omitempty"`
}

TypeConfigList contains the ID to version mapping of all configs and tombstones for a given type.

type TypeConfigs

type TypeConfigs struct {

	// Configs contains a mapping of config ID to configs. An ID present in this
	// map will not be present in Tombstones.
	Configs map[string]*Config

	// Tombstones contains a mapping of config ID to tombstones. An ID present
	// in this map will not be present in Configs.
	Tombstones map[string]*Tombstone
}

TypeConfigs container for configs and tombstone of a given type.

func (*TypeConfigs) Copy

func (configs *TypeConfigs) Copy() *TypeConfigs

Copy performs a deep copy of the container. Note that config and tombstones are assumed to be immutable so they will not be copied.

func (*TypeConfigs) DeadConfig

func (configs *TypeConfigs) DeadConfig(tombstone *Tombstone) (oldConfig *Config, isNew bool)

DeadConfig adds the config tombstones and returns a boolean to indicate whether the config is new. A tombstone is new if its version is superior or equal to an existing config of the same ID or is strictly greater then the version of an existing tombstone. If the tombstone is new and it killed an live config then the config being replaced is returned.

func (*TypeConfigs) Diff

func (configs *TypeConfigs) Diff(other *TypeConfigs) (newConfigs []*Config, deadConfigs []*Tombstone)

Diff returns the configs and tombstones that would be added if invoked by any of the mutating functions. This does not modify the object and the Data field is never looked at in the Config objects.

func (*TypeConfigs) Get

func (configs *TypeConfigs) Get(ID string) (ConfigResult, bool)

Get returns the config or tombstone associated with the given ID and a bool indicating whether the ID is present in the container.

func (*TypeConfigs) Len

func (configs *TypeConfigs) Len() (size int)

Len returns the number of configs and tombstones.

func (*TypeConfigs) List

func (configs *TypeConfigs) List() *TypeConfigList

List returns an ID to version mapping of the configs.

func (*TypeConfigs) MarshalJSON

func (configs *TypeConfigs) MarshalJSON() ([]byte, error)

MarshalJSON serializes the config object into a simpler json representation where the maps are replaced by arrays to avoid duplicating the IDs in the serialized object.

func (*TypeConfigs) Merge

func (configs *TypeConfigs) Merge(other *TypeConfigs) (newConfigs []*Config, deadConfigs []*Tombstone)

Merge invokes NewConfig on each config of other and invokes DeadConfig on each tombstone of other. This operation is commutative. Returns the list of configs that were added successfully by the calls to NewConfig and the list of configs that were replaced or killed by the calls to deadConfig. This function does not return the configs that were replaced by the calls to NewConfig.

func (*TypeConfigs) NewConfig

func (configs *TypeConfigs) NewConfig(config *Config) (oldConfig *Config, isNew bool)

NewConfig adds the config and returns a boolean to indicate whether the config is new. A config is new if its version is strictly superior to the version of an existing config or tombstone of the same ID. If the config is new and it replaces an existing config then the old config being replaced is returned.

func (*TypeConfigs) String

func (configs *TypeConfigs) String() string

String returns a string representation of the configs suitable for debugging.

func (*TypeConfigs) UnmarshalJSON

func (configs *TypeConfigs) UnmarshalJSON(body []byte) (err error)

UnmarshalJSON deserializes the config object from it's simpler json representation and builds the ID to object maps which are useful for in-memory accesses.

Jump to

Keyboard shortcuts

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