Skip to content

Commit

Permalink
feat: independent limiter for user/inbound
Browse files Browse the repository at this point in the history
  • Loading branch information
zakuwaki committed Jul 24, 2023
1 parent ba933c4 commit cd66bc8
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 31 deletions.
16 changes: 11 additions & 5 deletions docs/configuration/limiter/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@
"user-a",
"user-b"
],
"auth_user_independent": false,
"inbound": [
"in-a",
"in-b"
]
],
"inbound_independent": false
}
]
}
Expand All @@ -39,12 +41,16 @@ The tag of the limiter, used in route rule.

#### auth_user

Global limiter for a group of usernames, see each inbound for details.
Apply limiter for a group of usernames, see each inbound for details.

#### auth_user_independent

Make each auth_user's limiter independent. If disabled, the same limiter will be shared.

#### inbound

Global limiter for a group of inbounds.
Apply limiter for a group of inbounds.

!!! info ""
#### inbound_independent

All the auth_users, inbounds and route rule with limiter tag share the same limiter. To take effect independently, configure limiters seperately.
Make each inbound's limiter independent. If disabled, the same limiter will be shared.
16 changes: 11 additions & 5 deletions docs/configuration/limiter/index.zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@
"user-a",
"user-b"
],
"auth_user_independent": false,
"inbound": [
"in-a",
"in-b"
]
],
"inbound_independent": false
}
]
}
Expand All @@ -39,12 +41,16 @@

#### auth_user

用户组全局限速,参阅入站设置。
用户组限速,参阅入站设置。

#### auth_user_independent

使每个用户有单独的限速。关闭时将共享限速。

#### inbound

入站组全局限速
入站组限速

!!! info ""
#### inbound_independent

所有用户、入站和有限速标签的路由规则共享同一个限速。为了独立生效,请分别配置限速器
使每个入站有单独的限速。关闭时将共享限速
35 changes: 19 additions & 16 deletions limiter/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,26 +59,29 @@ func (m *defaultManager) createLimiter(ctx context.Context, option option.Limite
if download == 0 && upload == 0 {
return E.New("download/upload, at least one must be set")
}
l := newLimiter(download, upload)
valid := false
if len(option.Tag) > 0 {
valid = true
m.mp[limiterKey{prefixTag, option.Tag}] = l
if option.Tag == "" && len(option.AuthUser) == 0 && len(option.Inbound) == 0 {
return E.New("tag/user/inbound, at least one must be set")
}
if len(option.AuthUser) > 0 {
valid = true
for _, user := range option.AuthUser {
m.mp[limiterKey{prefixUser, user}] = l
}
var sharedLimiter *limiter
if option.Tag != "" || !option.AuthUserIndependent || !option.InboundIndependent {
sharedLimiter = newLimiter(download, upload)
}
if len(option.Inbound) > 0 {
valid = true
for _, inbound := range option.Inbound {
m.mp[limiterKey{prefixInbound, inbound}] = l
if option.Tag != "" {
m.mp[limiterKey{prefixTag, option.Tag}] = sharedLimiter
}
for _, user := range option.AuthUser {
if option.AuthUserIndependent {
m.mp[limiterKey{prefixUser, user}] = newLimiter(download, upload)
} else {
m.mp[limiterKey{prefixUser, user}] = sharedLimiter
}
}
if !valid {
return E.New("tag/user/inbound, at least one must be set")
for _, inbound := range option.Inbound {
if option.InboundIndependent {
m.mp[limiterKey{prefixInbound, inbound}] = newLimiter(download, upload)
} else {
m.mp[limiterKey{prefixInbound, inbound}] = sharedLimiter
}
}
return
}
Expand Down
12 changes: 7 additions & 5 deletions option/limiter.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package option

type Limiter struct {
Tag string `json:"tag"`
Download string `json:"download,omitempty"`
Upload string `json:"upload,omitempty"`
AuthUser Listable[string] `json:"auth_user,omitempty"`
Inbound Listable[string] `json:"inbound,omitempty"`
Tag string `json:"tag"`
Download string `json:"download,omitempty"`
Upload string `json:"upload,omitempty"`
AuthUser Listable[string] `json:"auth_user,omitempty"`
AuthUserIndependent bool `json:"auth_user_independent,omitempty"`
Inbound Listable[string] `json:"inbound,omitempty"`
InboundIndependent bool `json:"inbound_independent,omitempty"`
}

0 comments on commit cd66bc8

Please sign in to comment.