实现请求连接数等限制

This commit is contained in:
GoEdgeLab
2021-12-12 11:46:23 +08:00
parent 1fc7304721
commit 40d93c5b09
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 // mergeSlashes
config.MergeSlashes = web.MergeSlashes == 1 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 { if cacheMap != nil {
cacheMap.Put(cacheKey, config) cacheMap.Put(cacheKey, config)
} }
@@ -1070,6 +1082,43 @@ func (this *HTTPWebDAO) UpdateWebCommon(tx *dbs.Tx, webId int64, mergeSlashes bo
return this.NotifyUpdate(tx, webId) 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 通知更新 // NotifyUpdate 通知更新
func (this *HTTPWebDAO) NotifyUpdate(tx *dbs.Tx, webId int64) error { func (this *HTTPWebDAO) NotifyUpdate(tx *dbs.Tx, webId int64) error {
// server // server

View File

@@ -33,6 +33,7 @@ type HTTPWeb struct {
Webp string `field:"webp"` // WebP配置 Webp string `field:"webp"` // WebP配置
RemoteAddr string `field:"remoteAddr"` // 客户端IP配置 RemoteAddr string `field:"remoteAddr"` // 客户端IP配置
MergeSlashes uint8 `field:"mergeSlashes"` // 是否合并路径中的斜杠 MergeSlashes uint8 `field:"mergeSlashes"` // 是否合并路径中的斜杠
RequestLimit string `field:"requestLimit"` // 请求限制
} }
type HTTPWebOperator struct { type HTTPWebOperator struct {
@@ -67,6 +68,7 @@ type HTTPWebOperator struct {
Webp interface{} // WebP配置 Webp interface{} // WebP配置
RemoteAddr interface{} // 客户端IP配置 RemoteAddr interface{} // 客户端IP配置
MergeSlashes interface{} // 是否合并路径中的斜杠 MergeSlashes interface{} // 是否合并路径中的斜杠
RequestLimit interface{} // 请求限制
} }
func NewHTTPWebOperator() *HTTPWebOperator { 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) return this.UpdateServerTrafficLimitStatus(tx, trafficLimitConfig, serverId, true)
} }
// UpdateServerTrafficLimitStatus 修改服务的流量限制状态
func (this *ServerDAO) UpdateServerTrafficLimitStatus(tx *dbs.Tx, trafficLimitConfig *serverconfigs.TrafficLimitConfig, serverId int64, isUpdatingConfig bool) error { func (this *ServerDAO) UpdateServerTrafficLimitStatus(tx *dbs.Tx, trafficLimitConfig *serverconfigs.TrafficLimitConfig, serverId int64, isUpdatingConfig bool) error {
if !trafficLimitConfig.IsOn { if !trafficLimitConfig.IsOn {
if isUpdatingConfig { if isUpdatingConfig {

View File

@@ -660,3 +660,47 @@ func (this *HTTPWebService) UpdateHTTPWebCommon(ctx context.Context, req *pb.Upd
return this.Success() return this.Success()
} }
// UpdateHTTPWebRequestLimit 修改请求限制
func (this *HTTPWebService) UpdateHTTPWebRequestLimit(ctx context.Context, req *pb.UpdateHTTPWebRequestLimitRequest) (*pb.RPCSuccess, error) {
_, err := this.ValidateAdmin(ctx, 0)
if err != nil {
return nil, err
}
var tx = this.NullTx()
var config = &serverconfigs.HTTPRequestLimitConfig{}
err = json.Unmarshal(req.RequestLimitJSON, config)
if err != nil {
return nil, err
}
err = models.SharedHTTPWebDAO.UpdateWebRequestLimit(tx, req.HttpWebId, config)
if err != nil {
return nil, err
}
return this.Success()
}
// FindHTTPWebRequestLimit 查找请求限制
func (this *HTTPWebService) FindHTTPWebRequestLimit(ctx context.Context, req *pb.FindHTTPWebRequestLimitRequest) (*pb.FindHTTPWebRequestLimitResponse, error) {
_, err := this.ValidateAdmin(ctx, 0)
if err != nil {
return nil, err
}
var tx = this.NullTx()
config, err := models.SharedHTTPWebDAO.FindWebRequestLimit(tx, req.HttpWebId)
if err != nil {
return nil, err
}
configJSON, err := json.Marshal(config)
if err != nil {
return nil, err
}
return &pb.FindHTTPWebRequestLimitResponse{RequestLimitJSON: configJSON}, nil
}

File diff suppressed because one or more lines are too long