-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
introduce peer manager #31
Conversation
peerManager.go
Outdated
continue | ||
} | ||
if err := ma.host.Connect(ctx, peer); err != nil { | ||
return err |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we should count the new connection success / total, instead of return err on a single connection? something like
newConn = len(peerChan) - len(ma.host.Network().Conns())
for {
if err := ma.host.Connect(ctx, peer); err == nil {
success++
}
}
if success < newConn * 2/3 {
return err
}
d850ed1
to
047757e
Compare
p2p.go
Outdated
@@ -66,6 +65,8 @@ type ( | |||
EnableRateLimit bool `yaml:"enableRateLimit"` | |||
RateLimit RateLimitConfig `yaml:"rateLimit"` | |||
PrivateNetworkPSK string `yaml:"privateNetworkPSK"` | |||
GroupID string `yaml:"groupID"` | |||
ServerMode bool `yaml:"serverMode"` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- remove
ServerMode
? we discussed it needs to be server mode, so no need to change regarding this - add a blacklist timeout value, see comment below
p2p.go
Outdated
return nil | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remove
p2p.go
Outdated
dhtOpt = append(dhtOpt, dht.Mode(dht.ModeServer)) | ||
} else { | ||
dhtOpt = append(dhtOpt, dht.Mode(dht.ModeClient)) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remove
} | ||
|
||
// AddBootstrap adds bootnode into peermanager | ||
func (h *Host) AddBootstrap(bootNodeAddrs []multiaddr.Multiaddr) error { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remove this func? it is not used
peerManager.go
Outdated
return err | ||
} | ||
|
||
func (ma *peerManager) AddBootstrap(ids ...core.PeerID) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remove this func? it is not used
peerManager.go
Outdated
@@ -157,6 +157,11 @@ func (ma *peerManager) BlockPeer(pr peer.ID) { | |||
} | |||
} | |||
|
|||
func (ma *peerManager) BlockPeer(pr peer.ID) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: call this RemovePeer
?
routing: routing, | ||
ns: ns, | ||
bootstrap: make(map[peer.ID]bool), | ||
advertiseInterval: 3 * time.Hour, // DHT provider record is republished every 3hrs by default |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The group id will expire in 24 hrs. So it's advertised forcely every 3 hrs.
return pm | ||
} | ||
|
||
func (pm *peerManager) JoinOverlay() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i think the go-routine only needs to be triggered once? but not start a new go-routine every time this is called?
type peerManager struct {
advertise sync.Once
}
func (pm *peerManager) JoinOverlay(
pm.advertise.Do(func() {
for {
select
...
}
})
)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good suggestion
}(pm) | ||
} | ||
|
||
func (pm *peerManager) Advertise() error { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
also we don't need this? in JoinOverlay()
, ticker.C
will fire every 3 hours to keep advertising itself, right? maybe we can reduce to interval to like 30 min
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is the func triggerred externally
peerManager.go
Outdated
go func(pm *peerManager) { | ||
for { | ||
select { | ||
case ns := <-pm.advertiseQueue: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
also this is not needed? since it serves the same purpose as ticker.C
return pm.host.Network().ClosePeer(pr) | ||
} | ||
|
||
func (pm *peerManager) TryBlockPeer(pr peer.ID) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this name is not accurate
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any suggestion?
pm.blacklist.Set(pr, val.(int)+1) | ||
if val.(int)+1 >= pm.blacklistTolerance { | ||
pm.host.Network().ClosePeer(pr) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if a peer has been set more than pm.blacklistTolerance
times, then after that it will always go to this if, and keep calling ClosePeer()
, is that intended? maybe should remove the peer after reaching pm.blacklistTolerance
times (give him a chance to start over)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The peer will be filtered out by blocked()
, so the node can't get this peer by ConnectedPeers()
. And the id will be removed from the map when BlacklistTimeout reaches.
Description
The new component
peerManager
is introduced for peer discovering, peer connecting, and peer managing. In thepeerManager
, the Rendezvous protocol is used for peer discovery, which replaces the oldNeighbors
methodTo interact with
peerManager
externally, the methodsAdvertise
,FindPeers
,BlockPeer
,AddBootstrap
andConnectedPeers
are added inHost
class.Code clean work for
NewHost
func