selection

package
v1.0.0-rc.3 Latest Latest
Warning

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

Go to latest
Published: Feb 4, 2023 License: Apache-2.0, BSD-2-Clause Imports: 22 Imported by: 7

Documentation

Index

Constants

View Source
const (
	DefaultInboundNeighborSize        = 4
	DefaultOutboundNeighborSize       = 4
	DefaultSaltLifetime               = 2 * time.Hour
	DefaultOutboundUpdateInterval     = 1 * time.Second
	DefaultFullOutboundUpdateInterval = 1 * time.Minute
)

Default values for the global parameters.

Variables

This section is empty.

Functions

func SetParameters

func SetParameters(param Parameters)

SetParameters sets the global parameters for this package. This function cannot be used concurrently.

Types

type DiscoverProtocol

type DiscoverProtocol interface {
	IsVerified(identity.ID, net.IP) bool
	EnsureVerified(*peer.Peer) error

	GetVerifiedPeer(identity.ID) *peer.Peer
	GetVerifiedPeers() []*peer.Peer
}

DiscoverProtocol specifies the methods from the peer discovery that are required.

type DroppedEvent

type DroppedEvent struct {
	Peer      *peer.Peer
	DroppedID identity.ID // ID of the peer that gets dropped.
}

DroppedEvent bundles the information sent in Dropped events.

type Events

type Events struct {
	// A SaltUpdated event is triggered, when the private and public salt were updated.
	SaltUpdated *event.Event[*SaltUpdatedEvent]
	// An OutgoingPeering event is triggered, when a valid response of PeeringRequest has been received.
	OutgoingPeering *event.Event[*PeeringEvent]
	// An IncomingPeering event is triggered, when a valid PeerRequest has been received.
	IncomingPeering *event.Event[*PeeringEvent]
	// A Dropped event is triggered, when a neighbor is dropped or when a drop message is received.
	Dropped *event.Event[*DroppedEvent]
}

Events contains all the events that are triggered during the neighbor selection.

type Filter

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

func NewFilter

func NewFilter() *Filter

func (*Filter) AddCondition

func (f *Filter) AddCondition(c func(p *peer.Peer) bool)

func (*Filter) AddPeer

func (f *Filter) AddPeer(peer identity.ID)

func (*Filter) AddPeers

func (f *Filter) AddPeers(n []*peer.Peer)

func (*Filter) Apply

func (f *Filter) Apply(list []peer.PeerDistance) (filtered []peer.PeerDistance)

func (*Filter) Clean

func (f *Filter) Clean()

func (*Filter) RemovePeer

func (f *Filter) RemovePeer(peer identity.ID)

type Neighborhood

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

func NewNeighborhood

func NewNeighborhood(size int) *Neighborhood

func (*Neighborhood) Add

func (nh *Neighborhood) Add(toAdd peer.PeerDistance) bool

Add tries to add a new peer with distance to the neighborhood. It returns true, if the peer was added, or false if the neighborhood was full.

func (*Neighborhood) GetNumPeers

func (nh *Neighborhood) GetNumPeers() int

func (*Neighborhood) GetPeers

func (nh *Neighborhood) GetPeers() []*peer.Peer

func (*Neighborhood) IsFull

func (nh *Neighborhood) IsFull() bool

func (*Neighborhood) RemovePeer

func (nh *Neighborhood) RemovePeer(id identity.ID) *peer.Peer

RemovePeer removes the peer with the given ID from the neighborhood. It returns the peer that was removed or nil of no such peer exists.

func (*Neighborhood) Select

func (nh *Neighborhood) Select(candidates []peer.PeerDistance) peer.PeerDistance

func (*Neighborhood) String

func (nh *Neighborhood) String() string

func (*Neighborhood) UpdateDistance

func (nh *Neighborhood) UpdateDistance(anchor, salt []byte)

type Option

type Option interface {
	// contains filtered or unexported methods
}

An Option configures the peer selection.

func DropOnUpdate

func DropOnUpdate(dropOnUpdate bool) Option

DropOnUpdate sets the Option to drop all neighbors when the salt is updated.

func Logger

func Logger(log *logger.Logger) Option

Logger sets the logger.

func ManaFunc

func ManaFunc(manaFunc mana.Func) Option

ManaFunc sets the Option of the mana function to use.

func NeighborBlockDuration

func NeighborBlockDuration(blockDuration time.Duration) Option

NeighborBlockDuration sets the amount of time a peer should remain in the blocklist.

func NeighborSkipTimeout

func NeighborSkipTimeout(skipTimeout time.Duration) Option

NeighborSkipTimeout sets the amount of time for which we should skip the peer and don't try to connect with it after any problem encountered with that peer.

func NeighborValidator

func NeighborValidator(neighborValidator Validator) Option

NeighborValidator sets the potential neighbor validator.

func R

func R(r int) Option

R sets the Option for R.

func Ro

func Ro(ro float64) Option

Ro sets the Option for Ro.

func UseMana

func UseMana(useMana bool) Option

UseMana sets the Option to use mana.

type Parameters

type Parameters struct {
	InboundNeighborSize        int           // number of inbound neighbors
	OutboundNeighborSize       int           // number of outbound neighbors
	SaltLifetime               time.Duration // lifetime of the private and public local salt
	OutboundUpdateInterval     time.Duration // time interval after which the outbound neighbors are checked
	FullOutboundUpdateInterval time.Duration // time after which the full outbound neighbors are updated
}

Parameters holds the parameters that can be configured.

type PeeringEvent

type PeeringEvent struct {
	Peer     *peer.Peer // peering partner
	Status   bool       // true, when the peering partner has accepted the request
	Distance uint32     // the distance between the peers
}

PeeringEvent bundles the information sent in the OutgoingPeering and IncomingPeering event.

type Protocol

type Protocol struct {
	server.Protocol
	// contains filtered or unexported fields
}

The Protocol handles the neighbor selection. It responds to incoming messages and sends own requests when needed.

func New

func New(local *peer.Local, disc DiscoverProtocol, opts ...Option) *Protocol

New creates a new neighbor selection protocol.

func (*Protocol) BlockNeighbor

func (p *Protocol) BlockNeighbor(id identity.ID, ttl ...time.Duration)

BlockNeighbor does everything the RemoveNeighbor() does, but it also adds the neighbor to the blocklist for certain amount of time to prevent future peering.

func (*Protocol) Close

func (p *Protocol) Close()

Close finalizes the protocol.

func (*Protocol) Events

func (p *Protocol) Events() *Events

Events returns all the events that are triggered during the neighbor selection.

func (*Protocol) GetIncomingNeighbors

func (p *Protocol) GetIncomingNeighbors() []*peer.Peer

GetIncomingNeighbors returns the current incoming neighbors.

func (*Protocol) GetNeighbors

func (p *Protocol) GetNeighbors() []*peer.Peer

GetNeighbors returns the current neighbors.

func (*Protocol) GetOutgoingNeighbors

func (p *Protocol) GetOutgoingNeighbors() []*peer.Peer

GetOutgoingNeighbors returns the current outgoing neighbors.

func (*Protocol) HandleMessage

func (p *Protocol) HandleMessage(s *server.Server, fromAddr *net.UDPAddr, from *identity.Identity, data []byte) (bool, error)

HandleMessage responds to incoming neighbor selection messages.

func (*Protocol) PeeringDrop

func (p *Protocol) PeeringDrop(to *peer.Peer)

PeeringDrop sends a peering drop message to the given peer, non-blocking and does not wait for any responses.

func (*Protocol) PeeringRequest

func (p *Protocol) PeeringRequest(to *peer.Peer, salt *salt.Salt) (bool, error)

PeeringRequest sends a PeeringRequest to the given peer. This method blocks until a response is received and the status answer is returned.

func (*Protocol) RemoveNeighbor

func (p *Protocol) RemoveNeighbor(id identity.ID)

RemoveNeighbor removes the peer with the given id from the incoming and outgoing neighbors. If such a peer was actually contained in anyone of the neighbor sets, the corresponding event is triggered and the corresponding peering drop is sent. Otherwise the call is ignored.

func (*Protocol) Start

func (p *Protocol) Start(s server.Sender)

Start starts the actual neighbor selection over the provided Sender.

func (*Protocol) UnblockNeighbor

func (p *Protocol) UnblockNeighbor(id identity.ID)

UnblockNeighbor removes the neighbor from the blocklist to allow future peering.

type SaltUpdatedEvent

type SaltUpdatedEvent struct {
	Public, Private *salt.Salt // the updated salt
}

SaltUpdatedEvent bundles the information sent in the SaltUpdated event.

type Selector

type Selector interface {
	Select(candidates peer.PeerDistance) *peer.Peer
}

type Validator

type Validator interface {
	IsValid(*peer.Peer) bool
}

A Validator checks whether a peer is a valid neighbor.

type ValidatorFunc

type ValidatorFunc func(p *peer.Peer) bool

The ValidatorFunc type is an adapter to allow the use of ordinary functions as neighbor validators. If f is a function with the appropriate signature, ValidatorFunc(f) is a Validator that calls f.

func (ValidatorFunc) IsValid

func (f ValidatorFunc) IsValid(p *peer.Peer) bool

IsValid calls f(p).

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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