peer

package
v0.3.5 Latest Latest
Warning

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

Go to latest
Published: Jun 12, 2015 License: MIT Imports: 14 Imported by: 0

Documentation

Overview

package peer implements an object used to represent peers in the ipfs network.

Index

Constants

View Source
const (

	// TempAddrTTL is the ttl used for a short lived address
	TempAddrTTL = time.Second * 10

	// ProviderAddrTTL is the TTL of an address we've received from a provider.
	// This is also a temporary address, but lasts longer. After this expires,
	// the records we return will require an extra lookup.
	ProviderAddrTTL = time.Minute * 10

	// RecentlyConnectedAddrTTL is used when we recently connected to a peer.
	// It means that we are reasonably certain of the peer's address.
	RecentlyConnectedAddrTTL = time.Minute * 10

	// OwnObservedAddrTTL is used for our own external addresses observed by peers.
	OwnObservedAddrTTL = time.Minute * 10

	// PermanentAddrTTL is the ttl for a "permanent address" (e.g. bootstrap nodes)
	// if we haven't shipped you an update to ipfs in 356 days
	// we probably arent running the same bootstrap nodes...
	PermanentAddrTTL = time.Hour * 24 * 356

	// ConnectedAddrTTL is the ttl used for the addresses of a peer to whom
	// we're connected directly. This is basically permanent, as we will
	// clear them + re-add under a TempAddrTTL after disconnecting.
	ConnectedAddrTTL = PermanentAddrTTL
)
View Source
const (
	// AddressTTL is the expiration time of addresses.
	AddressTTL = time.Hour
)

Variables

View Source
var LatencyEWMASmoothing = 0.1

LatencyEWMASmooting governs the decay of the EWMA (the speed at which it changes). This must be a normalized (0-1) value. 1 is 100% change, 0 is no change.

Functions

func IDB58Encode

func IDB58Encode(id ID) string

IDB58Encode returns b58-encoded string

func IDHexEncode

func IDHexEncode(id ID) string

IDHexEncode returns b58-encoded string

Types

type AddrBook

type AddrBook interface {

	// AddAddr calls AddAddrs(p, []ma.Multiaddr{addr}, ttl)
	AddAddr(p ID, addr ma.Multiaddr, ttl time.Duration)

	// AddAddrs gives AddrManager addresses to use, with a given ttl
	// (time-to-live), after which the address is no longer valid.
	// If the manager has a longer TTL, the operation is a no-op for that address
	AddAddrs(p ID, addrs []ma.Multiaddr, ttl time.Duration)

	// SetAddr calls mgr.SetAddrs(p, addr, ttl)
	SetAddr(p ID, addr ma.Multiaddr, ttl time.Duration)

	// SetAddrs sets the ttl on addresses. This clears any TTL there previously.
	// This is used when we receive the best estimate of the validity of an address.
	SetAddrs(p ID, addrs []ma.Multiaddr, ttl time.Duration)

	// Addresses returns all known (and valid) addresses for a given
	Addrs(p ID) []ma.Multiaddr

	// ClearAddresses removes all previously stored addresses
	ClearAddrs(p ID)
}

AddrBook is an interface that fits the new AddrManager. I'm patching it up in here to avoid changing a ton of the codebase.

type AddrManager

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

AddrManager manages addresses. The zero-value is ready to be used.

func (*AddrManager) AddAddr

func (mgr *AddrManager) AddAddr(p ID, addr ma.Multiaddr, ttl time.Duration)

AddAddr calls AddAddrs(p, []ma.Multiaddr{addr}, ttl)

func (*AddrManager) AddAddrs

func (mgr *AddrManager) AddAddrs(p ID, addrs []ma.Multiaddr, ttl time.Duration)

AddAddrs gives AddrManager addresses to use, with a given ttl (time-to-live), after which the address is no longer valid. If the manager has a longer TTL, the operation is a no-op for that address

func (*AddrManager) Addrs

func (mgr *AddrManager) Addrs(p ID) []ma.Multiaddr

Addresses returns all known (and valid) addresses for a given

func (*AddrManager) ClearAddrs

func (mgr *AddrManager) ClearAddrs(p ID)

ClearAddresses removes all previously stored addresses

func (*AddrManager) Peers

func (mgr *AddrManager) Peers() []ID

func (*AddrManager) SetAddr

func (mgr *AddrManager) SetAddr(p ID, addr ma.Multiaddr, ttl time.Duration)

SetAddr calls mgr.SetAddrs(p, addr, ttl)

func (*AddrManager) SetAddrs

func (mgr *AddrManager) SetAddrs(p ID, addrs []ma.Multiaddr, ttl time.Duration)

SetAddrs sets the ttl on addresses. This clears any TTL there previously. This is used when we receive the best estimate of the validity of an address.

type ID

type ID string

ID represents the identity of a peer.

func IDB58Decode

func IDB58Decode(s string) (ID, error)

IDB58Decode returns a b58-decoded Peer

func IDFromBytes

func IDFromBytes(b []byte) (ID, error)

IDFromBytes cast a string to ID type, and validate the id to make sure it is a multihash.

func IDFromPrivateKey

func IDFromPrivateKey(sk ic.PrivKey) (ID, error)

IDFromPrivateKey returns the Peer ID corresponding to sk

func IDFromPublicKey

func IDFromPublicKey(pk ic.PubKey) (ID, error)

IDFromPublicKey returns the Peer ID corresponding to pk

func IDFromString

func IDFromString(s string) (ID, error)

IDFromString cast a string to ID type, and validate the id to make sure it is a multihash.

func IDHexDecode

func IDHexDecode(s string) (ID, error)

IDHexDecode returns a b58-decoded Peer

func PeerInfoIDs

func PeerInfoIDs(pis []PeerInfo) []ID

func (ID) Loggable

func (id ID) Loggable() map[string]interface{}

func (ID) MatchesPrivateKey

func (id ID) MatchesPrivateKey(sk ic.PrivKey) bool

MatchesPrivateKey tests whether this ID was derived from sk

func (ID) MatchesPublicKey

func (id ID) MatchesPublicKey(pk ic.PubKey) bool

MatchesPublicKey tests whether this ID was derived from pk

func (ID) Pretty

func (id ID) Pretty() string

Pretty returns a b58-encoded string of the ID

func (ID) String

func (id ID) String() string

String prints out the peer.

TODO(brian): ensure correctness at ID generation and enforce this by only exposing functions that generate IDs safely. Then any peer.ID type found in the codebase is known to be correct.

type IDSlice

type IDSlice []ID

IDSlice for sorting peers

func (IDSlice) Len

func (es IDSlice) Len() int

func (IDSlice) Less

func (es IDSlice) Less(i, j int) bool

func (IDSlice) Swap

func (es IDSlice) Swap(i, j int)

type KeyBook

type KeyBook interface {
	PubKey(ID) ic.PubKey
	AddPubKey(ID, ic.PubKey) error

	PrivKey(ID) ic.PrivKey
	AddPrivKey(ID, ic.PrivKey) error
}

KeyBook tracks the Public keys of Peers.

type Metrics

type Metrics interface {

	// RecordLatency records a new latency measurement
	RecordLatency(ID, time.Duration)

	// LatencyEWMA returns an exponentially-weighted moving avg.
	// of all measurements of a peer's latency.
	LatencyEWMA(ID) time.Duration
}

Metrics is just an object that tracks metrics across a set of peers.

func NewMetrics

func NewMetrics() Metrics

type PeerInfo

type PeerInfo struct {
	ID    ID
	Addrs []ma.Multiaddr
}

PeerInfo is a small struct used to pass around a peer with a set of addresses (and later, keys?). This is not meant to be a complete view of the system, but rather to model updates to the peerstore. It is used by things like the routing system.

func PeerInfos

func PeerInfos(ps Peerstore, peers []ID) []PeerInfo

func (*PeerInfo) MarshalJSON

func (pi *PeerInfo) MarshalJSON() ([]byte, error)

func (*PeerInfo) UnmarshalJSON

func (pi *PeerInfo) UnmarshalJSON(b []byte) error

type Peerstore

type Peerstore interface {
	AddrBook
	KeyBook
	Metrics

	// Peers returns a list of all peer.IDs in this Peerstore
	Peers() []ID

	// PeerInfo returns a peer.PeerInfo struct for given peer.ID.
	// This is a small slice of the information Peerstore has on
	// that peer, useful to other services.
	PeerInfo(ID) PeerInfo

	// Get/Put is a simple registry for other peer-related key/value pairs.
	// if we find something we use often, it should become its own set of
	// methods. this is a last resort.
	Get(id ID, key string) (interface{}, error)
	Put(id ID, key string, val interface{}) error
}

Peerstore provides a threadsafe store of Peer related information.

func NewPeerstore

func NewPeerstore() Peerstore

NewPeerstore creates a threadsafe collection of peers.

type Set

type Set map[ID]struct{}

Map maps a Peer ID to a struct.

Directories

Path Synopsis
Package addr provides utility functions to handle peer addresses.
Package addr provides utility functions to handle peer addresses.

Jump to

Keyboard shortcuts

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