refact(ratelimiter): Updates due to refactor in go-rate
This commit is contained in:
7 files changed
+180
-266
No files matched your search
@@ -94,7 +94,7 @@ require (
|
||||
github.com/hashicorp/cap/ldap v0.0.0-20231012003312-273118a6e3b8
|
||||
github.com/hashicorp/dbassert v0.0.0-20231012105025-1bc1bd88e22b
|
||||
github.com/hashicorp/go-kms-wrapping/extras/kms/v2 v2.0.0-20231124110655-4315424d22d0
|
||||
github.com/hashicorp/go-rate v0.0.0-20231129140553-d912bfc00ee2
|
||||
github.com/hashicorp/go-rate v0.0.0-20231130213406-dd41f530f095
|
||||
github.com/hashicorp/go-version v1.6.0
|
||||
github.com/hashicorp/nodeenrollment v0.2.9
|
||||
github.com/jackc/pgx/v5 v5.4.3
|
||||
|
||||
@@ -260,8 +260,8 @@ github.com/hashicorp/go-multierror v1.1.1/go.mod h1:iw975J/qwKPdAO1clOe2L8331t/9
|
||||
github.com/hashicorp/go-plugin v1.4.3/go.mod h1:5fGEH17QVwTTcR0zV7yhDPLLmFX9YSZ38b18Udy6vYQ=
|
||||
github.com/hashicorp/go-plugin v1.5.2 h1:aWv8eimFqWlsEiMrYZdPYl+FdHaBJSN4AWwGWfT1G2Y=
|
||||
github.com/hashicorp/go-plugin v1.5.2/go.mod h1:w1sAEES3g3PuV/RzUrgow20W2uErMly84hhD3um1WL4=
|
||||
github.com/hashicorp/go-rate v0.0.0-20231129140553-d912bfc00ee2 h1:vNHBERW+GdaqHG0a+McDV/ExpdFshheEzie41wb8nts=
|
||||
github.com/hashicorp/go-rate v0.0.0-20231129140553-d912bfc00ee2/go.mod h1:uTQC66EFZuSKunHYx0hHIfdH3nhRrv7UOOE8Ug0yI0A=
|
||||
github.com/hashicorp/go-rate v0.0.0-20231130213406-dd41f530f095 h1:l/scdJ8Uy823fhxFEdwlDR5NkDRdoOOaH6g36Uqz1AM=
|
||||
github.com/hashicorp/go-rate v0.0.0-20231130213406-dd41f530f095/go.mod h1:uTQC66EFZuSKunHYx0hHIfdH3nhRrv7UOOE8Ug0yI0A=
|
||||
github.com/hashicorp/go-retryablehttp v0.5.3/go.mod h1:9B5zBasrRhHXnJnui7y6sL7es7NDiJgTc6Er0maI1Xs=
|
||||
github.com/hashicorp/go-retryablehttp v0.7.4 h1:ZQgVdpTdAL7WpMIwLzCfbalOcSUdkDZnpUv3/+BxzFA=
|
||||
github.com/hashicorp/go-retryablehttp v0.7.4/go.mod h1:Jy/gPYAdjqffZ/yFGCFV2doI5wjtH1ewM9u8iYVjtX8=
|
||||
|
||||
@@ -116,10 +116,10 @@ func (c Configs) Equal(o Configs) bool {
|
||||
|
||||
// Limits creates a slice of rate.Limit from the Configs. This will enumerate
|
||||
// every combination of resource+action, defining a Limit for each.
|
||||
func (c Configs) Limits(ctx context.Context) ([]*rate.Limit, error) {
|
||||
func (c Configs) Limits(ctx context.Context) ([]rate.Limit, error) {
|
||||
const op = "ratelimit.(Configs).Limits"
|
||||
|
||||
defaults := make(map[string]*rate.Limit, len(resource.Map)*len(action.Map))
|
||||
defaults := make(map[string]rate.Limit, len(resource.Map)*len(action.Map))
|
||||
|
||||
allResources := make([]resource.Type, 0, len(resource.Map))
|
||||
for _, res := range resource.Map {
|
||||
@@ -145,52 +145,46 @@ func (c Configs) Limits(ctx context.Context) ([]*rate.Limit, error) {
|
||||
ipAddressKey := fmt.Sprintf("%s:%s:%s", res.String(), a.String(), rate.LimitPerIPAddress)
|
||||
switch a {
|
||||
case action.List:
|
||||
defaults[inTotalKey] = &rate.Limit{
|
||||
defaults[inTotalKey] = &rate.Limited{
|
||||
Resource: res.String(),
|
||||
Action: a.String(),
|
||||
Per: rate.LimitPerTotal,
|
||||
Unlimited: false,
|
||||
MaxRequests: DefaultInTotalListRequestLimit,
|
||||
Period: DefaultListPeriod,
|
||||
}
|
||||
defaults[authTokenKey] = &rate.Limit{
|
||||
defaults[authTokenKey] = &rate.Limited{
|
||||
Resource: res.String(),
|
||||
Action: a.String(),
|
||||
Per: rate.LimitPerAuthToken,
|
||||
Unlimited: false,
|
||||
MaxRequests: DefaultAuthTokenListRequestLimit,
|
||||
Period: DefaultListPeriod,
|
||||
}
|
||||
defaults[ipAddressKey] = &rate.Limit{
|
||||
defaults[ipAddressKey] = &rate.Limited{
|
||||
Resource: res.String(),
|
||||
Action: a.String(),
|
||||
Per: rate.LimitPerIPAddress,
|
||||
Unlimited: false,
|
||||
MaxRequests: DefaultIpAddressListRequestLimit,
|
||||
Period: DefaultListPeriod,
|
||||
}
|
||||
default:
|
||||
defaults[inTotalKey] = &rate.Limit{
|
||||
defaults[inTotalKey] = &rate.Limited{
|
||||
Resource: res.String(),
|
||||
Action: a.String(),
|
||||
Per: rate.LimitPerTotal,
|
||||
Unlimited: false,
|
||||
MaxRequests: DefaultInTotalRequestLimit,
|
||||
Period: DefaultPeriod,
|
||||
}
|
||||
defaults[authTokenKey] = &rate.Limit{
|
||||
defaults[authTokenKey] = &rate.Limited{
|
||||
Resource: res.String(),
|
||||
Action: a.String(),
|
||||
Per: rate.LimitPerAuthToken,
|
||||
Unlimited: false,
|
||||
MaxRequests: DefaultAuthTokenRequestLimit,
|
||||
Period: DefaultPeriod,
|
||||
}
|
||||
defaults[ipAddressKey] = &rate.Limit{
|
||||
defaults[ipAddressKey] = &rate.Limited{
|
||||
Resource: res.String(),
|
||||
Action: a.String(),
|
||||
Per: rate.LimitPerIPAddress,
|
||||
Unlimited: false,
|
||||
MaxRequests: DefaultIpAddressRequestLimit,
|
||||
Period: DefaultPeriod,
|
||||
}
|
||||
@@ -223,13 +217,21 @@ func (c Configs) Limits(ctx context.Context) ([]*rate.Limit, error) {
|
||||
for a := range validActions {
|
||||
key := fmt.Sprintf("%s:%s:%s", res.String(), a.String(), rate.LimitPer(cc.Per))
|
||||
|
||||
defaults[key] = &rate.Limit{
|
||||
Resource: res.String(),
|
||||
Action: a.String(),
|
||||
Per: rate.LimitPer(cc.Per),
|
||||
Unlimited: cc.Unlimited,
|
||||
MaxRequests: uint64(cc.Limit),
|
||||
Period: cc.Period,
|
||||
switch {
|
||||
case cc.Unlimited:
|
||||
defaults[key] = &rate.Unlimited{
|
||||
Resource: res.String(),
|
||||
Action: a.String(),
|
||||
Per: rate.LimitPer(cc.Per),
|
||||
}
|
||||
default:
|
||||
defaults[key] = &rate.Limited{
|
||||
Resource: res.String(),
|
||||
Action: a.String(),
|
||||
Per: rate.LimitPer(cc.Per),
|
||||
MaxRequests: uint64(cc.Limit),
|
||||
Period: cc.Period,
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -251,20 +253,28 @@ func (c Configs) Limits(ctx context.Context) ([]*rate.Limit, error) {
|
||||
}
|
||||
key := fmt.Sprintf("%s:%s:%s", res.String(), a.String(), rate.LimitPer(cc.Per))
|
||||
|
||||
defaults[key] = &rate.Limit{
|
||||
Resource: res.String(),
|
||||
Action: a.String(),
|
||||
Per: rate.LimitPer(cc.Per),
|
||||
Unlimited: cc.Unlimited,
|
||||
MaxRequests: uint64(cc.Limit),
|
||||
Period: cc.Period,
|
||||
switch {
|
||||
case cc.Unlimited:
|
||||
defaults[key] = &rate.Unlimited{
|
||||
Resource: res.String(),
|
||||
Action: a.String(),
|
||||
Per: rate.LimitPer(cc.Per),
|
||||
}
|
||||
default:
|
||||
defaults[key] = &rate.Limited{
|
||||
Resource: res.String(),
|
||||
Action: a.String(),
|
||||
Per: rate.LimitPer(cc.Per),
|
||||
MaxRequests: uint64(cc.Limit),
|
||||
Period: cc.Period,
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
limits := make([]*rate.Limit, 0, len(defaults))
|
||||
limits := make([]rate.Limit, 0, len(defaults))
|
||||
for _, v := range defaults {
|
||||
limits = append(limits, v)
|
||||
}
|
||||
|
||||
@@ -21,14 +21,14 @@ func TestConfigsLimits(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
configs Configs
|
||||
want []*rate.Limit
|
||||
want []rate.Limit
|
||||
wantErr error
|
||||
}{
|
||||
{
|
||||
"empty",
|
||||
Configs{},
|
||||
func() []*rate.Limit {
|
||||
limits := make([]*rate.Limit, 0, len(resource.Map)*len(action.Map))
|
||||
func() []rate.Limit {
|
||||
limits := make([]rate.Limit, 0, len(resource.Map)*len(action.Map))
|
||||
for _, res := range resource.Map {
|
||||
switch res {
|
||||
case resource.Unknown, resource.All, resource.Controller:
|
||||
@@ -43,27 +43,24 @@ func TestConfigsLimits(t *testing.T) {
|
||||
case action.List:
|
||||
limits = append(
|
||||
limits,
|
||||
&rate.Limit{
|
||||
&rate.Limited{
|
||||
Resource: res.String(),
|
||||
Action: a.String(),
|
||||
Per: rate.LimitPerTotal,
|
||||
Unlimited: false,
|
||||
MaxRequests: DefaultInTotalListRequestLimit,
|
||||
Period: DefaultListPeriod,
|
||||
},
|
||||
&rate.Limit{
|
||||
&rate.Limited{
|
||||
Resource: res.String(),
|
||||
Action: a.String(),
|
||||
Per: rate.LimitPerIPAddress,
|
||||
Unlimited: false,
|
||||
MaxRequests: DefaultIpAddressListRequestLimit,
|
||||
Period: DefaultListPeriod,
|
||||
},
|
||||
&rate.Limit{
|
||||
&rate.Limited{
|
||||
Resource: res.String(),
|
||||
Action: a.String(),
|
||||
Per: rate.LimitPerAuthToken,
|
||||
Unlimited: false,
|
||||
MaxRequests: DefaultAuthTokenListRequestLimit,
|
||||
Period: DefaultListPeriod,
|
||||
},
|
||||
@@ -71,27 +68,24 @@ func TestConfigsLimits(t *testing.T) {
|
||||
default:
|
||||
limits = append(
|
||||
limits,
|
||||
&rate.Limit{
|
||||
&rate.Limited{
|
||||
Resource: res.String(),
|
||||
Action: a.String(),
|
||||
Per: rate.LimitPerTotal,
|
||||
Unlimited: false,
|
||||
MaxRequests: DefaultInTotalRequestLimit,
|
||||
Period: DefaultPeriod,
|
||||
},
|
||||
&rate.Limit{
|
||||
&rate.Limited{
|
||||
Resource: res.String(),
|
||||
Action: a.String(),
|
||||
Per: rate.LimitPerIPAddress,
|
||||
Unlimited: false,
|
||||
MaxRequests: DefaultIpAddressRequestLimit,
|
||||
Period: DefaultPeriod,
|
||||
},
|
||||
&rate.Limit{
|
||||
&rate.Limited{
|
||||
Resource: res.String(),
|
||||
Action: a.String(),
|
||||
Per: rate.LimitPerAuthToken,
|
||||
Unlimited: false,
|
||||
MaxRequests: DefaultAuthTokenRequestLimit,
|
||||
Period: DefaultPeriod,
|
||||
},
|
||||
@@ -128,8 +122,8 @@ func TestConfigsLimits(t *testing.T) {
|
||||
Period: time.Minute * 5,
|
||||
},
|
||||
},
|
||||
func() []*rate.Limit {
|
||||
limits := make([]*rate.Limit, 0, len(resource.Map)*len(action.Map))
|
||||
func() []rate.Limit {
|
||||
limits := make([]rate.Limit, 0, len(resource.Map)*len(action.Map))
|
||||
for _, res := range resource.Map {
|
||||
switch res {
|
||||
case resource.Unknown, resource.All, resource.Controller:
|
||||
@@ -144,27 +138,24 @@ func TestConfigsLimits(t *testing.T) {
|
||||
default:
|
||||
limits = append(
|
||||
limits,
|
||||
&rate.Limit{
|
||||
&rate.Limited{
|
||||
Resource: res.String(),
|
||||
Action: a.String(),
|
||||
Per: rate.LimitPerTotal,
|
||||
Unlimited: false,
|
||||
MaxRequests: 20,
|
||||
Period: time.Minute * 5,
|
||||
},
|
||||
&rate.Limit{
|
||||
&rate.Limited{
|
||||
Resource: res.String(),
|
||||
Action: a.String(),
|
||||
Per: rate.LimitPerIPAddress,
|
||||
Unlimited: false,
|
||||
MaxRequests: 10,
|
||||
Period: time.Minute * 5,
|
||||
},
|
||||
&rate.Limit{
|
||||
&rate.Limited{
|
||||
Resource: res.String(),
|
||||
Action: a.String(),
|
||||
Per: rate.LimitPerAuthToken,
|
||||
Unlimited: false,
|
||||
MaxRequests: 5,
|
||||
Period: time.Minute * 5,
|
||||
},
|
||||
@@ -222,8 +213,8 @@ func TestConfigsLimits(t *testing.T) {
|
||||
Period: time.Minute,
|
||||
},
|
||||
},
|
||||
func() []*rate.Limit {
|
||||
limits := make([]*rate.Limit, 0, len(resource.Map)*len(action.Map))
|
||||
func() []rate.Limit {
|
||||
limits := make([]rate.Limit, 0, len(resource.Map)*len(action.Map))
|
||||
for _, res := range resource.Map {
|
||||
switch res {
|
||||
case resource.Unknown, resource.All, resource.Controller:
|
||||
@@ -239,27 +230,24 @@ func TestConfigsLimits(t *testing.T) {
|
||||
if res == resource.Target {
|
||||
limits = append(
|
||||
limits,
|
||||
&rate.Limit{
|
||||
&rate.Limited{
|
||||
Resource: res.String(),
|
||||
Action: a.String(),
|
||||
Per: rate.LimitPerTotal,
|
||||
Unlimited: false,
|
||||
MaxRequests: 10,
|
||||
Period: time.Minute,
|
||||
},
|
||||
&rate.Limit{
|
||||
&rate.Limited{
|
||||
Resource: res.String(),
|
||||
Action: a.String(),
|
||||
Per: rate.LimitPerIPAddress,
|
||||
Unlimited: false,
|
||||
MaxRequests: 5,
|
||||
Period: time.Minute,
|
||||
},
|
||||
&rate.Limit{
|
||||
&rate.Limited{
|
||||
Resource: res.String(),
|
||||
Action: a.String(),
|
||||
Per: rate.LimitPerAuthToken,
|
||||
Unlimited: false,
|
||||
MaxRequests: 1,
|
||||
Period: time.Minute,
|
||||
},
|
||||
@@ -270,27 +258,24 @@ func TestConfigsLimits(t *testing.T) {
|
||||
default:
|
||||
limits = append(
|
||||
limits,
|
||||
&rate.Limit{
|
||||
&rate.Limited{
|
||||
Resource: res.String(),
|
||||
Action: a.String(),
|
||||
Per: rate.LimitPerTotal,
|
||||
Unlimited: false,
|
||||
MaxRequests: 20,
|
||||
Period: time.Minute * 5,
|
||||
},
|
||||
&rate.Limit{
|
||||
&rate.Limited{
|
||||
Resource: res.String(),
|
||||
Action: a.String(),
|
||||
Per: rate.LimitPerIPAddress,
|
||||
Unlimited: false,
|
||||
MaxRequests: 20,
|
||||
Period: time.Minute * 5,
|
||||
},
|
||||
&rate.Limit{
|
||||
&rate.Limited{
|
||||
Resource: res.String(),
|
||||
Action: a.String(),
|
||||
Per: rate.LimitPerAuthToken,
|
||||
Unlimited: false,
|
||||
MaxRequests: 20,
|
||||
Period: time.Minute * 5,
|
||||
},
|
||||
@@ -327,8 +312,8 @@ func TestConfigsLimits(t *testing.T) {
|
||||
Period: time.Minute,
|
||||
},
|
||||
},
|
||||
func() []*rate.Limit {
|
||||
limits := make([]*rate.Limit, 0, len(resource.Map)*len(action.Map))
|
||||
func() []rate.Limit {
|
||||
limits := make([]rate.Limit, 0, len(resource.Map)*len(action.Map))
|
||||
for _, res := range resource.Map {
|
||||
switch res {
|
||||
case resource.Unknown, resource.All, resource.Controller:
|
||||
@@ -343,27 +328,24 @@ func TestConfigsLimits(t *testing.T) {
|
||||
default:
|
||||
limits = append(
|
||||
limits,
|
||||
&rate.Limit{
|
||||
&rate.Limited{
|
||||
Resource: res.String(),
|
||||
Action: a.String(),
|
||||
Per: rate.LimitPerTotal,
|
||||
Unlimited: false,
|
||||
MaxRequests: 10,
|
||||
Period: time.Minute,
|
||||
},
|
||||
&rate.Limit{
|
||||
&rate.Limited{
|
||||
Resource: res.String(),
|
||||
Action: a.String(),
|
||||
Per: rate.LimitPerIPAddress,
|
||||
Unlimited: false,
|
||||
MaxRequests: 5,
|
||||
Period: time.Minute,
|
||||
},
|
||||
&rate.Limit{
|
||||
&rate.Limited{
|
||||
Resource: res.String(),
|
||||
Action: a.String(),
|
||||
Per: rate.LimitPerAuthToken,
|
||||
Unlimited: false,
|
||||
MaxRequests: 1,
|
||||
Period: time.Minute,
|
||||
},
|
||||
@@ -380,27 +362,24 @@ func TestConfigsLimits(t *testing.T) {
|
||||
case action.List:
|
||||
limits = append(
|
||||
limits,
|
||||
&rate.Limit{
|
||||
&rate.Limited{
|
||||
Resource: res.String(),
|
||||
Action: a.String(),
|
||||
Per: rate.LimitPerTotal,
|
||||
Unlimited: false,
|
||||
MaxRequests: DefaultInTotalListRequestLimit,
|
||||
Period: DefaultListPeriod,
|
||||
},
|
||||
&rate.Limit{
|
||||
&rate.Limited{
|
||||
Resource: res.String(),
|
||||
Action: a.String(),
|
||||
Per: rate.LimitPerIPAddress,
|
||||
Unlimited: false,
|
||||
MaxRequests: DefaultIpAddressListRequestLimit,
|
||||
Period: DefaultListPeriod,
|
||||
},
|
||||
&rate.Limit{
|
||||
&rate.Limited{
|
||||
Resource: res.String(),
|
||||
Action: a.String(),
|
||||
Per: rate.LimitPerAuthToken,
|
||||
Unlimited: false,
|
||||
MaxRequests: DefaultAuthTokenListRequestLimit,
|
||||
Period: DefaultListPeriod,
|
||||
},
|
||||
@@ -408,27 +387,24 @@ func TestConfigsLimits(t *testing.T) {
|
||||
default:
|
||||
limits = append(
|
||||
limits,
|
||||
&rate.Limit{
|
||||
&rate.Limited{
|
||||
Resource: res.String(),
|
||||
Action: a.String(),
|
||||
Per: rate.LimitPerTotal,
|
||||
Unlimited: false,
|
||||
MaxRequests: DefaultInTotalRequestLimit,
|
||||
Period: DefaultPeriod,
|
||||
},
|
||||
&rate.Limit{
|
||||
&rate.Limited{
|
||||
Resource: res.String(),
|
||||
Action: a.String(),
|
||||
Per: rate.LimitPerIPAddress,
|
||||
Unlimited: false,
|
||||
MaxRequests: DefaultIpAddressRequestLimit,
|
||||
Period: DefaultPeriod,
|
||||
},
|
||||
&rate.Limit{
|
||||
&rate.Limited{
|
||||
Resource: res.String(),
|
||||
Action: a.String(),
|
||||
Per: rate.LimitPerAuthToken,
|
||||
Unlimited: false,
|
||||
MaxRequests: DefaultAuthTokenRequestLimit,
|
||||
Period: DefaultPeriod,
|
||||
},
|
||||
@@ -466,8 +442,8 @@ func TestConfigsLimits(t *testing.T) {
|
||||
Period: time.Minute,
|
||||
},
|
||||
},
|
||||
func() []*rate.Limit {
|
||||
limits := make([]*rate.Limit, 0, len(resource.Map)*len(action.Map))
|
||||
func() []rate.Limit {
|
||||
limits := make([]rate.Limit, 0, len(resource.Map)*len(action.Map))
|
||||
for _, res := range resource.Map {
|
||||
switch res {
|
||||
case resource.Unknown, resource.All, resource.Controller:
|
||||
@@ -482,27 +458,24 @@ func TestConfigsLimits(t *testing.T) {
|
||||
case action.List, action.Read:
|
||||
limits = append(
|
||||
limits,
|
||||
&rate.Limit{
|
||||
&rate.Limited{
|
||||
Resource: res.String(),
|
||||
Action: a.String(),
|
||||
Per: rate.LimitPerTotal,
|
||||
Unlimited: false,
|
||||
MaxRequests: 10,
|
||||
Period: time.Minute,
|
||||
},
|
||||
&rate.Limit{
|
||||
&rate.Limited{
|
||||
Resource: res.String(),
|
||||
Action: a.String(),
|
||||
Per: rate.LimitPerIPAddress,
|
||||
Unlimited: false,
|
||||
MaxRequests: 5,
|
||||
Period: time.Minute,
|
||||
},
|
||||
&rate.Limit{
|
||||
&rate.Limited{
|
||||
Resource: res.String(),
|
||||
Action: a.String(),
|
||||
Per: rate.LimitPerAuthToken,
|
||||
Unlimited: false,
|
||||
MaxRequests: 1,
|
||||
Period: time.Minute,
|
||||
},
|
||||
@@ -510,27 +483,24 @@ func TestConfigsLimits(t *testing.T) {
|
||||
default:
|
||||
limits = append(
|
||||
limits,
|
||||
&rate.Limit{
|
||||
&rate.Limited{
|
||||
Resource: res.String(),
|
||||
Action: a.String(),
|
||||
Per: rate.LimitPerTotal,
|
||||
Unlimited: false,
|
||||
MaxRequests: DefaultInTotalRequestLimit,
|
||||
Period: DefaultPeriod,
|
||||
},
|
||||
&rate.Limit{
|
||||
&rate.Limited{
|
||||
Resource: res.String(),
|
||||
Action: a.String(),
|
||||
Per: rate.LimitPerIPAddress,
|
||||
Unlimited: false,
|
||||
MaxRequests: DefaultIpAddressRequestLimit,
|
||||
Period: DefaultPeriod,
|
||||
},
|
||||
&rate.Limit{
|
||||
&rate.Limited{
|
||||
Resource: res.String(),
|
||||
Action: a.String(),
|
||||
Per: rate.LimitPerAuthToken,
|
||||
Unlimited: false,
|
||||
MaxRequests: DefaultAuthTokenRequestLimit,
|
||||
Period: DefaultPeriod,
|
||||
},
|
||||
@@ -547,27 +517,24 @@ func TestConfigsLimits(t *testing.T) {
|
||||
case action.List:
|
||||
limits = append(
|
||||
limits,
|
||||
&rate.Limit{
|
||||
&rate.Limited{
|
||||
Resource: res.String(),
|
||||
Action: a.String(),
|
||||
Per: rate.LimitPerTotal,
|
||||
Unlimited: false,
|
||||
MaxRequests: DefaultInTotalListRequestLimit,
|
||||
Period: DefaultListPeriod,
|
||||
},
|
||||
&rate.Limit{
|
||||
&rate.Limited{
|
||||
Resource: res.String(),
|
||||
Action: a.String(),
|
||||
Per: rate.LimitPerIPAddress,
|
||||
Unlimited: false,
|
||||
MaxRequests: DefaultIpAddressListRequestLimit,
|
||||
Period: DefaultListPeriod,
|
||||
},
|
||||
&rate.Limit{
|
||||
&rate.Limited{
|
||||
Resource: res.String(),
|
||||
Action: a.String(),
|
||||
Per: rate.LimitPerAuthToken,
|
||||
Unlimited: false,
|
||||
MaxRequests: DefaultAuthTokenListRequestLimit,
|
||||
Period: DefaultListPeriod,
|
||||
},
|
||||
@@ -575,27 +542,24 @@ func TestConfigsLimits(t *testing.T) {
|
||||
default:
|
||||
limits = append(
|
||||
limits,
|
||||
&rate.Limit{
|
||||
&rate.Limited{
|
||||
Resource: res.String(),
|
||||
Action: a.String(),
|
||||
Per: rate.LimitPerTotal,
|
||||
Unlimited: false,
|
||||
MaxRequests: DefaultInTotalRequestLimit,
|
||||
Period: DefaultPeriod,
|
||||
},
|
||||
&rate.Limit{
|
||||
&rate.Limited{
|
||||
Resource: res.String(),
|
||||
Action: a.String(),
|
||||
Per: rate.LimitPerIPAddress,
|
||||
Unlimited: false,
|
||||
MaxRequests: DefaultIpAddressRequestLimit,
|
||||
Period: DefaultPeriod,
|
||||
},
|
||||
&rate.Limit{
|
||||
&rate.Limited{
|
||||
Resource: res.String(),
|
||||
Action: a.String(),
|
||||
Per: rate.LimitPerAuthToken,
|
||||
Unlimited: false,
|
||||
MaxRequests: DefaultAuthTokenRequestLimit,
|
||||
Period: DefaultPeriod,
|
||||
},
|
||||
@@ -627,8 +591,8 @@ func TestConfigsLimits(t *testing.T) {
|
||||
Period: time.Minute * 5,
|
||||
},
|
||||
},
|
||||
func() []*rate.Limit {
|
||||
limits := make([]*rate.Limit, 0, len(resource.Map)*len(action.Map))
|
||||
func() []rate.Limit {
|
||||
limits := make([]rate.Limit, 0, len(resource.Map)*len(action.Map))
|
||||
for _, res := range resource.Map {
|
||||
switch res {
|
||||
case resource.Unknown, resource.All, resource.Controller:
|
||||
@@ -643,27 +607,24 @@ func TestConfigsLimits(t *testing.T) {
|
||||
case action.List:
|
||||
limits = append(
|
||||
limits,
|
||||
&rate.Limit{
|
||||
&rate.Limited{
|
||||
Resource: res.String(),
|
||||
Action: a.String(),
|
||||
Per: rate.LimitPerTotal,
|
||||
Unlimited: false,
|
||||
MaxRequests: 20,
|
||||
Period: time.Minute * 5,
|
||||
},
|
||||
&rate.Limit{
|
||||
&rate.Limited{
|
||||
Resource: res.String(),
|
||||
Action: a.String(),
|
||||
Per: rate.LimitPerIPAddress,
|
||||
Unlimited: false,
|
||||
MaxRequests: DefaultIpAddressListRequestLimit,
|
||||
Period: DefaultListPeriod,
|
||||
},
|
||||
&rate.Limit{
|
||||
&rate.Limited{
|
||||
Resource: res.String(),
|
||||
Action: a.String(),
|
||||
Per: rate.LimitPerAuthToken,
|
||||
Unlimited: false,
|
||||
MaxRequests: DefaultAuthTokenListRequestLimit,
|
||||
Period: DefaultListPeriod,
|
||||
},
|
||||
@@ -671,27 +632,24 @@ func TestConfigsLimits(t *testing.T) {
|
||||
default:
|
||||
limits = append(
|
||||
limits,
|
||||
&rate.Limit{
|
||||
&rate.Limited{
|
||||
Resource: res.String(),
|
||||
Action: a.String(),
|
||||
Per: rate.LimitPerTotal,
|
||||
Unlimited: false,
|
||||
MaxRequests: 20,
|
||||
Period: time.Minute * 5,
|
||||
},
|
||||
&rate.Limit{
|
||||
&rate.Limited{
|
||||
Resource: res.String(),
|
||||
Action: a.String(),
|
||||
Per: rate.LimitPerIPAddress,
|
||||
Unlimited: false,
|
||||
MaxRequests: DefaultIpAddressRequestLimit,
|
||||
Period: DefaultPeriod,
|
||||
},
|
||||
&rate.Limit{
|
||||
&rate.Limited{
|
||||
Resource: res.String(),
|
||||
Action: a.String(),
|
||||
Per: rate.LimitPerAuthToken,
|
||||
Unlimited: false,
|
||||
MaxRequests: DefaultAuthTokenRequestLimit,
|
||||
Period: DefaultPeriod,
|
||||
},
|
||||
@@ -725,8 +683,8 @@ func TestConfigsLimits(t *testing.T) {
|
||||
Unlimited: true,
|
||||
},
|
||||
},
|
||||
func() []*rate.Limit {
|
||||
limits := make([]*rate.Limit, 0, len(resource.Map)*len(action.Map))
|
||||
func() []rate.Limit {
|
||||
limits := make([]rate.Limit, 0, len(resource.Map)*len(action.Map))
|
||||
for _, res := range resource.Map {
|
||||
switch res {
|
||||
case resource.Unknown, resource.All, resource.Controller:
|
||||
@@ -741,23 +699,20 @@ func TestConfigsLimits(t *testing.T) {
|
||||
default:
|
||||
limits = append(
|
||||
limits,
|
||||
&rate.Limit{
|
||||
Resource: res.String(),
|
||||
Action: a.String(),
|
||||
Per: rate.LimitPerTotal,
|
||||
Unlimited: true,
|
||||
&rate.Unlimited{
|
||||
Resource: res.String(),
|
||||
Action: a.String(),
|
||||
Per: rate.LimitPerTotal,
|
||||
},
|
||||
&rate.Limit{
|
||||
Resource: res.String(),
|
||||
Action: a.String(),
|
||||
Per: rate.LimitPerIPAddress,
|
||||
Unlimited: true,
|
||||
&rate.Unlimited{
|
||||
Resource: res.String(),
|
||||
Action: a.String(),
|
||||
Per: rate.LimitPerIPAddress,
|
||||
},
|
||||
&rate.Limit{
|
||||
Resource: res.String(),
|
||||
Action: a.String(),
|
||||
Per: rate.LimitPerAuthToken,
|
||||
Unlimited: true,
|
||||
&rate.Unlimited{
|
||||
Resource: res.String(),
|
||||
Action: a.String(),
|
||||
Per: rate.LimitPerAuthToken,
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
@@ -113,7 +113,7 @@ func extractResourceAction(path, method string) (res, act string, err error) {
|
||||
}
|
||||
|
||||
// LimiterFunc returns a rate.Limiter
|
||||
type LimiterFunc func() *rate.Limiter
|
||||
type LimiterFunc func() Limiter
|
||||
|
||||
// Handler is an http middleware handler that checks if a request is allowed
|
||||
// using the rate limiter returned by f. If the request is allowed, the next handler
|
||||
|
||||
@@ -36,28 +36,25 @@ func TestHandler(t *testing.T) {
|
||||
{
|
||||
"AllowedList",
|
||||
func() *rate.Limiter {
|
||||
r, err := rate.NewLimiter([]*rate.Limit{
|
||||
{
|
||||
r, err := rate.NewLimiter([]rate.Limit{
|
||||
&rate.Limited{
|
||||
Resource: resource.Target.String(),
|
||||
Action: action.List.String(),
|
||||
Per: rate.LimitPerTotal,
|
||||
Unlimited: false,
|
||||
MaxRequests: 10,
|
||||
Period: time.Minute,
|
||||
},
|
||||
{
|
||||
&rate.Limited{
|
||||
Resource: resource.Target.String(),
|
||||
Action: action.List.String(),
|
||||
Per: rate.LimitPerIPAddress,
|
||||
Unlimited: false,
|
||||
MaxRequests: 10,
|
||||
Period: time.Minute,
|
||||
},
|
||||
{
|
||||
&rate.Limited{
|
||||
Resource: resource.Target.String(),
|
||||
Action: action.List.String(),
|
||||
Per: rate.LimitPerAuthToken,
|
||||
Unlimited: false,
|
||||
MaxRequests: 10,
|
||||
Period: time.Minute,
|
||||
},
|
||||
@@ -81,28 +78,25 @@ func TestHandler(t *testing.T) {
|
||||
{
|
||||
"AllowedCreate",
|
||||
func() *rate.Limiter {
|
||||
r, err := rate.NewLimiter([]*rate.Limit{
|
||||
{
|
||||
r, err := rate.NewLimiter([]rate.Limit{
|
||||
&rate.Limited{
|
||||
Resource: resource.Target.String(),
|
||||
Action: action.Create.String(),
|
||||
Per: rate.LimitPerTotal,
|
||||
Unlimited: false,
|
||||
MaxRequests: 10,
|
||||
Period: time.Minute,
|
||||
},
|
||||
{
|
||||
&rate.Limited{
|
||||
Resource: resource.Target.String(),
|
||||
Action: action.Create.String(),
|
||||
Per: rate.LimitPerIPAddress,
|
||||
Unlimited: false,
|
||||
MaxRequests: 10,
|
||||
Period: time.Minute,
|
||||
},
|
||||
{
|
||||
&rate.Limited{
|
||||
Resource: resource.Target.String(),
|
||||
Action: action.Create.String(),
|
||||
Per: rate.LimitPerAuthToken,
|
||||
Unlimited: false,
|
||||
MaxRequests: 10,
|
||||
Period: time.Minute,
|
||||
},
|
||||
@@ -126,28 +120,25 @@ func TestHandler(t *testing.T) {
|
||||
{
|
||||
"AllowedRead",
|
||||
func() *rate.Limiter {
|
||||
r, err := rate.NewLimiter([]*rate.Limit{
|
||||
{
|
||||
r, err := rate.NewLimiter([]rate.Limit{
|
||||
&rate.Limited{
|
||||
Resource: resource.Target.String(),
|
||||
Action: action.Read.String(),
|
||||
Per: rate.LimitPerTotal,
|
||||
Unlimited: false,
|
||||
MaxRequests: 10,
|
||||
Period: time.Minute,
|
||||
},
|
||||
{
|
||||
&rate.Limited{
|
||||
Resource: resource.Target.String(),
|
||||
Action: action.Read.String(),
|
||||
Per: rate.LimitPerIPAddress,
|
||||
Unlimited: false,
|
||||
MaxRequests: 10,
|
||||
Period: time.Minute,
|
||||
},
|
||||
{
|
||||
&rate.Limited{
|
||||
Resource: resource.Target.String(),
|
||||
Action: action.Read.String(),
|
||||
Per: rate.LimitPerAuthToken,
|
||||
Unlimited: false,
|
||||
MaxRequests: 10,
|
||||
Period: time.Minute,
|
||||
},
|
||||
@@ -171,28 +162,25 @@ func TestHandler(t *testing.T) {
|
||||
{
|
||||
"AllowedUpdate",
|
||||
func() *rate.Limiter {
|
||||
r, err := rate.NewLimiter([]*rate.Limit{
|
||||
{
|
||||
r, err := rate.NewLimiter([]rate.Limit{
|
||||
&rate.Limited{
|
||||
Resource: resource.Target.String(),
|
||||
Action: action.Update.String(),
|
||||
Per: rate.LimitPerTotal,
|
||||
Unlimited: false,
|
||||
MaxRequests: 10,
|
||||
Period: time.Minute,
|
||||
},
|
||||
{
|
||||
&rate.Limited{
|
||||
Resource: resource.Target.String(),
|
||||
Action: action.Update.String(),
|
||||
Per: rate.LimitPerIPAddress,
|
||||
Unlimited: false,
|
||||
MaxRequests: 10,
|
||||
Period: time.Minute,
|
||||
},
|
||||
{
|
||||
&rate.Limited{
|
||||
Resource: resource.Target.String(),
|
||||
Action: action.Update.String(),
|
||||
Per: rate.LimitPerAuthToken,
|
||||
Unlimited: false,
|
||||
MaxRequests: 10,
|
||||
Period: time.Minute,
|
||||
},
|
||||
@@ -216,28 +204,25 @@ func TestHandler(t *testing.T) {
|
||||
{
|
||||
"AllowedDelete",
|
||||
func() *rate.Limiter {
|
||||
r, err := rate.NewLimiter([]*rate.Limit{
|
||||
{
|
||||
r, err := rate.NewLimiter([]rate.Limit{
|
||||
&rate.Limited{
|
||||
Resource: resource.Target.String(),
|
||||
Action: action.Delete.String(),
|
||||
Per: rate.LimitPerTotal,
|
||||
Unlimited: false,
|
||||
MaxRequests: 10,
|
||||
Period: time.Minute,
|
||||
},
|
||||
{
|
||||
&rate.Limited{
|
||||
Resource: resource.Target.String(),
|
||||
Action: action.Delete.String(),
|
||||
Per: rate.LimitPerIPAddress,
|
||||
Unlimited: false,
|
||||
MaxRequests: 10,
|
||||
Period: time.Minute,
|
||||
},
|
||||
{
|
||||
&rate.Limited{
|
||||
Resource: resource.Target.String(),
|
||||
Action: action.Delete.String(),
|
||||
Per: rate.LimitPerAuthToken,
|
||||
Unlimited: false,
|
||||
MaxRequests: 10,
|
||||
Period: time.Minute,
|
||||
},
|
||||
@@ -261,28 +246,25 @@ func TestHandler(t *testing.T) {
|
||||
{
|
||||
"AllowedAuthorizeSession",
|
||||
func() *rate.Limiter {
|
||||
r, err := rate.NewLimiter([]*rate.Limit{
|
||||
{
|
||||
r, err := rate.NewLimiter([]rate.Limit{
|
||||
&rate.Limited{
|
||||
Resource: resource.Target.String(),
|
||||
Action: action.AuthorizeSession.String(),
|
||||
Per: rate.LimitPerTotal,
|
||||
Unlimited: false,
|
||||
MaxRequests: 10,
|
||||
Period: time.Minute,
|
||||
},
|
||||
{
|
||||
&rate.Limited{
|
||||
Resource: resource.Target.String(),
|
||||
Action: action.AuthorizeSession.String(),
|
||||
Per: rate.LimitPerIPAddress,
|
||||
Unlimited: false,
|
||||
MaxRequests: 10,
|
||||
Period: time.Minute,
|
||||
},
|
||||
{
|
||||
&rate.Limited{
|
||||
Resource: resource.Target.String(),
|
||||
Action: action.AuthorizeSession.String(),
|
||||
Per: rate.LimitPerAuthToken,
|
||||
Unlimited: false,
|
||||
MaxRequests: 10,
|
||||
Period: time.Minute,
|
||||
},
|
||||
@@ -306,28 +288,25 @@ func TestHandler(t *testing.T) {
|
||||
{
|
||||
"AllowedAuthorizeSessionTargetNameSlash",
|
||||
func() *rate.Limiter {
|
||||
r, err := rate.NewLimiter([]*rate.Limit{
|
||||
{
|
||||
r, err := rate.NewLimiter([]rate.Limit{
|
||||
&rate.Limited{
|
||||
Resource: resource.Target.String(),
|
||||
Action: action.AuthorizeSession.String(),
|
||||
Per: rate.LimitPerTotal,
|
||||
Unlimited: false,
|
||||
MaxRequests: 10,
|
||||
Period: time.Minute,
|
||||
},
|
||||
{
|
||||
&rate.Limited{
|
||||
Resource: resource.Target.String(),
|
||||
Action: action.AuthorizeSession.String(),
|
||||
Per: rate.LimitPerIPAddress,
|
||||
Unlimited: false,
|
||||
MaxRequests: 10,
|
||||
Period: time.Minute,
|
||||
},
|
||||
{
|
||||
&rate.Limited{
|
||||
Resource: resource.Target.String(),
|
||||
Action: action.AuthorizeSession.String(),
|
||||
Per: rate.LimitPerAuthToken,
|
||||
Unlimited: false,
|
||||
MaxRequests: 10,
|
||||
Period: time.Minute,
|
||||
},
|
||||
@@ -351,28 +330,25 @@ func TestHandler(t *testing.T) {
|
||||
{
|
||||
"Limited",
|
||||
func() *rate.Limiter {
|
||||
r, err := rate.NewLimiter([]*rate.Limit{
|
||||
{
|
||||
r, err := rate.NewLimiter([]rate.Limit{
|
||||
&rate.Limited{
|
||||
Resource: resource.Target.String(),
|
||||
Action: action.List.String(),
|
||||
Per: rate.LimitPerTotal,
|
||||
Unlimited: false,
|
||||
MaxRequests: 2,
|
||||
Period: time.Minute,
|
||||
},
|
||||
{
|
||||
&rate.Limited{
|
||||
Resource: resource.Target.String(),
|
||||
Action: action.List.String(),
|
||||
Per: rate.LimitPerIPAddress,
|
||||
Unlimited: false,
|
||||
MaxRequests: 2,
|
||||
Period: time.Minute,
|
||||
},
|
||||
{
|
||||
&rate.Limited{
|
||||
Resource: resource.Target.String(),
|
||||
Action: action.List.String(),
|
||||
Per: rate.LimitPerAuthToken,
|
||||
Unlimited: false,
|
||||
MaxRequests: 2,
|
||||
Period: time.Minute,
|
||||
},
|
||||
@@ -405,52 +381,46 @@ func TestHandler(t *testing.T) {
|
||||
{
|
||||
"LimiterFull",
|
||||
func() *rate.Limiter {
|
||||
r, err := rate.NewLimiter([]*rate.Limit{
|
||||
{
|
||||
r, err := rate.NewLimiter([]rate.Limit{
|
||||
&rate.Limited{
|
||||
Resource: resource.Target.String(),
|
||||
Action: action.List.String(),
|
||||
Per: rate.LimitPerTotal,
|
||||
Unlimited: false,
|
||||
MaxRequests: 2,
|
||||
Period: time.Minute,
|
||||
},
|
||||
{
|
||||
&rate.Limited{
|
||||
Resource: resource.Target.String(),
|
||||
Action: action.Read.String(),
|
||||
Per: rate.LimitPerTotal,
|
||||
Unlimited: false,
|
||||
MaxRequests: 2,
|
||||
Period: time.Minute,
|
||||
},
|
||||
{
|
||||
&rate.Limited{
|
||||
Resource: resource.Target.String(),
|
||||
Action: action.List.String(),
|
||||
Per: rate.LimitPerIPAddress,
|
||||
Unlimited: false,
|
||||
MaxRequests: 2,
|
||||
Period: time.Minute,
|
||||
},
|
||||
{
|
||||
&rate.Limited{
|
||||
Resource: resource.Target.String(),
|
||||
Action: action.Read.String(),
|
||||
Per: rate.LimitPerIPAddress,
|
||||
Unlimited: false,
|
||||
MaxRequests: 2,
|
||||
Period: time.Minute,
|
||||
},
|
||||
{
|
||||
&rate.Limited{
|
||||
Resource: resource.Target.String(),
|
||||
Action: action.List.String(),
|
||||
Per: rate.LimitPerAuthToken,
|
||||
Unlimited: false,
|
||||
MaxRequests: 2,
|
||||
Period: time.Minute,
|
||||
},
|
||||
{
|
||||
&rate.Limited{
|
||||
Resource: resource.Target.String(),
|
||||
Action: action.Read.String(),
|
||||
Per: rate.LimitPerAuthToken,
|
||||
Unlimited: false,
|
||||
MaxRequests: 2,
|
||||
Period: time.Minute,
|
||||
},
|
||||
@@ -476,28 +446,25 @@ func TestHandler(t *testing.T) {
|
||||
{
|
||||
"MissingLimit",
|
||||
func() *rate.Limiter {
|
||||
r, err := rate.NewLimiter([]*rate.Limit{
|
||||
{
|
||||
r, err := rate.NewLimiter([]rate.Limit{
|
||||
&rate.Limited{
|
||||
Resource: resource.Target.String(),
|
||||
Action: action.List.String(),
|
||||
Per: rate.LimitPerTotal,
|
||||
Unlimited: false,
|
||||
MaxRequests: 2,
|
||||
Period: time.Minute,
|
||||
},
|
||||
{
|
||||
&rate.Limited{
|
||||
Resource: resource.Target.String(),
|
||||
Action: action.List.String(),
|
||||
Per: rate.LimitPerIPAddress,
|
||||
Unlimited: false,
|
||||
MaxRequests: 2,
|
||||
Period: time.Minute,
|
||||
},
|
||||
{
|
||||
&rate.Limited{
|
||||
Resource: resource.Target.String(),
|
||||
Action: action.List.String(),
|
||||
Per: rate.LimitPerAuthToken,
|
||||
Unlimited: false,
|
||||
MaxRequests: 2,
|
||||
Period: time.Minute,
|
||||
},
|
||||
@@ -518,28 +485,25 @@ func TestHandler(t *testing.T) {
|
||||
{
|
||||
"UnknownResource",
|
||||
func() *rate.Limiter {
|
||||
r, err := rate.NewLimiter([]*rate.Limit{
|
||||
{
|
||||
r, err := rate.NewLimiter([]rate.Limit{
|
||||
&rate.Limited{
|
||||
Resource: resource.Target.String(),
|
||||
Action: action.List.String(),
|
||||
Per: rate.LimitPerTotal,
|
||||
Unlimited: false,
|
||||
MaxRequests: 2,
|
||||
Period: time.Minute,
|
||||
},
|
||||
{
|
||||
&rate.Limited{
|
||||
Resource: resource.Target.String(),
|
||||
Action: action.List.String(),
|
||||
Per: rate.LimitPerIPAddress,
|
||||
Unlimited: false,
|
||||
MaxRequests: 2,
|
||||
Period: time.Minute,
|
||||
},
|
||||
{
|
||||
&rate.Limited{
|
||||
Resource: resource.Target.String(),
|
||||
Action: action.List.String(),
|
||||
Per: rate.LimitPerAuthToken,
|
||||
Unlimited: false,
|
||||
MaxRequests: 2,
|
||||
Period: time.Minute,
|
||||
},
|
||||
@@ -560,28 +524,25 @@ func TestHandler(t *testing.T) {
|
||||
{
|
||||
"UnknownAction",
|
||||
func() *rate.Limiter {
|
||||
r, err := rate.NewLimiter([]*rate.Limit{
|
||||
{
|
||||
r, err := rate.NewLimiter([]rate.Limit{
|
||||
&rate.Limited{
|
||||
Resource: resource.Target.String(),
|
||||
Action: action.List.String(),
|
||||
Per: rate.LimitPerTotal,
|
||||
Unlimited: false,
|
||||
MaxRequests: 2,
|
||||
Period: time.Minute,
|
||||
},
|
||||
{
|
||||
&rate.Limited{
|
||||
Resource: resource.Target.String(),
|
||||
Action: action.List.String(),
|
||||
Per: rate.LimitPerIPAddress,
|
||||
Unlimited: false,
|
||||
MaxRequests: 2,
|
||||
Period: time.Minute,
|
||||
},
|
||||
{
|
||||
&rate.Limited{
|
||||
Resource: resource.Target.String(),
|
||||
Action: action.List.String(),
|
||||
Per: rate.LimitPerAuthToken,
|
||||
Unlimited: false,
|
||||
MaxRequests: 2,
|
||||
Period: time.Minute,
|
||||
},
|
||||
@@ -602,28 +563,25 @@ func TestHandler(t *testing.T) {
|
||||
{
|
||||
"UnknownActionInvalidIdHttpMethod",
|
||||
func() *rate.Limiter {
|
||||
r, err := rate.NewLimiter([]*rate.Limit{
|
||||
{
|
||||
r, err := rate.NewLimiter([]rate.Limit{
|
||||
&rate.Limited{
|
||||
Resource: resource.Target.String(),
|
||||
Action: action.List.String(),
|
||||
Per: rate.LimitPerTotal,
|
||||
Unlimited: false,
|
||||
MaxRequests: 2,
|
||||
Period: time.Minute,
|
||||
},
|
||||
{
|
||||
&rate.Limited{
|
||||
Resource: resource.Target.String(),
|
||||
Action: action.List.String(),
|
||||
Per: rate.LimitPerIPAddress,
|
||||
Unlimited: false,
|
||||
MaxRequests: 2,
|
||||
Period: time.Minute,
|
||||
},
|
||||
{
|
||||
&rate.Limited{
|
||||
Resource: resource.Target.String(),
|
||||
Action: action.List.String(),
|
||||
Per: rate.LimitPerAuthToken,
|
||||
Unlimited: false,
|
||||
MaxRequests: 2,
|
||||
Period: time.Minute,
|
||||
},
|
||||
@@ -644,28 +602,25 @@ func TestHandler(t *testing.T) {
|
||||
{
|
||||
"UnknownActionInvalidHttpMethod",
|
||||
func() *rate.Limiter {
|
||||
r, err := rate.NewLimiter([]*rate.Limit{
|
||||
{
|
||||
r, err := rate.NewLimiter([]rate.Limit{
|
||||
&rate.Limited{
|
||||
Resource: resource.Target.String(),
|
||||
Action: action.List.String(),
|
||||
Per: rate.LimitPerTotal,
|
||||
Unlimited: false,
|
||||
MaxRequests: 2,
|
||||
Period: time.Minute,
|
||||
},
|
||||
{
|
||||
&rate.Limited{
|
||||
Resource: resource.Target.String(),
|
||||
Action: action.List.String(),
|
||||
Per: rate.LimitPerIPAddress,
|
||||
Unlimited: false,
|
||||
MaxRequests: 2,
|
||||
Period: time.Minute,
|
||||
},
|
||||
{
|
||||
&rate.Limited{
|
||||
Resource: resource.Target.String(),
|
||||
Action: action.List.String(),
|
||||
Per: rate.LimitPerAuthToken,
|
||||
Unlimited: false,
|
||||
MaxRequests: 2,
|
||||
Period: time.Minute,
|
||||
},
|
||||
@@ -686,28 +641,25 @@ func TestHandler(t *testing.T) {
|
||||
{
|
||||
"UnsupportedAction",
|
||||
func() *rate.Limiter {
|
||||
r, err := rate.NewLimiter([]*rate.Limit{
|
||||
{
|
||||
r, err := rate.NewLimiter([]rate.Limit{
|
||||
&rate.Limited{
|
||||
Resource: resource.Target.String(),
|
||||
Action: action.List.String(),
|
||||
Per: rate.LimitPerTotal,
|
||||
Unlimited: false,
|
||||
MaxRequests: 2,
|
||||
Period: time.Minute,
|
||||
},
|
||||
{
|
||||
&rate.Limited{
|
||||
Resource: resource.Target.String(),
|
||||
Action: action.List.String(),
|
||||
Per: rate.LimitPerIPAddress,
|
||||
Unlimited: false,
|
||||
MaxRequests: 2,
|
||||
Period: time.Minute,
|
||||
},
|
||||
{
|
||||
&rate.Limited{
|
||||
Resource: resource.Target.String(),
|
||||
Action: action.List.String(),
|
||||
Per: rate.LimitPerAuthToken,
|
||||
Unlimited: false,
|
||||
MaxRequests: 2,
|
||||
Period: time.Minute,
|
||||
},
|
||||
@@ -815,28 +767,25 @@ func TestHandlerErrors(t *testing.T) {
|
||||
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
r, err := rate.NewLimiter([]*rate.Limit{
|
||||
{
|
||||
r, err := rate.NewLimiter([]rate.Limit{
|
||||
&rate.Limited{
|
||||
Resource: resource.Target.String(),
|
||||
Action: action.List.String(),
|
||||
Per: rate.LimitPerTotal,
|
||||
Unlimited: false,
|
||||
MaxRequests: 10,
|
||||
Period: time.Minute,
|
||||
},
|
||||
{
|
||||
&rate.Limited{
|
||||
Resource: resource.Target.String(),
|
||||
Action: action.List.String(),
|
||||
Per: rate.LimitPerIPAddress,
|
||||
Unlimited: false,
|
||||
MaxRequests: 10,
|
||||
Period: time.Minute,
|
||||
},
|
||||
{
|
||||
&rate.Limited{
|
||||
Resource: resource.Target.String(),
|
||||
Action: action.List.String(),
|
||||
Per: rate.LimitPerAuthToken,
|
||||
Unlimited: false,
|
||||
MaxRequests: 10,
|
||||
Period: time.Minute,
|
||||
},
|
||||
|
||||
@@ -6,7 +6,7 @@ package ratelimit
|
||||
import "github.com/hashicorp/go-rate"
|
||||
|
||||
// NewLimiter creates a rate.Limiter.
|
||||
func NewLimiter(limits []*rate.Limit, maxEntries int) (*rate.Limiter, error) {
|
||||
func NewLimiter(limits []rate.Limit, maxEntries int) (*rate.Limiter, error) {
|
||||
return rate.NewLimiter(
|
||||
limits,
|
||||
maxEntries,
|
||||
|
||||
Reference in new issue
Block a user