-
Notifications
You must be signed in to change notification settings - Fork 0
/
ratelimit.go
59 lines (50 loc) · 1.45 KB
/
ratelimit.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package ari
import (
"fmt"
"codeberg.org/anaseto/goal"
"go.uber.org/ratelimit"
)
type RateLimiter struct {
Limiter *ratelimit.Limiter
}
// Append implements goal.BV.
func (rateLimiter *RateLimiter) Append(_ *goal.Context, dst []byte, _ bool) []byte {
return append(dst, fmt.Sprintf("<%v %#v>", rateLimiter.Type(), rateLimiter.Limiter)...)
}
// LessT implements goal.BV.
func (rateLimiter *RateLimiter) LessT(y goal.BV) bool {
return rateLimiter.Type() < y.Type()
}
// Matches implements goal.BV.
func (rateLimiter *RateLimiter) Matches(_ goal.BV) bool {
return false
}
// Type implements goal.BV.
func (rateLimiter *RateLimiter) Type() string {
return "ari.RateLimiter"
}
func vfRateLimitNew(_ *goal.Context, args []goal.V) goal.V {
if len(args) > 1 {
return goal.Panicf("ratelimit.new : too many arguments (%d), expects 1 argument", len(args))
}
x := args[0]
if x.IsI() {
limiter := ratelimit.New(int(x.I()), ratelimit.WithoutSlack)
return goal.NewV(&RateLimiter{Limiter: &limiter})
}
return panicType("ratelimit.new i", "i", x)
}
func vfRateLimitTake(_ *goal.Context, args []goal.V) goal.V {
if len(args) > 1 {
return goal.Panicf("ratelimit.take : too many arguments (%d), expects 1 argument", len(args))
}
x := args[0]
switch xv := x.BV().(type) {
case *RateLimiter:
limiter := xv.Limiter
now := (*limiter).Take()
return goal.NewV(&Time{Time: &now})
default:
return panicType("ratelimit.take ari.RateLimiter", "ari.RateLimiter", x)
}
}