fix(scheduler): add boundary validation and safety limits to worker count and queue size to prevent OOM
This commit is contained in:
@@ -293,6 +293,24 @@ func (sc *SettingsController) UpdateSchedulerSettings(c *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var workerCount int
|
||||||
|
if _, err := fmt.Sscanf(req.WorkerCount, "%d", &workerCount); err != nil || workerCount < 1 || workerCount > 1000 {
|
||||||
|
utils.BadRequest(c, "工作线程数必须在 1 至 1000 之间")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var queueSize int
|
||||||
|
if _, err := fmt.Sscanf(req.QueueSize, "%d", &queueSize); err != nil || queueSize < 1 || queueSize > 50000 {
|
||||||
|
utils.BadRequest(c, "等待队列容量必须在 1 至 50000 之间")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var rateInterval int
|
||||||
|
if _, err := fmt.Sscanf(req.RateInterval, "%d", &rateInterval); err != nil || rateInterval < 1 {
|
||||||
|
utils.BadRequest(c, "限频间隔必须为正整数")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
values := map[string]string{
|
values := map[string]string{
|
||||||
constant.KeyWorkerCount: req.WorkerCount,
|
constant.KeyWorkerCount: req.WorkerCount,
|
||||||
constant.KeyQueueSize: req.QueueSize,
|
constant.KeyQueueSize: req.QueueSize,
|
||||||
|
|||||||
@@ -208,9 +208,15 @@ func NewScheduler(config SchedulerConfig, handler SchedulerEventHandler) *Schedu
|
|||||||
if config.WorkerCount <= 0 {
|
if config.WorkerCount <= 0 {
|
||||||
config.WorkerCount = 4
|
config.WorkerCount = 4
|
||||||
}
|
}
|
||||||
|
if config.WorkerCount > 1000 {
|
||||||
|
config.WorkerCount = 1000
|
||||||
|
}
|
||||||
if config.QueueSize <= 0 {
|
if config.QueueSize <= 0 {
|
||||||
config.QueueSize = 100
|
config.QueueSize = 100
|
||||||
}
|
}
|
||||||
|
if config.QueueSize > 50000 {
|
||||||
|
config.QueueSize = 50000
|
||||||
|
}
|
||||||
if config.RateInterval <= 0 {
|
if config.RateInterval <= 0 {
|
||||||
config.RateInterval = 200 * time.Millisecond
|
config.RateInterval = 200 * time.Millisecond
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user