service

package
v0.9.0-alpha Latest Latest
Warning

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

Go to latest
Published: Feb 1, 2017 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Overview

Package service provides basic integration with go.serversets

Index

Constants

View Source
const (
	DefaultScheme        = "http"
	DefaultHost          = "localhost"
	DefaultServer        = "localhost:2181"
	DefaultTimeout       = 5 * time.Second
	DefaultBaseDirectory = "/webpa"
	DefaultMemberPrefix  = "webpa_"
	DefaultEnvironment   = serversets.Local
	DefaultServiceName   = "test"
	DefaultVnodeCount    = 211
)
View Source
const (
	// DiscoveryKey is the default Viper subkey used for service discovery configuration.
	// WebPA servers should typically use this key as a standard.
	DiscoveryKey = "discovery"
)

Variables

This section is empty.

Functions

func Initialize

func Initialize(logger logging.Logger, pingFunc func() error, v *viper.Viper) (o *Options, rw RegistrarWatcher, err error)

Initialize is the top-level function for bootstrapping the service discovery infrastructure using a Viper instance. No watches are set by this function, but all registrations are made and monitored via the returned RegistrarWatcher.

func NewRedirectHandler

func NewRedirectHandler(accessor Accessor, code int, keyFunc func(*http.Request) ([]byte, error), logger logging.Logger) http.Handler

NewRedirectHandler produces an http.Handler which simply redirects all requests using the results of an Accessor hash. The supplied keyFunc is used to examine a request and return the []byte key.

func ParseHostPort

func ParseHostPort(value string) (baseURL string, err error)

ParseHostPort parses a value of the form returned by net.JoinHostPort and produces a base URL. If no scheme is present, this function prepends "http://" to the URL. All base URLs returned by this function are guaranteed to have a scheme, host, and port.

The go.serversets library returns endpoints in this format. This function is used to turn and endpoint into a valid base URL for a given service.

func ParseRegistration

func ParseRegistration(value string) (string, uint16, error)

ParseRegistration accepts a string containing host and port and an optional scheme and returns the host and port for registering as an endpoint.

Examples of registration strings include "http://something.comcast.net:8080" and "foobar.com". If no scheme is supplied, "http" is used. If no port is supplied, the default port for the scheme is used, if one is present. Unrecognized schemes are permitted.

func RegisterAll

func RegisterAll(registrar Registrar, o *Options) ([]*serversets.Endpoint, error)

RegisterAll registers all host:port strings found in o.Registrations.

func ReplaceHostPort

func ReplaceHostPort(hostPort string, originalURL *url.URL) string

ReplaceHostPort accepts a hostPort value of the form produced by ParseHostPort and returns a URL with the scheme, host, and port replaced in the original URL. The original URL's path, query, and fragment are preserved.

This function is primarily useful when using a string returned from Accessor.Get to redirect to or dispatch to a hashed service node.

func Subscribe

func Subscribe(logger logging.Logger, watch Watch, subscription func([]string)) func()

Subscribe consumes watch events and invokes a subscription function with the endpoints. The returned function can be called to cancel the subscription. This returned cancellation function is idempotent.

Types

type Accessor

type Accessor interface {
	Get([]byte) (string, error)
}

Accessor provides access to services based around []byte keys. *consistentHash.ConsistentHash implements this interface.

type AccessorFactory

type AccessorFactory interface {
	// New creates an Accessor using a slice of endpoints.  Each endpoint must
	// be of the form parseable by ParseHostPort.  Invalid endpoints are skipped
	// with an error log message.  The returned slice of strings is the sorted
	// list of base URLs added to the Accessor.
	New([]string) (Accessor, []string)
}

AccessorFactory is a Factory Interface for creating service Accessors.

func NewAccessorFactory

func NewAccessorFactory(o *Options) AccessorFactory

NewAccessorFactory uses a set of Options to produce an AccessorFactory

type AccessorSubscription

type AccessorSubscription interface {
	Accessor

	// Cancel removes this subscription from the underlying infrastructure.  No further
	// updates will occur, but this subscription's state will still be usable.
	// This method is idempotent.
	Cancel()
}

AccessorSubscription represents an Accessor whose state changes as the result of events via a subscription.

func NewAccessorSubscription

func NewAccessorSubscription(watch Watch, factory AccessorFactory, o *Options) AccessorSubscription

NewAccessorSubscription subscribes to a watch and updates an atomic Accessor in response to updated service endpoints. The returned object is fully initialized and can be used to access endpoints immediately. In addition, the subscription can be cancelled at any time. If the underlying service discovery infrastructure is shutdown, the subscription will no longer receive updates but can continue to be used in its stale state.

type Options

type Options struct {
	// Logger is used by any component configured via this Options.  If unset, a default
	// logger is used.
	Logger logging.Logger `json:"-"`

	// Connection is the comma-delimited Zookeeper connection string.  Both this and
	// Servers may be set, and they will be merged together when connecting to Zookeeper.
	Connection string `json:"connection,omitempty"`

	// Servers is the array of Zookeeper servers.  Both this and Connection may be set,
	// and they will be merged together when connecting to Zookeeper.
	Servers []string `json:"servers,omitempty"`

	// Timeout is the Zookeeper connection timeout.
	Timeout time.Duration `json:"timeout"`

	// BaseDirectory is the base path for all znodes created via this Options.
	BaseDirectory string `json:"baseDirectory,omitempty"`

	// MemberPrefix is the prefix for ephemeral nodes regstered via this Options.
	MemberPrefix string `json:"memberPrefix,omitempty"`

	// Environment is the environment component of the ephemeral znode path.
	Environment string `json:"environment,omitempty"`

	// ServiceName is the name of the service being registered.
	ServiceName string `json:"serviceName,omitempty"`

	// Registrations holds the slice of information used to register endpoints.  Typically,
	// this slice will either (1) be empty for an application that only watches for changes,  or (2) have the single
	// Registration indicating how this service is known.  Multiple registrations, essentially
	// being aliases for the same application, are supported.
	Registrations []string `json:"registrations,omitempty"`

	// VnodeCount is used to tune the underlying consistent hash algorithm for servers.
	VnodeCount uint `json:"vnodeCount"`

	// PingFunc is the callback function used to determine if this application is still able
	// to respond to requests.  This can be nil, and there is no default.
	PingFunc func() error `json:"-"`
}

Options represents the set of configurable attributes for service discovery and registration

func NewOptions

func NewOptions(logger logging.Logger, pingFunc func() error, v *viper.Viper) (o *Options, err error)

NewOptions produces an Options from a Viper instance. Typically, the Viper instance will be configured via the server package.

Since service discovery is an optional module for a WebPA server, this function allows the supplied Viper to be nil or otherwise uninitialized. Client code that opts in to service discovery can thus use the same codepath to configure an Options instance.

type Registrar

type Registrar interface {
	RegisterEndpoint(string, int, func() error) (*serversets.Endpoint, error)
}

Registrar is the interface which is used to register endpoints. *serversets.ServerSet implements this interface.

type RegistrarWatcher

type RegistrarWatcher interface {
	Registrar
	Watcher
}

RegistrarWatcher is simply the union of the serversets interfaces in this package.

func NewRegistrarWatcher

func NewRegistrarWatcher(o *Options) RegistrarWatcher

NewRegistrarWatcher produces a serversets.ServerSet using a supplied set of options. Because of limitations with the underlying go.serversets library, this function should be called exactly once for any given process.

type Watch

type Watch interface {
	IsClosed() bool
	Event() <-chan struct{}
	Endpoints() []string
}

Watch is the subset of methods required by this package that *serversets.Watch implements

type Watcher

type Watcher interface {
	Watch() (*serversets.Watch, error)
}

Watcher is the interface used to observe changes to the set of endpoints. *serversets.ServerSet implements this interface.

Directories

Path Synopsis
cmd
endpoint command

Jump to

Keyboard shortcuts

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