MCPcopy Create free account
hub / github.com/TheThingsNetwork/lorawan-stack / New

Function New

pkg/ratelimit/config.go:43–89  ·  view source on GitHub ↗

New creates a new ratelimit.Interface from configuration.

(ctx context.Context, conf config.RateLimiting, blobConf config.BlobConfig, httpClientProvider httpclient.Provider)

Source from the content-addressed store, hash-verified

41
42// New creates a new ratelimit.Interface from configuration.
43func New(ctx context.Context, conf config.RateLimiting, blobConf config.BlobConfig, httpClientProvider httpclient.Provider) (Interface, error) {
44 defaultLimiter := &NoopRateLimiter{}
45 profiles := conf.Profiles
46
47 fetcher, err := conf.Fetcher(ctx, blobConf, httpClientProvider)
48 if err != nil {
49 return nil, err
50 }
51 if fetcher != nil {
52 b, err := fetcher.File("rate-limiting.yml")
53 if err != nil {
54 return nil, err
55 }
56 var overrideProfiles struct {
57 Profiles []config.RateLimitingProfile `yaml:"profiles"`
58 }
59 if err := yaml.Unmarshal(b, &overrideProfiles); err != nil {
60 return nil, err
61 }
62 profiles = append(profiles, overrideProfiles.Profiles...)
63 }
64 if len(profiles) == 0 {
65 return defaultLimiter, nil
66 }
67
68 l := &muxRateLimiter{
69 defaultLimiter: defaultLimiter,
70 limiters: make(map[string]Interface, len(profiles)),
71 }
72 for _, profile := range profiles {
73 if len(profile.Associations) == 0 {
74 continue
75 }
76 limiter, err := NewProfile(ctx, profile, StoreConfig{
77 Provider: conf.Provider,
78 Memory: conf.Memory,
79 Redis: conf.Redis.Client,
80 })
81 if err != nil {
82 return nil, err
83 }
84 for _, assocName := range profile.Associations {
85 l.limiters[assocName] = limiter
86 }
87 }
88 return l, nil
89}
90
91var errInvalidRate = errors.DefineInvalidArgument("invalid_rate", "invalid rate `{rate}` for profile `{name}`")
92

Callers 2

NewFunction · 0.92
TestRateLimitFunction · 0.92

Calls 4

NewProfileFunction · 0.85
FileMethod · 0.65
UnmarshalMethod · 0.65
FetcherMethod · 0.45

Tested by 1

TestRateLimitFunction · 0.74