service

package
v1.3.2 Latest Latest
Warning

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

Go to latest
Published: Sep 5, 2019 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Overview

Package service provides basic integration with go.serversets

Index

Constants

View Source
const (
	ErrorCount          = "sd_error_count"
	UpdateCount         = "sd_update_count"
	InstanceCount       = "sd_instance_count"
	LastErrorTimestamp  = "sd_last_error_timestamp"
	LastUpdateTimestamp = "sd_last_update_timestamp"

	ServiceLabel = "service"
)
View Source
const DefaultScheme = "https"

DefaultScheme is the default URI scheme assumed by this service discovery infrastructure

View Source
const DefaultVnodeCount = 211

Variables

View Source
var (
	// ErrIncomplete is returned by platform-specific code that creates environments
	// whenever the configuration is incomplete: it contains connection information
	// but no watches or registrations.
	ErrIncomplete = errors.New("No watches or registrations configured")
)

Functions

func FormatInstance

func FormatInstance(scheme, address string, port int) string

FormatInstance creates an instance string from a (scheme, address, port) tuple. If the port is the default for the scheme, it is not included. If scheme is empty, DefaultScheme is used. If the port is nonpositive, then it is ignored.

func Metrics

func Metrics() []xmetrics.Metric

Metrics is the service discovery module function for metrics

func NewAccessorEndpoint

func NewAccessorEndpoint(a Accessor) endpoint.Endpoint

NewAccessorEndpoint produces a go-kit Endpoint which delegates to an Accessor. The returned Endpoint expects a service Key as its request, and returns the instance string as the response.

func NewContextualInstancer

func NewContextualInstancer(i sd.Instancer, m map[string]interface{}) sd.Instancer

NewContextualInstancer returns an sd.Instancer that has been enriched with metadata. This metadata allows infrastructure to carry configuration information about the instancer across API boundaries so that it can be logged or otherwise processed.

If m is empty, i is returned as is.

func NopCloser

func NopCloser() error

NopCloser is a closer function that does nothing. It always returns a nil error. Useful for testing. Also used internally for the Environment's closer function in place of nil.

func NormalizeInstance

func NormalizeInstance(defaultScheme, instance string) (string, error)

NormalizeInstance canonicalizes an instance string from a service discovery backend, using an optional defaultScheme to be used if no scheme is found in the instance string. If defaultScheme is empty, DefaultScheme is assumed instead.

This function performs the following on the instance:

(1) If instance is a blank string, e.g. contains only whitespace or is empty, an empty string is returned with an error
(2) If the instance with whitespace trimmed off is not a valid instance, an error is returned with the trimmed instance string.
    This function is intentionally lenient on what is a valid instance string, e.g. "foobar.com", "foobar.com:8080", "asdf://foobar.com", etc
(3) If there was no scheme prepended on the instance, either defaultScheme (if non-empty) or the global DefaultScheme is used instead
(4) Finally, FormatInstance is called with the parsed scheme, address, and port.  Default ports for schemes will be omitted from the
    final string.

Types

type Accessor

type Accessor interface {
	// Get fetches the server node associated with a particular key.
	Get(key []byte) (string, error)
}

Accessor holds a hash of server nodes.

func DefaultAccessorFactory

func DefaultAccessorFactory(instances []string) Accessor

DefaultAccessorFactory is the default strategy for creating Accessors based on a set of instances. This default creates consistent hashing accessors.

func EmptyAccessor

func EmptyAccessor() Accessor

EmptyAccessor returns an Accessor that always returns an error from Get.

func GateAccessor

func GateAccessor(g gate.Interface, accessor Accessor) Accessor

type AccessorFactory

type AccessorFactory func([]string) Accessor

AccessorFactory defines the behavior of functions which can take a set of nodes and turn them into an Accessor.

func NewConsistentAccessorFactory

func NewConsistentAccessorFactory(vnodeCount int) AccessorFactory

NewConsistentAccessorFactory produces a factory which uses consistent hashing of server nodes. The returned factory does not modify instances passed to it. Instances are hashed as is.

If vnodeCount is nonpositive or equal to DefaultVnodeCount, the returned factory is the DefaultAccessorFactory.

func NewConsistentAccessorFactoryWithGate

func NewConsistentAccessorFactoryWithGate(vnodeCount int, g gate.Interface) AccessorFactory

NewConsistentAccessorFactoryWithGate produces a factory which uses consistent hashing of server nodes with the gate feature. The returned factory does not modify instances passed to it. Instances are hashed as is. If the gate is closed an error saying the gate is closed will be returned

type AccessorFunc

type AccessorFunc func([]byte) (string, error)

AccessorFunc is a function type that implements Accessor

func (AccessorFunc) Get

func (af AccessorFunc) Get(key []byte) (string, error)

type AccessorQueue

type AccessorQueue interface {
	Order([]string) []string
}

func DefaultOrder

func DefaultOrder() AccessorQueue

type AccessorValue

type AccessorValue struct {
	Accessor Accessor
	Err      error
}

type Environment

type Environment interface {
	sd.Registrar
	io.Closer

	// IsRegistered tests if the given instance is registered in this environment.  Useful for
	// determining if an arbitrary instance refers to this process.
	IsRegistered(string) bool

	// DefaultScheme is the default URI scheme to assume for discovered service instances.  This is
	// typically driven by configuration.
	DefaultScheme() string

	// Instancers returns a copy of the internal set of Instancers this environment is configured to watch.
	// Changing the returned Instancers will not result in changing this Environment's state.
	Instancers() Instancers

	// AccessorFactory returns the creation strategy for Accessors used in this environment.
	// Typically, this factory is set via configuration by some external source.
	AccessorFactory() AccessorFactory

	// Closed returns a channel that is closed when this Environment in closed.
	Closed() <-chan struct{}
}

Environment represents everything known about a service discovery backend. It also provide a central handle for tasks related to service discovery, such as Accessor hashing.

func NewEnvironment

func NewEnvironment(options ...Option) Environment

NewEnvironment constructs a new service discovery client environment. It is possible to construct an environment without any Registrars or Instancers, which essentially makes a no-op environment.

type ErrorChain

type ErrorChain struct {
	SubError error
	Err      error
}

func (ErrorChain) Empty

func (err ErrorChain) Empty() bool

func (ErrorChain) Error

func (err ErrorChain) Error() string

type Instancers

type Instancers map[string]sd.Instancer

Instancers is a collection of sd.Instancer objects, keyed by arbitrary strings.

func (Instancers) Copy

func (is Instancers) Copy() Instancers

func (Instancers) Get

func (is Instancers) Get(key string) (sd.Instancer, bool)

func (Instancers) Has

func (is Instancers) Has(key string) bool

func (Instancers) Len

func (is Instancers) Len() int

func (*Instancers) Set

func (is *Instancers) Set(key string, i sd.Instancer)

func (Instancers) Stop

func (is Instancers) Stop()

type Key

type Key interface {
	// Bytes returns the raw byte slice contents of the key.  This is what will be
	// passed to Accessor.Get.
	Bytes() []byte
}

Key represents a service key.

type KeyParser

type KeyParser func(string) (Key, error)

KeyParser is a parsing strategy that takes an arbitrary string and produces a service Key.

type LayeredAccessor

type LayeredAccessor interface {
	Accessor

	SetError(err error)
	SetPrimary(a Accessor)
	UpdatePrimary(a Accessor, err error)
	SetFailOver(failover map[string]AccessorValue)
	UpdateFailOver(key string, a Accessor, err error)
}

func NewLayeredAccesor

func NewLayeredAccesor(router RouteTraffic, chooser AccessorQueue) LayeredAccessor

type MapAccessor

type MapAccessor map[string]string

MapAccessor is a static Accessor that honors a set of known keys. Any other key will result in an error. Mostly useful for testing.

func (MapAccessor) Get

func (ma MapAccessor) Get(key []byte) (string, error)

type MockAccessor

type MockAccessor struct {
	mock.Mock
}

MockAccessor is a mocked Accessor

func (*MockAccessor) Get

func (m *MockAccessor) Get(v []byte) (string, error)

type MockEnvironment

type MockEnvironment struct {
	mock.Mock
}

MockEnvironment is a stretchr/testify mocked Environment

func (*MockEnvironment) AccessorFactory

func (m *MockEnvironment) AccessorFactory() AccessorFactory

func (*MockEnvironment) Close

func (m *MockEnvironment) Close() error

func (*MockEnvironment) Closed

func (m *MockEnvironment) Closed() <-chan struct{}

func (*MockEnvironment) DefaultScheme

func (m *MockEnvironment) DefaultScheme() string

func (*MockEnvironment) Deregister

func (m *MockEnvironment) Deregister()

func (*MockEnvironment) Instancers

func (m *MockEnvironment) Instancers() Instancers

func (*MockEnvironment) IsRegistered

func (m *MockEnvironment) IsRegistered(instance string) bool

func (*MockEnvironment) Register

func (m *MockEnvironment) Register()

type MockInstancer

type MockInstancer struct {
	mock.Mock
}

MockInstancer is a stretchr/testify mocked sd.Instancer

func (*MockInstancer) Deregister

func (m *MockInstancer) Deregister(events chan<- sd.Event)

func (*MockInstancer) Register

func (m *MockInstancer) Register(events chan<- sd.Event)

func (*MockInstancer) Stop

func (m *MockInstancer) Stop()

type MockRegistrar

type MockRegistrar struct {
	mock.Mock
}

MockRegistrar is a stretchr/testify mocked sd.Registrar

func (*MockRegistrar) Deregister

func (m *MockRegistrar) Deregister()

func (*MockRegistrar) Register

func (m *MockRegistrar) Register()

type Option

type Option func(*environment)

Option represents a service discovery option for configuring an Environment

func WithAccessorFactory

func WithAccessorFactory(af AccessorFactory) Option

WithAccessorFactory configures the creation strategy for Accessor objects. By default, DefaultAccessorFactory is used. Passing nil via this option sets (or resets) the environment back to using the DefaultAccessorFactory.

func WithCloser

func WithCloser(f func() error) Option

WithCloser configures the function used to completely shut down the service discover backend. By default, NopCloser is used. Passing a nil function for this option sets (or resets) the closer back to the NopCloser.

func WithDefaultScheme

func WithDefaultScheme(s string) Option

WithDefaultScheme configures the default URI scheme for discovered instances that do not specify a scheme. Some service discovery backends do not have a way to advertise a particular scheme that is revealed as part of the discovered instances.

func WithInstancers

func WithInstancers(i Instancers) Option

WithInstancers configures the set of sd.Instancer objects for use in the environment. The Instancers may be nil or empty for applications which have no need of monitoring discovered services.

func WithRegistrars

func WithRegistrars(r Registrars) Option

WithRegistrars configures the mapping of sd.Registrar objects to use for service advertisement.

type Registrars

type Registrars map[string]sd.Registrar

Registrars is a aggregate sd.Registrar that allows allows composite registration and deregistration. Keys in this map type will be service advertisements or instances, e.g. "host.com:8080" or "https://foobar.com"

func (*Registrars) Add

func (r *Registrars) Add(key string, v sd.Registrar)

func (Registrars) Deregister

func (r Registrars) Deregister()

func (Registrars) Has

func (r Registrars) Has(key string) bool

func (Registrars) Len

func (r Registrars) Len() int

func (Registrars) Register

func (r Registrars) Register()

type RouteError

type RouteError struct {
	ErrChain ErrorChain
	Instance string
}

func (RouteError) Error

func (err RouteError) Error() string

type RouteTraffic

type RouteTraffic interface {
	Route(instance string) error
}

func DefaultTrafficRouter

func DefaultTrafficRouter() RouteTraffic

type StringKey

type StringKey string

StringKey is a simple string that implements Key.

func (StringKey) Bytes

func (sk StringKey) Bytes() []byte

type UpdatableAccessor

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

UpdatableAccessor is an Accessor whose contents can be mutated safely under concurrency. The zero value of this struct is a valid Accessor initialized with no instances. Get will return an error until there is an update with at least (1) instance.

func (*UpdatableAccessor) Get

func (ua *UpdatableAccessor) Get(key []byte) (instance string, err error)

Get hashes the key against the current set of instances to select an instance consistently. This method will return an error if this instance isn't updated yet or has been updated with no instances.

func (*UpdatableAccessor) SetError

func (ua *UpdatableAccessor) SetError(err error)

SetError clears the instances being used by this instance and sets the error to be returned by Get with every call. This error will be returned by Get until an update with one or more instances occurs.

func (*UpdatableAccessor) SetInstances

func (ua *UpdatableAccessor) SetInstances(a Accessor)

SetInstances changes the instances used by this UpdateAccessor, clearing any error. Note that Get will still return an error if a is nil or empty.

func (*UpdatableAccessor) Update

func (ua *UpdatableAccessor) Update(a Accessor, err error)

Update sets both the instances and the Get error in a single, atomic call.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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