Make Limits type a struct
This commit is contained in:
parent
07b2c3d750
commit
56292fb505
|
@ -47,6 +47,6 @@ func (f *TopicLimitsFlag) Set(value string) error {
|
|||
if err != nil {
|
||||
return err
|
||||
}
|
||||
(*f)[discv5.Topic(parts[0])] = params.Limits{minL, maxL}
|
||||
(*f)[discv5.Topic(parts[0])] = params.NewLimits(minL, maxL)
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -52,17 +52,17 @@ func TestTopicLimitsFlag(t *testing.T) {
|
|||
{
|
||||
shortcut: "single",
|
||||
flags: []string{"whisper=1,1"},
|
||||
expected: TopicLimitsFlag{"whisper": params.Limits{1, 1}},
|
||||
expected: TopicLimitsFlag{"whisper": params.NewLimits(1, 1)},
|
||||
},
|
||||
{
|
||||
shortcut: "multiple",
|
||||
flags: []string{"whisper=1,1", "les=2,3"},
|
||||
expected: TopicLimitsFlag{"whisper": params.Limits{1, 1}, "les": params.Limits{2, 3}},
|
||||
expected: TopicLimitsFlag{"whisper": params.NewLimits(1, 1), "les": params.NewLimits(2, 3)},
|
||||
},
|
||||
{
|
||||
shortcut: "corrupted",
|
||||
flags: []string{" whisper=1,1 ", " les=2,3"},
|
||||
expected: TopicLimitsFlag{"whisper": params.Limits{1, 1}, "les": params.Limits{2, 3}},
|
||||
expected: TopicLimitsFlag{"whisper": params.NewLimits(1, 1), "les": params.NewLimits(2, 3)},
|
||||
},
|
||||
{
|
||||
shortcut: "badseparator",
|
||||
|
|
|
@ -181,7 +181,17 @@ func (c *ClusterConfig) String() string {
|
|||
}
|
||||
|
||||
// Limits represent min and max amount of peers
|
||||
type Limits [2]int
|
||||
type Limits struct {
|
||||
Min, Max int
|
||||
}
|
||||
|
||||
// NewLimits creates new Limits config with given min and max values.
|
||||
func NewLimits(min, max int) Limits {
|
||||
return Limits{
|
||||
Min: min,
|
||||
Max: max,
|
||||
}
|
||||
}
|
||||
|
||||
// ----------
|
||||
// UpstreamRPCConfig
|
||||
|
|
|
@ -96,7 +96,7 @@ func (s *PeerPoolSimulationSuite) TestSingleTopicDiscoveryWithFailover() {
|
|||
topic := discv5.Topic("cap=test")
|
||||
// simulation should only rely on fast sync
|
||||
config := map[discv5.Topic]params.Limits{
|
||||
topic: {1, 1}, // limits a chosen for simplicity of the simulation
|
||||
topic: params.NewLimits(1, 1), // limits a chosen for simplicity of the simulation
|
||||
}
|
||||
peerPool := NewPeerPool(config, 100*time.Millisecond, 100*time.Millisecond, nil, true)
|
||||
register := NewRegister(topic)
|
||||
|
|
|
@ -107,14 +107,14 @@ func (t *TopicPool) SearchRunning() bool {
|
|||
func (t *TopicPool) MaxReached() bool {
|
||||
t.mu.RLock()
|
||||
defer t.mu.RUnlock()
|
||||
return len(t.connectedPeers) == t.limits[1]
|
||||
return len(t.connectedPeers) == t.limits.Max
|
||||
}
|
||||
|
||||
// BelowMin returns true if current number of peers is below min limit.
|
||||
func (t *TopicPool) BelowMin() bool {
|
||||
t.mu.RLock()
|
||||
defer t.mu.RUnlock()
|
||||
return len(t.connectedPeers) < t.limits[0]
|
||||
return len(t.connectedPeers) < t.limits.Min
|
||||
}
|
||||
|
||||
// ConfirmAdded called when peer was added by p2p Server.
|
||||
|
@ -145,7 +145,7 @@ func (t *TopicPool) ConfirmAdded(server *p2p.Server, nodeID discover.NodeID) {
|
|||
}
|
||||
|
||||
// if the upper limit is already reached, drop this peer
|
||||
if len(t.connectedPeers) == t.limits[1] {
|
||||
if len(t.connectedPeers) == t.limits.Max {
|
||||
log.Debug("max limit is reached drop the peer", "ID", nodeID, "topic", t.topic)
|
||||
peer.dismissed = true
|
||||
t.removeServerPeer(server, peer.peerInfo)
|
||||
|
@ -158,7 +158,7 @@ func (t *TopicPool) ConfirmAdded(server *p2p.Server, nodeID discover.NodeID) {
|
|||
peer.dismissed = false
|
||||
|
||||
// when the lower limit is reached, we can switch to slow mode
|
||||
if t.SearchRunning() && len(t.connectedPeers) == t.limits[0] {
|
||||
if t.SearchRunning() && len(t.connectedPeers) == t.limits.Min {
|
||||
t.period <- t.slowSync
|
||||
}
|
||||
}
|
||||
|
@ -193,7 +193,7 @@ func (t *TopicPool) ConfirmDropped(server *p2p.Server, nodeID discover.NodeID) b
|
|||
|
||||
// switch to fast mode as the number of connected peers is about to drop
|
||||
// below the lower limit
|
||||
if t.SearchRunning() && len(t.connectedPeers) == t.limits[0] {
|
||||
if t.SearchRunning() && len(t.connectedPeers) == t.limits.Min {
|
||||
t.period <- t.fastSync
|
||||
}
|
||||
|
||||
|
@ -267,7 +267,7 @@ func (t *TopicPool) StartSearch(server *p2p.Server) error {
|
|||
}
|
||||
|
||||
func (t *TopicPool) handleFoundPeers(server *p2p.Server, found <-chan *discv5.Node, lookup <-chan bool) {
|
||||
if len(t.connectedPeers) >= t.limits[0] {
|
||||
if len(t.connectedPeers) >= t.limits.Min {
|
||||
t.period <- t.slowSync
|
||||
} else {
|
||||
t.period <- t.fastSync
|
||||
|
@ -313,7 +313,7 @@ func (t *TopicPool) processFoundNode(server *p2p.Server, node *discv5.Node) {
|
|||
}
|
||||
|
||||
// the upper limit is not reached, so let's add this peer
|
||||
if len(t.connectedPeers) < t.limits[1] {
|
||||
if len(t.connectedPeers) < t.limits.Max {
|
||||
t.addServerPeer(server, t.peerPool[node.ID].peerInfo)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -38,7 +38,7 @@ func (s *TopicPoolSuite) SetupTest() {
|
|||
}
|
||||
s.Require().NoError(s.peer.Start())
|
||||
topic := discv5.Topic("cap=cap1")
|
||||
limits := params.Limits{1, 2}
|
||||
limits := params.NewLimits(1, 2)
|
||||
s.topicPool = NewTopicPool(topic, limits, 100*time.Millisecond, 200*time.Millisecond)
|
||||
s.topicPool.period = make(chan time.Duration, 2)
|
||||
s.topicPool.running = 1
|
||||
|
@ -97,7 +97,7 @@ func (s *TopicPoolSuite) TestNewPeerSelectedOnDrop() {
|
|||
func (s *TopicPoolSuite) TestRequestedDoesntRemove() {
|
||||
// max limit is 1 because we test that 2nd peer will stay in local table
|
||||
// when we request to drop it
|
||||
s.topicPool.limits = params.Limits{1, 1}
|
||||
s.topicPool.limits = params.NewLimits(1, 1)
|
||||
peer1 := discv5.NewNode(discv5.NodeID{1}, s.peer.Self().IP, 32311, 32311)
|
||||
peer2 := discv5.NewNode(discv5.NodeID{2}, s.peer.Self().IP, 32311, 32311)
|
||||
s.topicPool.processFoundNode(s.peer, peer1)
|
||||
|
@ -115,7 +115,7 @@ func (s *TopicPoolSuite) TestRequestedDoesntRemove() {
|
|||
}
|
||||
|
||||
func (s *TopicPoolSuite) TestTheMostRecentPeerIsSelected() {
|
||||
s.topicPool.limits = params.Limits{1, 1}
|
||||
s.topicPool.limits = params.NewLimits(1, 1)
|
||||
|
||||
peer1 := discv5.NewNode(discv5.NodeID{1}, s.peer.Self().IP, 32311, 32311)
|
||||
peer2 := discv5.NewNode(discv5.NodeID{2}, s.peer.Self().IP, 32311, 32311)
|
||||
|
|
Loading…
Reference in New Issue