Skip to content

Commit

Permalink
decouple global coutner decay from source counter decay
Browse files Browse the repository at this point in the history
So that we can have fast reaction, while retaining source counters for longer.
  • Loading branch information
vyzo committed Sep 7, 2020
1 parent 1d21536 commit 8191980
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 14 deletions.
32 changes: 19 additions & 13 deletions peer_gater.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,17 @@ var (
DefaultPeerGaterIgnoreWeight = 1.0
DefaultPeerGaterRejectWeight = 4.0
DefaultPeerGaterThreshold = 0.33
DefaultPeerGaterDecay = ScoreParameterDecay(time.Hour)
DefaultPeerGaterGlobalDecay = ScoreParameterDecay(10 * time.Minute)
DefaultPeerGaterSourceDecay = ScoreParameterDecay(time.Hour)
)

// PeerGaterParams groups together parameters that control the operation of the peer gater
type PeerGaterParams struct {
// when the ratio of throttled/validated messages exceeds this threshold, the gater turns on
Threshold float64
// (linear) decay parameter for gater counters
Decay float64
GlobalDecay float64 // global counter decay
SourceDecay float64 // per IP counter decay
// decay interval
DecayInterval time.Duration
// counter zeroing threshold
Expand All @@ -53,8 +55,11 @@ func (p *PeerGaterParams) validate() error {
if p.Threshold <= 0 {
return fmt.Errorf("invalid Threshold; must be > 0")
}
if p.Decay <= 0 || p.Decay >= 1 {
return fmt.Errorf("invalid Decay; must be between 0 and 1")
if p.GlobalDecay <= 0 || p.GlobalDecay >= 1 {
return fmt.Errorf("invalid GlobalDecay; must be between 0 and 1")
}
if p.SourceDecay <= 0 || p.SourceDecay >= 1 {
return fmt.Errorf("invalid SourceDecay; must be between 0 and 1")
}
if p.DecayInterval < time.Second {
return fmt.Errorf("invalid DecayInterval; must be at least 1s")
Expand All @@ -81,10 +86,11 @@ func (p *PeerGaterParams) validate() error {

// NewPeerGaterParams creates a new PeerGaterParams struct, using the specified threshold and decay
// parameters and default values for all other parameters.
func NewPeerGaterParams(threshold, decay float64) *PeerGaterParams {
func NewPeerGaterParams(threshold, globalDecay, sourceDecay float64) *PeerGaterParams {
return &PeerGaterParams{
Threshold: threshold,
Decay: decay,
GlobalDecay: globalDecay,
SourceDecay: sourceDecay,
DecayToZero: DefaultDecayToZero,
DecayInterval: DefaultDecayInterval,
RetainStats: DefaultPeerGaterRetainStats,
Expand All @@ -97,7 +103,7 @@ func NewPeerGaterParams(threshold, decay float64) *PeerGaterParams {

// DefaultPeerGaterParams creates a new PeerGaterParams struct using default values
func DefaultPeerGaterParams() *PeerGaterParams {
return NewPeerGaterParams(DefaultPeerGaterThreshold, DefaultPeerGaterDecay)
return NewPeerGaterParams(DefaultPeerGaterThreshold, DefaultPeerGaterGlobalDecay, DefaultPeerGaterSourceDecay)
}

// the gater object.
Expand Down Expand Up @@ -205,35 +211,35 @@ func (pg *peerGater) decayStats() {
pg.Lock()
defer pg.Unlock()

pg.validate *= pg.params.Decay
pg.validate *= pg.params.GlobalDecay
if pg.validate < pg.params.DecayToZero {
pg.validate = 0
}

pg.throttle *= pg.params.Decay
pg.throttle *= pg.params.GlobalDecay
if pg.throttle < pg.params.DecayToZero {
pg.throttle = 0
}

now := time.Now()
for ip, st := range pg.ipStats {
if st.connected > 0 {
st.deliver *= pg.params.Decay
st.deliver *= pg.params.SourceDecay
if st.deliver < pg.params.DecayToZero {
st.deliver = 0
}

st.duplicate *= pg.params.Decay
st.duplicate *= pg.params.SourceDecay
if st.duplicate < pg.params.DecayToZero {
st.duplicate = 0
}

st.ignore *= pg.params.Decay
st.ignore *= pg.params.SourceDecay
if st.ignore < pg.params.DecayToZero {
st.ignore = 0
}

st.reject *= pg.params.Decay
st.reject *= pg.params.SourceDecay
if st.reject < pg.params.DecayToZero {
st.reject = 0
}
Expand Down
2 changes: 1 addition & 1 deletion peer_gater_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func TestPeerGater(t *testing.T) {
peerA := peer.ID("A")
peerAip := "1.2.3.4"

params := NewPeerGaterParams(.1, .9)
params := NewPeerGaterParams(.1, .9, .999)
err := params.validate()
if err != nil {
t.Fatal(err)
Expand Down

0 comments on commit 8191980

Please sign in to comment.