dht

package
v1.3.1 Latest Latest
Warning

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

Go to latest
Published: Dec 27, 2023 License: Apache-2.0 Imports: 29 Imported by: 0

Documentation

Index

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

func New(host host.Host, logger api.Logger, opts ...Option) (*DHT, error)

New return a pointer of DHT a default conf will take effect if opts is nil

func (*DHT) AddToTable

func (d *DHT) AddToTable(serviceName string, peerInfo *pb.PeerInfo) (bool, error)

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

func (d *DHT) Announce(_ context.Context, serviceName string, _ ...discovery.Option) error

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

func (d *DHT) PeerConnected(pid peer.ID)

PeerConnected notify

@Description:
 when PeerConnected,DHT try AddToTable
 save addr for peer
 and sendFindReqMsgToOthers
@receiver d
@param pid

func (*DHT) PeerDisconnected

func (d *DHT) PeerDisconnected(pid peer.ID)

PeerDisconnected .

@Description:
@receiver d
@param pid

func (*DHT) PeerProtocolSupported

func (d *DHT) PeerProtocolSupported(protocolID protocol.ID, pid peer.ID)

PeerProtocolSupported do nothing

@Description:
@receiver d
@param protocolID
@param pid

func (*DHT) PeerProtocolUnsupported

func (d *DHT) PeerProtocolUnsupported(protocolID protocol.ID, pid peer.ID)

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

func WithBucketSize(bucketSize int) Option

WithBucketSize .

@Description:
@param bucketSize: RoutingTable bucketSize
@return Option:

func WithFindingInterval

func WithFindingInterval(interval time.Duration) Option

WithFindingInterval .

@Description: set findingTickerInterval
@param interval:
@return Option:

func WithHeartbeatCheckInterval

func WithHeartbeatCheckInterval(heartbeatCheckInterval int) Option

WithHeartbeatCheckInterval .

@Description: set heartbeatInterval
@param heartbeatCheckInterval: unit is Second
@return Option:

func WithHeartbeatInterval

func WithHeartbeatInterval(heartbeatInterval int) Option

WithHeartbeatInterval .

@Description: set heartbeatInterval
@param heartbeatInterval: unit is Second
@return Option:

func WithMessageRoutingCacheMaxSize

func WithMessageRoutingCacheMaxSize(msgCacheMaxSize int) Option

WithMessageRoutingCacheMaxSize Set the message buffer size for message routing

func WithMessageRoutingEnable

func WithMessageRoutingEnable(enable bool) Option

WithMessageRoutingEnable whether to enable message routing

func WithMessageRoutingForwardPeerCount

func WithMessageRoutingForwardPeerCount(forwardPeerCount int) Option

WithMessageRoutingForwardPeerCount set the number of peers selected when message routing forwards messages

func WithMessageRoutingMsgLifetime

func WithMessageRoutingMsgLifetime(lifetime int) Option

WithMessageRoutingMsgLifetime set the time-to-live of the message routed by the message

func WithMinConnectRate

func WithMinConnectRate(minConnectRate float32) Option

WithMinConnectRate .

@Description: set a rate that connect at least should connected.
@param minConnectRate:
@return Option:

func WithPeerTimeoutInterval

func WithPeerTimeoutInterval(peerTimeoutInterval int) Option

WithPeerTimeoutInterval .

@Description: set time. if dont recv heartbeat when timeout ,peer will removed
@param peerTimeoutInterval: unit is Second
@return Option:

func WithRoutingTableRefreshInterval

func WithRoutingTableRefreshInterval(refreshInterval time.Duration) Option

WithRoutingTableRefreshInterval .

@Description:  RoutingTable refreshInterval
@param refreshInterval:
@return Option:

func WithRoutingTableRefreshQueryTimeout

func WithRoutingTableRefreshQueryTimeout(timeout time.Duration) Option

WithRoutingTableRefreshQueryTimeout .

@Description:  RoutingTable Refresh
@param timeout:
@return Option:

func WithVerifyPeerFunc

func WithVerifyPeerFunc(verifyPeer func(peerId peer.ID, chainId string) (bool, error)) Option

WithVerifyPeerFunc .

@Description: register verify peer function
@param verifyPeer:
@return Option:

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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