node

package
v0.0.0-...-364fed7 Latest Latest
Warning

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

Go to latest
Published: Jul 11, 2021 License: Apache-2.0 Imports: 22 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrNoKey = errors.New("no key in context")

ErrNoKey is returned when a key is missing.

View Source
var ErrSelfRouting = errors.New("route to self")

Functions

func ExtractClientKey

func ExtractClientKey(ctx context.Context) (id.ID, error)

ExtractClientKey will extract the client key from ctx. Returns ErrNoKey when no key was found.

func WithClientKey

func WithClientKey(ctx context.Context, id id.ID) context.Context

WithClientKey injects the ID into the context to be used for request routing.

func WriteHTTPState

func WriteHTTPState(l log.Logger, w io.Writer, n *Node)

WriteHTTPState writes the state of n as HTTP to w.

Types

type Application

type Application interface {
	// PeersChanged is invoked when the set of peers changes.
	PeersChanged(ps []Peer)
}

Application represents the application using the cluster. Methods will be invoked by the node depending on the state of the cluster.

type Client

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

Client is a transparent gRPC client interface to the cluster. If a request context is missing a key from WithClientKey, requests will fail with InvalidArgument.

func NewClient

func NewClient(n *Node, opts ...ClientOption) *Client

NewClient creates a new server Client using the node for routing. If allowSelfRouting is false, requests will fail with ErrSelfRouting if a node would connect to itself.

func (*Client) Invoke

func (c *Client) Invoke(ctx context.Context, method string, args interface{}, reply interface{}, opts ...grpc.CallOption) error

Invoke makes a request against the cluster, routing the request to the appropriate node. ctx must have a ClientKey set (via WithClientKey) or the request will fail.

func (*Client) NewStream

func (c *Client) NewStream(ctx context.Context, desc *grpc.StreamDesc, method string, opts ...grpc.CallOption) (grpc.ClientStream, error)

NewStream makes a request against the cluster, routing the request to the appropriate node. ctx must have a ClientKey set (via WithClientKey) or the request will fail.

type ClientOption

type ClientOption func(c *Client)

ClientOption modifies a Client.

func WithAllowSelfRouting

func WithAllowSelfRouting(allow bool) ClientOption

WithAllowSelfRouting enables routing to self. Default is true.

func WithForwardHook

func WithForwardHook(hook func(Peer) (Peer, error)) ClientOption

WithForwardHook allows to hook into the forwarding functionality. Hooks may change the address of where data is sent or modify the message prior to sending.

type Config

type Config struct {
	// ID represents the server. Must be specified.
	ID id.ID

	// BroadcastAddr is the address to share with peers when joining.
	// Must bet set.
	BroadcastAddr string

	// Number of leaves to track. Will be divisible by 2. Defaults to 8 if
	// unset.
	NumLeaves int
	// Number of neighbors to track for locality. Defaults to 8 if unset.
	NumNeighbors int

	// Log will be used for logging messages.
	Log log.Logger
}

Config controls how a node is initialized.

type Node

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

Node is a node within a Croissant cluster.

func New

func New(cfg Config, app Application, dial ...grpc.DialOption) (*Node, error)

New creates a new Node and registers it against the given gRPC server. The provided set of DialOptions are used when communicating with a cluster peer.

func (*Node) Close

func (n *Node) Close() error

Close leaves the cluster.

func (*Node) Join

func (n *Node) Join(ctx context.Context, addrs []string) error

Join joins the cluster. Calling this more than once will attempt to re-join the cluster.

func (*Node) NextPeer

func (n *Node) NextPeer(key id.ID) (next Peer, self bool, err error)

NextPeer returns the next peer in the routing chain for a given key. self will be true if next is the node itself.

This allows applications to implement special routing methods; e.g., batch routing.

func (*Node) Register

func (n *Node) Register(s grpc.ServiceRegistrar)

Register registers the cluster API to gRPC. Must be called before Join, otherwise other nodes will be unable to connect to this node.

type Peer

type Peer struct {
	ID   id.ID
	Addr string
}

Peer is a peer in the cluster.

type Router

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

Router supplies a set of gRPC server interceptors that can route requests through the cluster. A Node must be set with SetNode for the Router to work. set with SetNode

func (*Router) SetNode

func (r *Router) SetNode(n *Node)

SetNode sets the node to be used for routing requests.

func (*Router) Stream

func (r *Router) Stream() grpc.StreamServerInterceptor

Stream returns grpc.StreamServerInterceptor.

func (*Router) Unary

func (r *Router) Unary() grpc.UnaryServerInterceptor

Unary returns a grpc.UnaryServerInterceptor.

Jump to

Keyboard shortcuts

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