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

mendersoftware / mender-server / 1550767116

19 Nov 2024 11:58AM UTC coverage: 72.563% (-0.2%) from 72.771%
1550767116

Pull #202

gitlab-ci

alfrunes
test: Fix deviceauth.cache tests after changing client initialization

Signed-off-by: Alf-Rune Siqveland <alf.rune@northern.tech>
Pull Request #202: MEN-7733: Rate limits for devices APIs

4174 of 6072 branches covered (68.74%)

Branch coverage included in aggregate %.

135 of 380 new or added lines in 8 files covered. (35.53%)

84 existing lines in 3 files now uncovered.

42593 of 58378 relevant lines covered (72.96%)

15.45 hits per line

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

0.0
/backend/pkg/rate/limit.go
1
package rate
2

3
import (
4
        "context"
5
        "time"
6

7
        "golang.org/x/time/rate"
8
)
9

10
// Limiter implements a rate limiting interface based on golang.org/x/time/rate
11
// but extending the interface with the ability to expose internal errors.
12
type Limiter interface {
13
        Reserve(ctx context.Context) (Reservation, error)
14
        Tokens(ctx context.Context) (uint64, error)
15
}
16

17
// Reservation is a point-in-time reservation of a ratelimit token. If the token
18
// is approved OK return true, otherways Delay will return the duration for next
19
// token(s) to become available. While Tokens return the number of available
20
// tokens after the reservation.
21
type Reservation interface {
22
        OK() bool
23
        Delay() time.Duration
24
        Tokens() uint64
25
}
26

27
type limiter rate.Limiter
28

NEW
29
func NewLimiter(limit int, interval time.Duration) Limiter {
×
NEW
30
        return (*limiter)(rate.NewLimiter(rate.Every(interval/time.Duration(limit)), limit))
×
NEW
31
}
×
32

NEW
33
func (lim *limiter) Reserve(context.Context) (Reservation, error) {
×
NEW
34
        now := time.Now()
×
NEW
35
        goLimiter := (*rate.Limiter)(lim)
×
NEW
36
        res := &reservation{
×
NEW
37
                reservation: goLimiter.ReserveN(now, 1),
×
NEW
38
                time:        now,
×
NEW
39
        }
×
NEW
40
        if res.OK() {
×
NEW
41
                res.tokens = uint64(goLimiter.TokensAt(now))
×
NEW
42
        }
×
NEW
43
        return res, nil
×
44
}
45

NEW
46
func (lim *limiter) Tokens(context.Context) (uint64, error) {
×
NEW
47
        goLimiter := (*rate.Limiter)(lim)
×
NEW
48
        if tokens := goLimiter.Tokens(); tokens > 0 {
×
NEW
49
                return uint64(tokens), nil
×
NEW
50
        }
×
NEW
51
        return 0, nil
×
52
}
53

54
type reservation struct {
55
        time        time.Time
56
        tokens      uint64
57
        reservation *rate.Reservation
58
}
59

NEW
60
func (r *reservation) OK() bool {
×
NEW
61
        return r.Delay() == 0
×
NEW
62
}
×
63

NEW
64
func (r *reservation) Delay() time.Duration {
×
NEW
65
        return r.reservation.DelayFrom(r.time)
×
NEW
66
}
×
67

NEW
68
func (r *reservation) Tokens() uint64 {
×
NEW
69
        return r.tokens
×
NEW
70
}
×
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