network

package
v0.4.3 Latest Latest
Warning

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

Go to latest
Published: Jan 29, 2019 License: Apache-2.0 Imports: 31 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrPeerNotFound = errors.New("Peer not found")

ErrPeerNotFound means the peer is not found

Functions

func LenSyncMap added in v0.2.0

func LenSyncMap(m *sync.Map) uint

LenSyncMap counts the length of a sync.map

Types

type ConfigBasedPeerMaintainer

type ConfigBasedPeerMaintainer struct {
	Overlay *IotxOverlay
	Addrs   []net.Addr
}

ConfigBasedPeerMaintainer maintain the neighbors by reading the topology file

func NewConfigBasedPeerMaintainer

func NewConfigBasedPeerMaintainer(o *IotxOverlay, t *Topology) *ConfigBasedPeerMaintainer

NewConfigBasedPeerMaintainer creates an instance of ConfigBasedPeerMaintainer

func (*ConfigBasedPeerMaintainer) Update added in v0.3.0

func (cbpm *ConfigBasedPeerMaintainer) Update()

Update adds the configured addresses into the peer list

type Gossip

type Gossip struct {
	Overlay     *IotxOverlay
	Dispatcher  dispatcher.Dispatcher
	MsgLogs     *sync.Map
	CleanerTask *routine.RecurringTask
	// contains filtered or unexported fields
}

Gossip relays messages in the IotxOverlay (at least once semantics)

func NewGossip

func NewGossip(o *IotxOverlay) *Gossip

NewGossip generates a Gossip instance

func (*Gossip) AttachDispatcher

func (g *Gossip) AttachDispatcher(dispatcher dispatcher.Dispatcher)

AttachDispatcher attaches to a Dispatcher instance

func (*Gossip) OnReceivingMsg

func (g *Gossip) OnReceivingMsg(msg *network.BroadcastReq) error

OnReceivingMsg listens to and handles the incoming broadcast message

func (*Gossip) Start added in v0.3.0

func (g *Gossip) Start(ctx context.Context) error

Start starts Gossip.

func (*Gossip) Stop added in v0.3.0

func (g *Gossip) Stop(ctx context.Context) error

Stop stops Gossip.

type HealthChecker

type HealthChecker struct {
	Overlay        *IotxOverlay
	SilentInterval time.Duration
}

HealthChecker will check its peers at constant interval. If a peer is found not reachable for given period, it would be removed from the peer list

func NewHealthChecker

func NewHealthChecker(o *IotxOverlay) *HealthChecker

NewHealthChecker creates an instance of HealthChecker

func (*HealthChecker) Check added in v0.3.0

func (hc *HealthChecker) Check()

Check checks peer health

type IotxOverlay added in v0.3.0

type IotxOverlay struct {
	PM         *PeerManager
	RPC        *RPCServer
	Gossip     *Gossip
	Tasks      []*routine.RecurringTask
	Config     *config.Network
	Dispatcher dispatcher.Dispatcher
	// contains filtered or unexported fields
}

IotxOverlay is the implementation

func NewOverlay

func NewOverlay(config *config.Network) *IotxOverlay

NewOverlay creates an instance of IotxOverlay

func (*IotxOverlay) AttachDispatcher added in v0.3.0

func (o *IotxOverlay) AttachDispatcher(dispatcher dispatcher.Dispatcher)

AttachDispatcher attaches to a Dispatcher instance

func (*IotxOverlay) Broadcast added in v0.3.0

func (o *IotxOverlay) Broadcast(chainID uint32, msg proto.Message) error

Broadcast lets the caller to broadcast the message to all nodes in the P2P network

func (*IotxOverlay) GetPeers added in v0.3.0

func (o *IotxOverlay) GetPeers() []net.Addr

GetPeers returns the current neighbors' network identifiers

func (*IotxOverlay) Self added in v0.3.0

func (o *IotxOverlay) Self() net.Addr

Self returns the RPC server address to receive messages

func (*IotxOverlay) Start added in v0.3.0

func (o *IotxOverlay) Start(ctx context.Context) error

Start starts IotxOverlay and it's sub-models.

func (*IotxOverlay) Stop added in v0.3.0

func (o *IotxOverlay) Stop(ctx context.Context) error

Stop stops IotxOverlay and it's sub-models.

func (*IotxOverlay) Tell added in v0.3.0

func (o *IotxOverlay) Tell(chainID uint32, node net.Addr, msg proto.Message) error

Tell tells a given node a proto message

type MsgLogsCleaner

type MsgLogsCleaner struct {
	G *Gossip
}

MsgLogsCleaner periodically refreshes the recent received message log

func NewMsgLogsCleaner

func NewMsgLogsCleaner(g *Gossip) *MsgLogsCleaner

NewMsgLogsCleaner generates a MsgLogsCleaner instance

func (*MsgLogsCleaner) Clean added in v0.3.0

func (c *MsgLogsCleaner) Clean()

Clean cleans logs

type Overlay

type Overlay interface {
	lifecycle.StartStopper
	Broadcast(uint32, proto.Message) error
	Tell(uint32, net.Addr, proto.Message) error
	Self() net.Addr
	GetPeers() []net.Addr
}

Overlay represents the peer-to-peer network

type Peer

type Peer struct {
	node.Node
	Client      pb.PeerClient
	Conn        *grpc.ClientConn
	Ctx         context.Context
	LastResTime time.Time
}

Peer represents a node in the peer-to-peer networks

func NewPeer

func NewPeer(n string, addr string) *Peer

NewPeer creates an instance of Peer

func NewTCPPeer

func NewTCPPeer(addr string) *Peer

NewTCPPeer creates an instance of Peer with tcp transportation

func (*Peer) BroadcastMsg

func (p *Peer) BroadcastMsg(req *pb.BroadcastReq) (*pb.BroadcastRes, error)

BroadcastMsg implements the client side RPC

func (*Peer) Close

func (p *Peer) Close() error

Close terminates the connection

func (*Peer) Connect

func (p *Peer) Connect(config *config.Network) error

Connect connects the peer

func (*Peer) GetPeers

func (p *Peer) GetPeers(req *pb.GetPeersReq) (*pb.GetPeersRes, error)

GetPeers implements the client side RPC

func (*Peer) Ping

func (p *Peer) Ping(ping *pb.Ping) (*pb.Pong, error)

Ping implements the client side RPC

func (*Peer) Tell

func (p *Peer) Tell(req *pb.TellReq) (*pb.TellRes, error)

Tell implements the client side RPC

type PeerMaintainer

type PeerMaintainer struct {
	Overlay *IotxOverlay
	// contains filtered or unexported fields
}

PeerMaintainer helps maintain enough connections to other peers in the P2P networks

func NewPeerMaintainer

func NewPeerMaintainer(o *IotxOverlay) *PeerMaintainer

NewPeerMaintainer creates an instance of PeerMaintainer

func (*PeerMaintainer) Update added in v0.3.0

func (pm *PeerMaintainer) Update()

Update maintains peer connection. Current strategy is to get the (upper_bound - count) peer addresses from one of the current peer if the count is lower than the lower bound

type PeerManager

type PeerManager struct {
	// TODO: Need to revisit sync.Map: https://github.com/golang/go/issues/24112
	Peers              *sync.Map
	Overlay            *IotxOverlay
	NumPeersLowerBound uint
	NumPeersUpperBound uint
}

PeerManager represents the outgoing neighbor list TODO: We should decouple peer address and peer. Node can know more nodes than it connects to

func NewPeerManager

func NewPeerManager(o *IotxOverlay, lb uint, ub uint) *PeerManager

NewPeerManager creates an instance of PeerManager

func (*PeerManager) AddPeer

func (pm *PeerManager) AddPeer(addr string)

AddPeer adds a new peer

func (*PeerManager) GetOrAddPeer

func (pm *PeerManager) GetOrAddPeer(addr string) *Peer

GetOrAddPeer gets a peer. If it is still not in the neighbor list, it will be added first.

func (*PeerManager) RemoveLRUPeer

func (pm *PeerManager) RemoveLRUPeer()

RemoveLRUPeer removes the least recently used (contacted) peer

func (*PeerManager) RemovePeer

func (pm *PeerManager) RemovePeer(addr string)

RemovePeer removes an existing peer

type Pinger

type Pinger struct {
	Overlay *IotxOverlay
}

Pinger is the recurring logic to constantly check if the node can talk to its peers

func NewPinger

func NewPinger(o *IotxOverlay) *Pinger

NewPinger creates an instance of Pinger

func (*Pinger) Ping added in v0.3.0

func (h *Pinger) Ping()

Ping pings the neighbor peers

type RPCServer

type RPCServer struct {
	node.Node

	Server  *grpc.Server
	Overlay *IotxOverlay
	// contains filtered or unexported fields
}

RPCServer represents the listener at the transportation layer

func NewRPCServer

func NewRPCServer(o *IotxOverlay) *RPCServer

NewRPCServer creates an instance of RPCServer

func (*RPCServer) Broadcast

func (s *RPCServer) Broadcast(ctx context.Context, req *pb.BroadcastReq) (*pb.BroadcastRes, error)

Broadcast implements the server side RPC logic

func (*RPCServer) GetPeers

func (s *RPCServer) GetPeers(ctx context.Context, req *pb.GetPeersReq) (*pb.GetPeersRes, error)

GetPeers implements the server side RPC logic

func (*RPCServer) LastReqTime added in v0.2.0

func (s *RPCServer) LastReqTime() time.Time

LastReqTime returns the timestamp of the last accepted request

func (*RPCServer) Ping

func (s *RPCServer) Ping(ctx context.Context, ping *pb.Ping) (*pb.Pong, error)

Ping implements the server side RPC logic

func (*RPCServer) Start

func (s *RPCServer) Start(_ context.Context) error

Start starts the rpc server

func (*RPCServer) Stop

func (s *RPCServer) Stop(_ context.Context) error

Stop stops the rpc server

func (*RPCServer) Tell

func (s *RPCServer) Tell(ctx context.Context, req *pb.TellReq) (*pb.TellRes, error)

Tell implements the server side RPC logic

type Topology added in v0.3.0

type Topology struct {
	NeighborList map[string][]string `yaml:"neighborList"`
}

Topology is the neighbor list for each node. This is used for generating the P2P network in a given topology. Note that the list contains the outgoing connections.

func NewTopology added in v0.3.0

func NewTopology(path string) (*Topology, error)

NewTopology loads the topology struct from the given yaml file

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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