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

mendersoftware / mender-server / 1833359013

23 May 2025 01:25PM UTC coverage: 66.318% (+0.5%) from 65.861%
1833359013

Pull #674

gitlab-ci

mzedel
fix(gui): prevented device tag editor to be shown when no tags exist

- this is to reduce confusion about tags defined async to the current session not being visible

Ticket: ME-528
Signed-off-by: Manuel Zedel <manuel.zedel@northern.tech>
Pull Request #674: ME-529, ME-528 - adjustments to device tag editing

29554 of 44564 relevant lines covered (66.32%)

1.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

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

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

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

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

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

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

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