comm

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Sep 25, 2018 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrKnownAboveQueryLimit indicates when a known peer is above a query limit.
	ErrKnownAboveQueryLimit = errors.New("known peer above query limit")

	// ErrUnknownAboveQueryLimit indicates when an unknown peer is above a query limit.
	ErrUnknownAboveQueryLimit = errors.New("unknown peer above query limit")

	// ErrKnownAbovePeerLimit indicates when a known peer is above a peer limit.
	ErrKnownAbovePeerLimit = errors.New("known peer above peer limit")

	// ErrUnknownAbovePeerLimit indicates when an unknown peer is above a peer limit.
	ErrUnknownAbovePeerLimit = errors.New("unknown peer above peer limit")

	// ErrUnauthorized indicates when a peer is not authorized to make requests against an
	// endpoint.
	ErrUnauthorized = errors.New("unauthorized")

	// Second defines a second time window for a Recorder.
	Second = time.Second

	// Day defines a day time window for a Recorder.
	Day = 24 * time.Hour

	// Week defines a week time window for a Recorder.
	Week = 7 * Day
)

Functions

func MaybeRecordRpErr

func MaybeRecordRpErr(r QueryRecorder, peerID id.ID, endpoint api.Endpoint, err error)

MaybeRecordRpErr records skips recording an error using th given QueryRecorder if the given error has a health error status code (indicating that the problem is on the client end).

func NewWindowQueryRecorderGetters

func NewWindowQueryRecorderGetters(
	knower Knower, windows []time.Duration,
) (QueryRecorder, WindowQueryGetters)

NewWindowQueryRecorderGetters returns a single QueryRecorder wrapper a set of individual QueryRecorderGetters, one for each time window. It also returns WindowQueryGetters containing QueryGetters for each time window.

Types

type Allower

type Allower interface {

	// Allow determines whether a peer should be allowed to make a request on a given
	// endpoint. It returns an error with a grpc status code if the peer is not allowed.
	Allow(peerID id.ID, endpoint api.Endpoint) error
}

Allower decides whether peers should be allowed to make requests.

func NewAllower

func NewAllower(auth Authorizer, limiters ...Limiter) Allower

NewAllower returns a new Allower using the given Authorizer and Limiters.

func NewDefaultAllower

func NewDefaultAllower(knower Knower, queryGetters WindowQueryGetters) Allower

NewDefaultAllower returns a new Allower using a

type Authorizations

type Authorizations map[api.Endpoint]map[bool]bool

Authorizations defines whether a peer is authorized on an endpoint based on whether it is known or not.

type Authorizer

type Authorizer interface {

	// Authorized determines whether a peer is authorized to make requests on a given endpoint.
	// It returns an error if the peer is not authorized.
	Authorized(peerID id.ID, endpoint api.Endpoint) error
}

Authorizer authorizes peers on endpoints.

func NewAlwaysAuthorizer

func NewAlwaysAuthorizer() Authorizer

NewAlwaysAuthorizer returns an Authorizer that always

func NewConfiguredAuthorizer

func NewConfiguredAuthorizer(auths Authorizations, knower Knower) Authorizer

NewConfiguredAuthorizer creates a new Authorizer from the given authorizations and using the given Knower.

type Doctor

type Doctor interface {

	// Healthy indicates whether the peer is currently deemed healthy.
	Healthy(peerID id.ID) bool
}

Doctor determines whether a peer is currently healthy.

func NewNaiveDoctor

func NewNaiveDoctor() Doctor

NewNaiveDoctor returns a Doctor that naively assumes all peers are healthy.

func NewResponseTimeDoctor added in v0.5.0

func NewResponseTimeDoctor(recorder QueryGetter) Doctor

NewResponseTimeDoctor returns a Doctor that assumes peers are health if their latest successful response is within a fixed window of the latest unsuccessful/errored response.

type Knower

type Knower interface {

	// Know returns whether a peer is known.
	Know(peerID id.ID) bool
}

Knower defines which peers are known, and thus usually more trustworthy, versus unknown.

func NewAlwaysKnower

func NewAlwaysKnower() Knower

NewAlwaysKnower returns a Knower than treats all peers as known.

type Limiter

type Limiter interface {

	// WithinLimit determines whether a given peer's request on a given endpoint is within the
	// configured limit. It returns nil if it is within the limit or an error if not.
	WithinLimit(peerID id.ID, endpoint api.Endpoint) error
}

Limiter determines whether requests from peers are with a set of rate limits.

func NewPeerLimiter

func NewPeerLimiter(limit Limits, knower Knower, qGetter QueryGetter) Limiter

NewPeerLimiter returns a new Limiter on the number of peers allowed to make requests for certain endpoints and known status.

func NewQueryLimiter

func NewQueryLimiter(limit Limits, knower Knower, qGetter QueryGetter) Limiter

NewQueryLimiter returns a new Limiter on the number of requests a peer can make for a given endpoint.

type Limits

type Limits map[api.Endpoint]map[bool]uint64

Limits defines a set of limits for endpoints and whether the peer is known or not.

type Outcome

type Outcome int

Outcome is the outcome of a query, for now just distinguishing between successes and failures.

const (
	// Success denotes a successful query.
	Success Outcome = iota

	// Error denotes a failed query.
	Error
)

func (Outcome) String

func (o Outcome) String() string

String returns a string representation of the outcome.

type Preferer

type Preferer interface {

	// Prefer indicates whether peer 1 should be preferred over peer 2 when prioritization
	// is necessary.
	Prefer(peerID1, peerID2 id.ID) bool
}

Preferer judges whether one peer is preferable over another.

func NewRpPreferer added in v0.5.0

func NewRpPreferer(getter QueryGetter) Preferer

NewRpPreferer returns a Preferer that prefers peers with a larger number of successful Verify or Find responses.

type PromRecorder

type PromRecorder interface {
	QueryRecorder

	// Register registers the Prometheus metric(s) with the default Prometheus registerer.
	Register()

	// Unregister unregisters the Prometheus metrics form the default Prometheus registerer.
	Unregister()
}

PromRecorder is a QueryRecorder that exposes state via Prometheus metrics.

func NewPromScalarRecorder

func NewPromScalarRecorder(selfID id.ID, inner QueryRecorder) PromRecorder

NewPromScalarRecorder creates a new scalar recorder that also emits Prometheus metrics for each (peer, endpoint, query, outcome).

type QueryGetter

type QueryGetter interface {

	// Get the query types outcomes on a particular endpoint for a peer.
	Get(peerID id.ID, endpoint api.Endpoint) QueryOutcomes

	// CountPeers returns the number of peers with queries on the given endpoint on the given
	// type.
	CountPeers(endpoint api.Endpoint, qt QueryType, known bool) int
}

QueryGetter gets metrics about each peer's endpoint query outcomes.

type QueryOutcomes

type QueryOutcomes map[QueryType]map[Outcome]*ScalarMetrics

QueryOutcomes contains the metrics for the 4 (query type, outcome) tuples.

type QueryRecorder

type QueryRecorder interface {

	// Record the outcome from a given query to/from a peer on the endpoint.
	Record(peerID id.ID, endpoint api.Endpoint, qt QueryType, o Outcome)
}

QueryRecorder records metrics about each peer's endpoint query outcomes.

type QueryRecorderGetter

type QueryRecorderGetter interface {
	QueryRecorder
	QueryGetter
}

QueryRecorderGetter both records and gets metrics about each peer's endpoint query outcomes.

func NewQueryRecorderGetter

func NewQueryRecorderGetter(knower Knower) QueryRecorderGetter

NewQueryRecorderGetter creates a new QueryRecorder that stores scalar metrics about each peer's endpoint query outcomes.

func NewWindowRecorderGetter

func NewWindowRecorderGetter(knower Knower, window time.Duration) QueryRecorderGetter

NewWindowRecorderGetter returns a WindowRecorder using the given Knower and window size.

type QueryType

type QueryType int

QueryType is a type of query, for now just distinguishing between requests and responses.

const (
	// Request denotes a request query from the peer.
	Request QueryType = iota

	// Response denotes a query response from the peer.
	Response
)

func (QueryType) String

func (qt QueryType) String() string

String returns a string representation of the query type.

type ScalarMetrics

type ScalarMetrics struct {

	// Earliest response time from the peer
	Earliest time.Time

	// Latest response time from the peer
	Latest time.Time

	// CountPeers of queries sent to the peer
	Count uint64
	// contains filtered or unexported fields
}

ScalarMetrics contains scalar metrics for a given query type and outcome.

func (*ScalarMetrics) Record

func (m *ScalarMetrics) Record()

Record updates the metrics to mark a query.

type WindowQueryGetters

type WindowQueryGetters map[time.Duration]QueryGetter

WindowQueryGetters contains a collection of QueryGetters, each defined over a specific time window.

type WindowQueryRecorders

type WindowQueryRecorders map[time.Duration]QueryRecorder

WindowQueryRecorders contains a collection of QueryRecorders, each defined over a specific time window.

func (WindowQueryRecorders) Record

func (rs WindowQueryRecorders) Record(
	peerID id.ID, endpoint api.Endpoint, qt QueryType, o Outcome,
)

Record the outcome from a given query to/from a peer on the endpoint.

Jump to

Keyboard shortcuts

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