Skip to content

Commit

Permalink
Merge pull request #34 from migalabs/feature/limit-array-sizes
Browse files Browse the repository at this point in the history
Feature/limit array sizes
  • Loading branch information
Miga Labs authored Dec 13, 2021
2 parents 70958c1 + 8f37d93 commit 6a1f7d0
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
14 changes: 14 additions & 0 deletions src/db/peer.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ import (
"github.com/sirupsen/logrus"
)

var (
MaxArraySize int = 10
)

// Stores all the information related to a peer
type Peer struct {

Expand Down Expand Up @@ -202,13 +206,23 @@ func (pm *Peer) FetchConnectionsFromNewPeer(newPeer Peer) {

pm.ConnectionEvent(newConnectedDirection, time)
}

if len(pm.ConnectedDirection) > MaxArraySize {
tmpConnDirection := pm.ConnectedDirection[len(pm.ConnectedDirection)-MaxArraySize-1 : len(pm.ConnectedDirection)-1]
pm.ConnectedDirection = tmpConnDirection
}

for _, time := range newPeer.DisconnectionTimes {
pm.DisconnectionEvent(time)
}

for _, errorTmp := range newPeer.Error {
pm.Error = append(pm.Error, errorTmp)
}
if len(pm.Error) > MaxArraySize {
tmpError := pm.Error[len(pm.Error)-MaxArraySize-1 : len(pm.Error)-1]
pm.Error = tmpError
}

if newPeer.LastErrorTimestamp.After(pm.LastErrorTimestamp) {
pm.LastErrorTimestamp = newPeer.LastErrorTimestamp
Expand Down
10 changes: 6 additions & 4 deletions src/peering/pruning_strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -614,6 +614,11 @@ func (c *PrunedPeer) NextConnection() time.Time {
if c.DelayObj.GetType() == Minus1DelayType { // in case of Minus1, this is new peer and we want it to connect as soon as possible
return time.Time{}
}

if c.DelayObj.CalculateDelay() > MaxDelayTime {
return c.BaseConnectionTimestamp.Add(MaxDelayTime)
}

// nextConnection should be from first event + the applied delay
return c.BaseConnectionTimestamp.Add(c.DelayObj.CalculateDelay())
}
Expand Down Expand Up @@ -657,10 +662,7 @@ func (c *PrunedPeer) UpdateDelay(newDelayType string) {
c.BaseDeprecationTimestamp = time.Now()
}

// only add degree in case we have not exceeded the MaxDelay allowed
if c.DelayObj.CalculateDelay() < MaxDelayTime {
c.DelayObj.AddDegree()
}
c.DelayObj.AddDegree()
}

// ErrorToDelayType:
Expand Down

0 comments on commit 6a1f7d0

Please sign in to comment.