-
Notifications
You must be signed in to change notification settings - Fork 25
/
backend.go
106 lines (92 loc) · 3.91 KB
/
backend.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// Copyright 2015-present, Cyrill @ Schumacher.fm and the CoreStore contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package backendratelimit
import (
"github.com/corestoreio/pkg/config/cfgmodel"
"github.com/corestoreio/pkg/config/element"
"github.com/corestoreio/pkg/net/ratelimit"
)
// Configuration just exported for the sake of documentation. See fields for more
// information. Please call the New() function for creating a new Backend
// object. Only the New() function will set the paths to the fields.
type Configuration struct {
*ratelimit.OptionFactories
// Disabled set to true to disable the rate limiting.
//
// Path: net/ratelimit/disabled
Disabled cfgmodel.Bool
// Burst defines the number of requests that will be allowed to
// exceed the rate in a single burst and must be greater than or equal to
// zero.
//
// Path: net/ratelimit/burst
Burst cfgmodel.Int
// Requests number of requests allowed per time period
//
// Path: net/ratelimit/requests
Requests cfgmodel.Int
// Duration per second (s), minute (i), hour (h), day (d)
//
// Path: net/ratelimit/duration
Duration cfgmodel.Str
// GCRAName sets the name which GCRA can be used. The GCRA must be
// registered prior to calling the middleware handler. The name is usually
// the package name. For example net/ratelimit/memstore or
// net/ratelimit/redigostore. Leaving this configuration value empty or
// setting a not registered name causes the middleware handler to panic.
//
// Path: net/ratelimit_storage/gcra_name
GCRAName cfgmodel.Str
// StorageGCRAMaxMemoryKeys If maxKeys > 0 (enabled), the number of
// different keys is restricted to the specified amount. In this case, it
// uses an LRU algorithm to evict older keys to make room for newer ones.
//
// Path: net/ratelimit_storage/enable_gcra_memory
StorageGCRAMaxMemoryKeys cfgmodel.Int
// StorageGCRARedis a valid Redis URL enables Redis as GCRA key
// storage. URLs should follow the draft IANA specification for the scheme
// (https://www.iana.org/assignments/uri-schemes/prov/redis).
//
//
// For example:
// redis://localhost:6379/3
// redis://:6380/0 => connects to localhost:6380
// redis:// => connects to localhost:6379 with DB 0
// redis://empty:myPassword@clusterName.xxxxxx.0001.usw2.cache.amazonaws.com:6379/0
//
// Path: net/ratelimit_storage/enable_gcra_redis
StorageGCRARedis cfgmodel.Str
}
// New initializes the backend configuration models containing the cfgpath.Route
// variable to the appropriate entries in the storage. The argument Sections
// and opts will be applied to all models.
func New(cfgStruct element.Sections, opts ...cfgmodel.Option) *Configuration {
be := &Configuration{
OptionFactories: ratelimit.NewOptionFactories(),
}
opts = append(opts, cfgmodel.WithFieldFromSectionSlice(cfgStruct))
be.Disabled = cfgmodel.NewBool(`net/ratelimit/disabled`, opts...)
be.Burst = cfgmodel.NewInt(`net/ratelimit/burst`, opts...)
be.Requests = cfgmodel.NewInt(`net/ratelimit/requests`, opts...)
be.Duration = cfgmodel.NewStr(`net/ratelimit/duration`, append(opts, cfgmodel.WithSourceByString(
"s", "Second",
"i", "Minute",
"h", "Hour",
"d", "Day",
))...)
be.GCRAName = cfgmodel.NewStr(`net/ratelimit_storage/gcra_name`, opts...)
be.StorageGCRAMaxMemoryKeys = cfgmodel.NewInt(`net/ratelimit_storage/enable_gcra_memory`, opts...)
be.StorageGCRARedis = cfgmodel.NewStr(`net/ratelimit_storage/enable_gcra_redis`, opts...)
return be
}