实现请求连接数等限制

This commit is contained in:
刘祥超
2021-12-12 11:46:23 +08:00
parent 01ea13d283
commit 72b94c5035
5 changed files with 97 additions and 1 deletions

View File

@@ -422,6 +422,18 @@ func (this *HTTPWebDAO) ComposeWebConfig(tx *dbs.Tx, webId int64, cacheMap *util
// mergeSlashes
config.MergeSlashes = web.MergeSlashes == 1
// 请求限制
if len(web.RequestLimit) > 0 {
var requestLimitConfig = &serverconfigs.HTTPRequestLimitConfig{}
if len(web.RequestLimit) > 0 {
err = json.Unmarshal([]byte(web.RequestLimit), requestLimitConfig)
if err != nil {
return nil, err
}
config.RequestLimit = requestLimitConfig
}
}
if cacheMap != nil {
cacheMap.Put(cacheKey, config)
}
@@ -1070,6 +1082,43 @@ func (this *HTTPWebDAO) UpdateWebCommon(tx *dbs.Tx, webId int64, mergeSlashes bo
return this.NotifyUpdate(tx, webId)
}
// UpdateWebRequestLimit 修改服务的请求限制
func (this *HTTPWebDAO) UpdateWebRequestLimit(tx *dbs.Tx, webId int64, config *serverconfigs.HTTPRequestLimitConfig) error {
configJSON, err := json.Marshal(config)
if err != nil {
return err
}
err = this.Query(tx).
Pk(webId).
Set("requestLimit", configJSON).
UpdateQuickly()
if err != nil {
return err
}
return this.NotifyUpdate(tx, webId)
}
// FindWebRequestLimit 获取服务的请求限制
func (this *HTTPWebDAO) FindWebRequestLimit(tx *dbs.Tx, webId int64) (*serverconfigs.HTTPRequestLimitConfig, error) {
configString, err := this.Query(tx).
Pk(webId).
Result("requestLimit").
FindStringCol("")
if err != nil {
return nil, err
}
var config = &serverconfigs.HTTPRequestLimitConfig{}
if len(configString) == 0 {
return config, nil
}
err = json.Unmarshal([]byte(configString), config)
if err != nil {
return nil, err
}
return config, nil
}
// NotifyUpdate 通知更新
func (this *HTTPWebDAO) NotifyUpdate(tx *dbs.Tx, webId int64) error {
// server

View File

@@ -33,6 +33,7 @@ type HTTPWeb struct {
Webp string `field:"webp"` // WebP配置
RemoteAddr string `field:"remoteAddr"` // 客户端IP配置
MergeSlashes uint8 `field:"mergeSlashes"` // 是否合并路径中的斜杠
RequestLimit string `field:"requestLimit"` // 请求限制
}
type HTTPWebOperator struct {
@@ -67,6 +68,7 @@ type HTTPWebOperator struct {
Webp interface{} // WebP配置
RemoteAddr interface{} // 客户端IP配置
MergeSlashes interface{} // 是否合并路径中的斜杠
RequestLimit interface{} // 请求限制
}
func NewHTTPWebOperator() *HTTPWebOperator {

View File

@@ -2043,6 +2043,7 @@ func (this *ServerDAO) UpdateServerTrafficLimitConfig(tx *dbs.Tx, serverId int64
return this.UpdateServerTrafficLimitStatus(tx, trafficLimitConfig, serverId, true)
}
// UpdateServerTrafficLimitStatus 修改服务的流量限制状态
func (this *ServerDAO) UpdateServerTrafficLimitStatus(tx *dbs.Tx, trafficLimitConfig *serverconfigs.TrafficLimitConfig, serverId int64, isUpdatingConfig bool) error {
if !trafficLimitConfig.IsOn {
if isUpdatingConfig {