Documentation ¶
Overview ¶
Package scale provides utility functions for internal service and resource discovery, management and access.
Index ¶
- Constants
- Variables
- func DereferenceService(service string) (target string, node string, err error)
- func DeregisterService(instanceID string) error
- func GetOwnNodeName() (string, error)
- func InternalRPCConn(grpcPool *GRPCPool, service string) (conn *grpc.ClientConn, err error)
- func InternalRPCConnResource(grpcPool *GRPCPool, service string, resource string, ttl time.Duration) (conn *grpc.ClientConn, sessionID string, err error)
- func RegisterServiceLocal(service string, instanceID string, target string, ttl time.Duration) (err error)
- func ResourceReleased(service string, resource string) (released chan error)
- func ServiceKeepAlive(instanceID string) error
- func WaitResource(service string, resource string) error
- type GRPCPool
- type KeepAliveBuffer
- type Resource
- func ConstructResource(service string, resource string, timeout time.Duration) (res *Resource, success bool, err error)
- func DereferenceOrRegisterResource(service string, resource string, ttl time.Duration) (res *Resource, isNew bool, err error)
- func DereferenceResource(service string, resource string) (res *Resource, err error)
- func DereferenceResourceConsistent(service string, resource string) (res *Resource, err error)
- func ExistingResource(service string, resource string, sessionID string) (res *Resource)
- func RegisterResource(service string, resource string, ttl time.Duration) (res *Resource, success bool, err error)
- func RegisterResourceCustom(service string, resource string, target string, targetNode string, ...) (res *Resource, success bool, err error)
- func RegisterResourceLocal(resource string, ttl time.Duration) (res *Resource, success bool, err error)
- func (res *Resource) Deregister() error
- func (res *Resource) DoneConstructing(target string, targetNode string, ttl time.Duration) (err error)
- func (res *Resource) GetSessionID() string
- func (res *Resource) GetTarget() string
- func (res *Resource) GetTargetNode() string
- func (res *Resource) KeepAlive() error
- func (res *Resource) LoseLockCh() <-chan struct{}
- func (res *Resource) LostLock() bool
- type SelfKeepAlive
Constants ¶
const PackageName = "scale"
PackageName is the name of this package.
Variables ¶
var ( // DNSServerFlag is the DNS server address in format <ip>:<port>. Used for // service discovery. DNSServerFlag = config.DeclareString( PackageName, "dnsServer", "leverosconsul:8600") )
var ErrServiceNotFound = fmt.Errorf("Service not found")
ErrServiceNotFound is an error that is returned when a service with provided name is not found.
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 ¶
DereferenceService returns a random target and a node associated with given service name.
func DeregisterService ¶
DeregisterService deregisters a service from Consul.
func GetOwnNodeName ¶
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 ¶
ResourceReleased returns a channel which closes once the provided resource is released.
func ServiceKeepAlive ¶
ServiceKeepAlive maintains the TTL for a service.
func WaitResource ¶
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 ¶
NewGRPCPool returns a new GRPC connection 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 ¶
DereferenceResource populates the target fields for a resource that already exists.
func DereferenceResourceConsistent ¶
DereferenceResourceConsistent is like DereferenceResource, except it does consistent read.
func ExistingResource ¶
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 ¶
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 ¶
GetSessionID returns the resource's owning session.
func (*Resource) GetTargetNode ¶
GetTargetNode returns the resource's target node.
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.
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).