Documentation ¶
Index ¶
- type DHT
- func (d *DHT) AddToTable(serviceName string, peerInfo *pb.PeerInfo) (bool, error)
- func (d *DHT) Announce(_ context.Context, serviceName string, _ ...discovery.Option) error
- func (d *DHT) FindPeerSupportProtocolsAsync(context.Context, int, ...protocol.ID) <-chan peer.AddrInfo
- func (d *DHT) FindPeers(ctx context.Context, serviceName string, opts ...discovery.Option) (<-chan ma.Multiaddr, error)
- func (d *DHT) PeerConnected(pid peer.ID)
- func (d *DHT) PeerDisconnected(pid peer.ID)
- func (d *DHT) PeerProtocolSupported(protocolID protocol.ID, pid peer.ID)
- func (d *DHT) PeerProtocolUnsupported(protocolID protocol.ID, pid peer.ID)
- func (d *DHT) SendMessage(ctx context.Context, service string, protocolID protocol.ID, ...) (peer.AddrInfo, error)
- type MessageRoutingMsgMgr
- type Option
- func WithBucketSize(bucketSize int) Option
- func WithFindingInterval(interval time.Duration) Option
- func WithHeartbeatCheckInterval(heartbeatCheckInterval int) Option
- func WithHeartbeatInterval(heartbeatInterval int) Option
- func WithMessageRoutingCacheMaxSize(msgCacheMaxSize int) Option
- func WithMessageRoutingEnable(enable bool) Option
- func WithMessageRoutingForwardPeerCount(forwardPeerCount int) Option
- func WithMessageRoutingMsgLifetime(lifetime int) Option
- func WithMinConnectRate(minConnectRate float32) Option
- func WithPeerTimeoutInterval(peerTimeoutInterval int) Option
- func WithRoutingTableRefreshInterval(refreshInterval time.Duration) Option
- func WithRoutingTableRefreshQueryTimeout(timeout time.Duration) Option
- func WithVerifyPeerFunc(verifyPeer func(peerId peer.ID, chainId string) (bool, error)) Option
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type DHT ¶
type DHT struct {
// contains filtered or unexported fields
}
DHT provides a discovery service based on Distributed Hash Table. include host interface to support in different case. include a RoutingTable for searching peer. provides heartbeat function similar as ping/pong to make sure peer in table is live. addrStore save addr for peer in table. provides messageRouting function to send a msg to a peer disconnected by routing msg.
func New ¶
New return a pointer of DHT a default conf will take effect if opts is nil
func (*DHT) AddToTable ¶
AddToTable tries to add peer to routingTable and addrStore If the peer is existed in the Routing Table and has been queried before, this call is a no-op. If the peer is existed in the Routing Table but hasn't been queried before, we set it's LastUsefulAt value to the current time. This needs to done because we don't mark peers as "Useful"(by setting the LastUsefulAt value) when we first connect to them.
If the bucket to which the peer belongs is full and it's not the last bucket, we try to replace an existing peer whose LastSuccessfulOutboundQuery is above the maximum allowed threshold in that bucket with the new peer. If no such peer exists in the bucket, we do NOT add the peer to the Routing Table and return error "ErrPeerRejectedNoCapacity".
It returns a bool value set to true if the peer was newly added to the Routing Table, false otherwise. It also returns any error while adding the peer to the Routing Table. If the error is not nil, the bool value will ALWAYS be false i.e. the peer wont be added to the Routing Table it it's not already there.
A return value of false with error=nil indicates that the peer already exists in the Routing Table.
func (*DHT) Announce ¶
Announce .
@Description: tell other peers that I have supported a new service with name given. and new a RoutingTable ,initMessageRouting ,sendAnnounceToOthers ,init heartbeat. @receiver d @param _: @param serviceName: @param _: @return error:
func (*DHT) FindPeerSupportProtocolsAsync ¶
func (d *DHT) FindPeerSupportProtocolsAsync(context.Context, int, ...protocol.ID) <-chan peer.AddrInfo
FindPeerSupportProtocolsAsync provides a way to query the net addresses of the peers who support protocols given.
func (*DHT) FindPeers ¶
func (d *DHT) FindPeers(ctx context.Context, serviceName string, opts ...discovery.Option) (<-chan ma.Multiaddr, error)
FindPeers .
@Description: provides a way to find peers who support the service which name is the given. @receiver d @param ctx: @param serviceName: @param opts: @return <-chan: @return error:
func (*DHT) PeerConnected ¶
PeerConnected notify
@Description: when PeerConnected,DHT try AddToTable save addr for peer and sendFindReqMsgToOthers @receiver d @param pid
func (*DHT) PeerProtocolSupported ¶
PeerProtocolSupported do nothing
@Description: @receiver d @param protocolID @param pid
func (*DHT) PeerProtocolUnsupported ¶
PeerProtocolUnsupported do nothing
@Description: @receiver d @param protocolID @param pid
func (*DHT) SendMessage ¶
func (d *DHT) SendMessage(ctx context.Context, service string, protocolID protocol.ID, targetPeerId peer.ID, msgPayload []byte, timeout time.Duration, waitResp bool) (peer.AddrInfo, error)
SendMessage Forward messages to nodes not directly connected
@Description: Forward messages to nodes not directly connected @receiver d @param ctx: the context is used to control whether the action ends @param service:the name of the service to communicate with the target peer @param protocolID:the protocol id to communicate with the target peer @param targetPeerId: target peer id @param msgPayload: message data to send @param timeout: timeout for waiting for the target peer to respond @param waitResp: if the value is true, wait for the response information of the target peer, if it is false, return directly without waiting @return peer.AddrInfo: the address information of the target peer carried in the target peer response message @return error
type MessageRoutingMsgMgr ¶
type MessageRoutingMsgMgr struct {
// contains filtered or unexported fields
}
MessageRoutingMsgMgr used to record messages that have been received
type Option ¶
type Option func(*config) error
Option DHT option type.
func WithBucketSize ¶
WithBucketSize .
@Description: @param bucketSize: RoutingTable bucketSize @return Option:
func WithFindingInterval ¶
WithFindingInterval .
@Description: set findingTickerInterval @param interval: @return Option:
func WithHeartbeatCheckInterval ¶
WithHeartbeatCheckInterval .
@Description: set heartbeatInterval @param heartbeatCheckInterval: unit is Second @return Option:
func WithHeartbeatInterval ¶
WithHeartbeatInterval .
@Description: set heartbeatInterval @param heartbeatInterval: unit is Second @return Option:
func WithMessageRoutingCacheMaxSize ¶
WithMessageRoutingCacheMaxSize Set the message buffer size for message routing
func WithMessageRoutingEnable ¶
WithMessageRoutingEnable whether to enable message routing
func WithMessageRoutingForwardPeerCount ¶
WithMessageRoutingForwardPeerCount set the number of peers selected when message routing forwards messages
func WithMessageRoutingMsgLifetime ¶
WithMessageRoutingMsgLifetime set the time-to-live of the message routed by the message
func WithMinConnectRate ¶
WithMinConnectRate .
@Description: set a rate that connect at least should connected. @param minConnectRate: @return Option:
func WithPeerTimeoutInterval ¶
WithPeerTimeoutInterval .
@Description: set time. if dont recv heartbeat when timeout ,peer will removed @param peerTimeoutInterval: unit is Second @return Option:
func WithRoutingTableRefreshInterval ¶
WithRoutingTableRefreshInterval .
@Description: RoutingTable refreshInterval @param refreshInterval: @return Option:
func WithRoutingTableRefreshQueryTimeout ¶
WithRoutingTableRefreshQueryTimeout .
@Description: RoutingTable Refresh @param timeout: @return Option: