Back to godoc.org

Package presence

v0.0.0 (8b5ae34)
Latest Go to latest
Published: Jun 15, 2020 | License: MIT | Module: github.com/dogmatiq/verity

Overview

Package presence implements mechanisms for observing which replicas within an application are present.

Index

Package Files

Constants

const (
	// DefaultMinPollInterval is the default minimum time between discovery
	// polls.
	DefaultMinPollInterval = 1 * time.Second

	// DefaultMaxPollInterval is the default maximum time between discovery
	// polls.
	DefaultMaxPollInterval = 15 * time.Second
)
const (
	// DefaultMinDisconnectDelay is the default minimum amount of time to wait
	// after a replica disconnects before triggering a poll.
	DefaultMinDisconnectDelay = 100 * time.Millisecond

	// DefaultMaxDisconnectDelay is the default maximum amount of time to wait
	// after a replica disconnects before triggering a poll.
	DefaultMaxDisconnectDelay = 300 * time.Millisecond
)
const DefaultDialTimeout = 10 * time.Second

DefaultDialTimeout is the default timeout to use when dialing a gRPC API address. It is used if the context passed to d.Dial() does not already have a deadline.

Variables

var RegisterServer = pb.RegisterAPIServer

RegisterServer registers a presence server with a gRPC server.

func AdvertiseAddresses

func AdvertiseAddresses(addr net.Addr) []string

AdvertiseAddresses computes the API addresses to advertise based on a listener's bound address.

The returned addresses are given in order of preference.

func ExtrapolateAddresses

func ExtrapolateAddresses(addresses []string) []string

ExtrapolateAddresses extrapolates a full list of addresses that should be tried in order to connect to a replica from its list of advertised addresses.

type Client

type Client struct {
	// Conn is the gRPC connection used to communicate with the remote replica.
	Conn *grpc.ClientConn

	// Self is this replica.
	Self *replica.Replica

	// Remote is the replica being connected to.
	Remote *replica.Replica
}

Client communicates with the presence API server of another replica.

func (*Client) MaintainPresence

func (c *Client) MaintainPresence(ctx context.Context) error

MaintainPresence keeps the remote replica informed of our presence.

func (*Client) VerifyIdentity

func (c *Client) VerifyIdentity(ctx context.Context) error

VerifyIdentity checks that the remote replica is actually the expected replica.

type Connector

type Connector struct {
	// Self is this replica.
	Self *replica.Replica

	// Remote is the replica being connected to.
	Remote *replica.Replica

	// Dialer is the API dialer used to establish the connection.
	Dialer *Dialer

	// Observer is the target for fact notifications about the connection state.
	Observer observer.Observer
}

Connector attempts to maintain an open connection to a remote replica's presence API.

If the connection fails, it is reopened after a delay based on the replica's heartbeat interval.

func (*Connector) Run

func (c *Connector) Run(ctx context.Context) error

Run attempts to maintain an open stream to the remote replica's Presence operation.

type Dialer

type Dialer struct {
	// Options is the set of gRPC options to pass onto the underlying gRPC
	// dialer.
	Options []grpc.DialOption
}

Dialer establishes gRPC connections to replicas.

func (*Dialer) Dial

func (d *Dialer) Dial(
	ctx context.Context,
	r *replica.Replica,
) (*grpc.ClientConn, string, error)

Dial opens a gRPC client connection to the given replica.

If successful it returns the address that succeeded.

type Discoverer

type Discoverer interface {
	// Discover returns a list of replicas of this application, excluding this one.
	Discover(context.Context) ([]*replica.Replica, error)
}

Discoverer is an interface for discovering replicas of this application.

type Launcher

type Launcher struct {
	// Runner is used to launch goroutines for each replica.
	Runner *task.Runner

	// Func is the function to run in each goroutine.
	Func func(context.Context, *replica.Replica)
	// contains filtered or unexported fields
}

Launcher is an observer that maintains a goroutine for each online replica.

func (*Launcher) Notify

func (l *Launcher) Notify(f fact.Fact)

Notify the observer of a fact.

type Poller

type Poller struct {
	// Discoverer is the discoverer used to locate replicas.
	Discoverer Discoverer

	// Observer is the observer to notify when replica presence changes.
	Observer observer.Observer

	// MinInterval is the minimum interval at which discovery attempts are
	// made. If it is zero, DefaultMinPollInterval is used.
	MinInterval time.Duration

	// MaxInterval is the maximum interval at which discovery attempts are made.
	// If it is zero, DefaultMaxPollInterval is used.
	MaxInterval time.Duration
	// contains filtered or unexported fields
}

Poller is a service that periodically performs replica discovery and notifies an observer of the replicas that are present.

func (*Poller) Run

func (p *Poller) Run(ctx context.Context) error

Run makes discovery requests at the configured interval until ctx is canceled or an error occurs.

func (*Poller) Trigger

func (p *Poller) Trigger()

Trigger triggers a poll as soon as possible.

Multiple calls to Trigger() that occur more frequently than the minimum poll interval will only result in a single poll.

type Server

type Server struct {
	// Self is the current replica.
	Self *replica.Replica

	// Observer is notified whenever a replica begins or ends a call to the
	// streaming Presence operation.
	Observer observer.Observer
}

Server is the implementation of pb.APIServer.

func (*Server) Identity

func (s *Server) Identity(
	context.Context,
	*pb.IdentityRequest,
) (*pb.IdentityResponse, error)

Identity returns information about the replica running this API.

func (*Server) Present

func (s *Server) Present(
	req *pb.PresentRequest,
	stream pb.API_PresentServer,
) error

Present is called when a replica comes online, and the stream held open until it goes offline.

type Trigger

type Trigger struct {
	// Runner is used to launch goroutines that polls after replicas go offline.
	Runner *task.Runner

	// Poll is a function that triggers a discovery poll.
	Poll func()

	// MinDisconnectDelay is the minimum amount of time to wait after a replica
	// disconnects before triggering a poll. If it is nil,
	// DefaultMinDisconnectDelay is used.
	MinDisconnectDelay time.Duration

	// MaxDisconnectDelay is the maximum amount of time to wait after a replica
	// disconnects before triggering a poll. If it is nil,
	// DefaultMaxDisconnectDelay is used.
	MaxDisconnectDelay time.Duration
	// contains filtered or unexported fields
}

Trigger is an observer that triggers discovery polls when replica connections are opened and closed.

func (*Trigger) Notify

func (t *Trigger) Notify(f fact.Fact)

Notify the observer of a fact.

Documentation was rendered with GOOS=linux and GOARCH=amd64.

Jump to identifier

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to identifier