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

lightningnetwork / lnd / 15561477203

10 Jun 2025 01:54PM UTC coverage: 58.351% (-10.1%) from 68.487%
15561477203

Pull #9356

github

web-flow
Merge 6440b25db into c6d6d4c0b
Pull Request #9356: lnrpc: add incoming/outgoing channel ids filter to forwarding history request

33 of 36 new or added lines in 2 files covered. (91.67%)

28366 existing lines in 455 files now uncovered.

97715 of 167461 relevant lines covered (58.35%)

1.81 hits per line

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

73.33
/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 {
3✔
40
        // Figure out the tier we fall into based on our current flap count.
3✔
41
        tier := flapCount / rateLimitScale
3✔
42

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

50
        return rateLimits[tier]
3✔
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 {
3✔
60

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

3✔
65
        // If our cooldown period has not elapsed yet, we just return our flap
3✔
66
        // count. We allow fractional cooldown periods once this period has
3✔
67
        // elapsed, so we do not want to apply a fractional cooldown before the
3✔
68
        // full cooldown period has elapsed.
3✔
69
        if timeSinceFlap < flapCountCooldownPeriod {
6✔
70
                return flapCount
3✔
71
        }
3✔
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.
UNCOV
76
        cooldownPeriods := float64(timeSinceFlap) /
×
UNCOV
77
                float64(flapCountCooldownPeriod)
×
UNCOV
78

×
UNCOV
79
        effectiveFactor := math.Pow(flapCountCooldownFactor, cooldownPeriods)
×
UNCOV
80

×
UNCOV
81
        return int(float64(flapCount) * effectiveFactor)
×
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