scale

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: May 12, 2016 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Overview

Package scale provides utility functions for internal service and resource discovery, management and access.

Index

Constants

View Source
const PackageName = "scale"

PackageName is the name of this package.

Variables

View Source
var (
	// DNSServerFlag is the DNS server address in format <ip>:<port>. Used for
	// service discovery.
	DNSServerFlag = config.DeclareString(
		PackageName, "dnsServer", "leverosconsul:8600")
)
View Source
var ErrServiceNotFound = fmt.Errorf("Service not found")

ErrServiceNotFound is an error that is returned when a service with provided name is not found.

View Source
var (
	// GRPCUnusedTimeoutFlag is the time a connection is dropped after it has
	// been last used.
	GRPCUnusedTimeoutFlag = config.DeclareDuration(
		PackageName, "grpcUnusedTimeout", 5*time.Minute)
)

Functions

func DereferenceService

func DereferenceService(
	service string) (target string, node string, err error)

DereferenceService returns a random target and a node associated with given service name.

func DeregisterService

func DeregisterService(instanceID string) error

DeregisterService deregisters a service from Consul.

func GetOwnNodeName

func GetOwnNodeName() (string, error)

GetOwnNodeName returns the node name of the current node.

func InternalRPCConn

func InternalRPCConn(
	grpcPool *GRPCPool, service string) (conn *grpc.ClientConn, err error)

InternalRPCConn creates a GRPC connection to an internal service.

func InternalRPCConnResource

func InternalRPCConnResource(
	grpcPool *GRPCPool, service string, resource string, ttl time.Duration) (
	conn *grpc.ClientConn, sessionID string, err error)

InternalRPCConnResource creates a GRPC connection to an internal resource. If the resource does dot exist, it is assigned atomically to an instance in the service.

func RegisterServiceLocal

func RegisterServiceLocal(
	service string, instanceID string, target string,
	ttl time.Duration) (err error)

RegisterServiceLocal registers the service as running on the current node.

func ResourceReleased

func ResourceReleased(
	service string, resource string) (released chan error)

ResourceReleased returns a channel which closes once the provided resource is released.

func ServiceKeepAlive

func ServiceKeepAlive(instanceID string) error

ServiceKeepAlive maintains the TTL for a service.

func WaitResource

func WaitResource(service string, resource string) error

WaitResource monitors a resource and blocks until that resource is released or there is some other error.

Types

type GRPCPool

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

GRPCPool represents a pool of GRPC client connections.

func NewGRPCPool

func NewGRPCPool() (*GRPCPool, error)

NewGRPCPool returns a new GRPC connection pool.

func (*GRPCPool) Dial

func (pool *GRPCPool) Dial(target string) (*grpc.ClientConn, error)

Dial returns a (potentially cached) GRPC connection associated with target.

func (*GRPCPool) KeepAlive

func (pool *GRPCPool) KeepAlive(target string)

KeepAlive resets the target's timer to prevent the connection from expiring and being closed down by the pool.

type KeepAliveBuffer

type KeepAliveBuffer struct {
	ID string
	// contains filtered or unexported fields
}

KeepAliveBuffer is an object that maintains a resource's TTL without overwhelming consul if calls to keep alive are very frequent. In case of frequent firing, the object absorbs most of the calls and ensures that actual keep alives are sent within maxWait apart.

func NewServiceKeepAliveBuffer

func NewServiceKeepAliveBuffer(
	instanceID string, maxWait time.Duration) *KeepAliveBuffer

NewServiceKeepAliveBuffer returns a new instance of KeepAliveBuffer for a service TTL.

func NewSessionKeepAliveBuffer

func NewSessionKeepAliveBuffer(
	sessionID string, maxWait time.Duration) *KeepAliveBuffer

NewSessionKeepAliveBuffer returns a new instance of KeepAliveBuffer for a session TTL.

func (*KeepAliveBuffer) Close

func (kab *KeepAliveBuffer) Close()

Close destroys the object so that keep alives are no longer sent.

func (*KeepAliveBuffer) KeepAlive

func (kab *KeepAliveBuffer) KeepAlive()

KeepAlive maintains the TTL of the resource.

type Resource

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

Resource represents a resource key in Consul. It can be used to associate instances of objects with owners in a distributed environment.

func ConstructResource

func ConstructResource(
	service string, resource string, timeout time.Duration) (
	res *Resource, success bool, err error)

ConstructResource begins constructing a resource. This operation blocks if resource is already being constructed. The success return value indicates whether the construction lock has been successfully acquired. If it is false (but no error), the resource has already been constructed. In this case, the target of the returned object is populated with the already constructed resource.

func DereferenceOrRegisterResource

func DereferenceOrRegisterResource(
	service string, resource string, ttl time.Duration) (
	res *Resource, isNew bool, err error)

DereferenceOrRegisterResource attempts to dereference a resource but registers as new if it fails. This is similar to ConstructResource+DoneConstructing except that it doesn't use a construction lock.

func DereferenceResource

func DereferenceResource(
	service string, resource string) (res *Resource, err error)

DereferenceResource populates the target fields for a resource that already exists.

func DereferenceResourceConsistent

func DereferenceResourceConsistent(
	service string, resource string) (res *Resource, err error)

DereferenceResourceConsistent is like DereferenceResource, except it does consistent read.

func ExistingResource

func ExistingResource(
	service string, resource string, sessionID string) (res *Resource)

ExistingResource can be used to operate on a resource that is already registered (perhaps in a different process).

func RegisterResource

func RegisterResource(
	service string, resource string, ttl time.Duration) (
	res *Resource, success bool, err error)

RegisterResource selects a random service instance as the target for a new resource. The operation fails if the resource already exists.

func RegisterResourceCustom

func RegisterResourceCustom(
	service string, resource string, target string,
	targetNode string, ttl time.Duration) (
	res *Resource, success bool, err error)

RegisterResourceCustom registers an arbitrary target for a new resource. The operation fails altogether if the resource already exists.

func RegisterResourceLocal

func RegisterResourceLocal(
	resource string, ttl time.Duration) (
	res *Resource, success bool, err error)

RegisterResourceLocal registers a resource being served by this process.

func (*Resource) Deregister

func (res *Resource) Deregister() error

Deregister removes the resource from Consul.

func (*Resource) DoneConstructing

func (res *Resource) DoneConstructing(
	target string, targetNode string, ttl time.Duration) (err error)

DoneConstructing can be used to release the construction lock and populate the resource's target.

func (*Resource) GetSessionID

func (res *Resource) GetSessionID() string

GetSessionID returns the resource's owning session.

func (*Resource) GetTarget

func (res *Resource) GetTarget() string

GetTarget returns the resource's target.

func (*Resource) GetTargetNode

func (res *Resource) GetTargetNode() string

GetTargetNode returns the resource's target node.

func (*Resource) KeepAlive

func (res *Resource) KeepAlive() error

KeepAlive maintains the resource's TTL to prevent expiry.

func (*Resource) LoseLockCh

func (res *Resource) LoseLockCh() <-chan struct{}

LoseLockCh returns the construction lose lock channel. This channel is closed if for any reason the construction lock is lost.

func (*Resource) LostLock

func (res *Resource) LostLock() bool

LostLock returns true if the construction lock was lost in the mean time.

type SelfKeepAlive

type SelfKeepAlive struct {
	ID string
	// contains filtered or unexported fields
}

SelfKeepAlive automatically maintains the TTL of a resource by sending regular KeepAlive's from a goroutine.

func NewServiceSelfKeepAlive

func NewServiceSelfKeepAlive(
	instanceID string, interval time.Duration) *SelfKeepAlive

NewServiceSelfKeepAlive returns a new instance of a SelfKeepAlive for a Consul service.

func NewSessionSelfKeepAlive

func NewSessionSelfKeepAlive(
	sessionID string, interval time.Duration) *SelfKeepAlive

NewSessionSelfKeepAlive returns a new instance of a SelfKeepAlive for a Consul session.

func (*SelfKeepAlive) Stop

func (ska *SelfKeepAlive) Stop()

Stop stops the SelfKeepAlive goroutine (and no longer sends keep alives).

Jump to

Keyboard shortcuts

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