• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

lightningnetwork / lnd / 15951470896

29 Jun 2025 04:23AM UTC coverage: 67.594% (-0.01%) from 67.606%
15951470896

Pull #9751

github

web-flow
Merge 599d9b051 into 6290edf14
Pull Request #9751: multi: update Go to 1.23.10 and update some packages

135088 of 199851 relevant lines covered (67.59%)

21909.44 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

100.0
/chanfitness/rate_limit.go
1
package chanfitness
2

3
import (
4
        "math"
5
        "time"
6
)
7

8
const (
9
        // rateLimitScale is the number of events we allow per rate limited
10
        // tier. Increasing this value makes our rate limiting more lenient,
11
        // decreasing it makes us less lenient.
12
        rateLimitScale = 200
13

14
        // flapCountCooldownFactor is the factor by which we decrease a peer's
15
        // flap count if they have not flapped for the cooldown period.
16
        flapCountCooldownFactor = 0.95
17

18
        // flapCountCooldownPeriod is the amount of time that we require a peer
19
        // has not flapped for before we reduce their all time flap count using
20
        // our cooldown factor.
21
        flapCountCooldownPeriod = time.Hour * 8
22
)
23

24
// rateLimits is the set of rate limit tiers we apply to our peers based on
25
// their flap count. A peer can be placed in their tier by dividing their flap
26
// count by the rateLimitScale and returning the value at that index.
27
var rateLimits = []time.Duration{
28
        time.Second,
29
        time.Second * 5,
30
        time.Second * 30,
31
        time.Minute,
32
        time.Minute * 30,
33
        time.Hour,
34
}
35

36
// getRateLimit returns the value of the rate limited tier that we are on based
37
// on current flap count. If a peer's flap count exceeds the top tier, we just
38
// return our highest tier.
39
func getRateLimit(flapCount int) time.Duration {
32✔
40
        // Figure out the tier we fall into based on our current flap count.
32✔
41
        tier := flapCount / rateLimitScale
32✔
42

32✔
43
        // If we have more events than our number of tiers, we just use the
32✔
44
        // last tier
32✔
45
        tierLen := len(rateLimits)
32✔
46
        if tier >= tierLen {
33✔
47
                tier = tierLen - 1
1✔
48
        }
1✔
49

50
        return rateLimits[tier]
32✔
51
}
52

53
// cooldownFlapCount takes a timestamped flap count, and returns its value
54
// scaled down by our cooldown factor if at least our cooldown period has
55
// elapsed since the peer last flapped. We do this because we store all-time
56
// flap count for peers, and want to allow downgrading of peers that have not
57
// flapped for a long time.
58
func cooldownFlapCount(now time.Time, flapCount int,
59
        lastFlap time.Time) int {
26✔
60

26✔
61
        // Calculate time since our last flap, and the number of times we need
26✔
62
        // to apply our cooldown factor.
26✔
63
        timeSinceFlap := now.Sub(lastFlap)
26✔
64

26✔
65
        // If our cooldown period has not elapsed yet, we just return our flap
26✔
66
        // count. We allow fractional cooldown periods once this period has
26✔
67
        // elapsed, so we do not want to apply a fractional cooldown before the
26✔
68
        // full cooldown period has elapsed.
26✔
69
        if timeSinceFlap < flapCountCooldownPeriod {
48✔
70
                return flapCount
22✔
71
        }
22✔
72

73
        // Get the factor by which we need to cooldown our flap count. If
74
        // insufficient time has passed to cooldown our flap count. Use use a
75
        // float so that we allow fractional cooldown periods.
76
        cooldownPeriods := float64(timeSinceFlap) /
4✔
77
                float64(flapCountCooldownPeriod)
4✔
78

4✔
79
        effectiveFactor := math.Pow(flapCountCooldownFactor, cooldownPeriods)
4✔
80

4✔
81
        return int(float64(flapCount) * effectiveFactor)
4✔
82
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc