mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-10-06 22:41:56 +02:00
reduce lock scope
This commit is contained in:
1 parent
57d025910d
commit
553a229fd3
1 file changed
+17
-13
@@ -575,10 +575,9 @@ func (mq *MaintenanceQueue) getRepeatPreventionInterval(taskType MaintenanceTask
|
||||
|
||||
// GetTasks returns tasks with optional filtering
|
||||
func (mq *MaintenanceQueue) GetTasks(status MaintenanceTaskStatus, taskType MaintenanceTaskType, limit int) []*MaintenanceTask {
|
||||
// Create a copy of task slice while holding the lock for minimal time
|
||||
mq.mutex.RLock()
|
||||
defer mq.mutex.RUnlock()
|
||||
|
||||
var tasks []*MaintenanceTask
|
||||
tasksCopy := make([]*MaintenanceTask, 0, len(mq.tasks))
|
||||
for _, task := range mq.tasks {
|
||||
if status != "" && task.Status != status {
|
||||
continue
|
||||
@@ -586,29 +585,34 @@ func (mq *MaintenanceQueue) GetTasks(status MaintenanceTaskStatus, taskType Main
|
||||
if taskType != "" && task.Type != taskType {
|
||||
continue
|
||||
}
|
||||
tasks = append(tasks, task)
|
||||
if limit > 0 && len(tasks) >= limit {
|
||||
// Create a shallow copy to avoid data races
|
||||
taskCopy := *task
|
||||
tasksCopy = append(tasksCopy, &taskCopy)
|
||||
if limit > 0 && len(tasksCopy) >= limit {
|
||||
break
|
||||
}
|
||||
}
|
||||
mq.mutex.RUnlock()
|
||||
|
||||
// Sort by creation time (newest first)
|
||||
sort.Slice(tasks, func(i, j int) bool {
|
||||
return tasks[i].CreatedAt.After(tasks[j].CreatedAt)
|
||||
// Sort after releasing the lock to prevent deadlocks
|
||||
sort.Slice(tasksCopy, func(i, j int) bool {
|
||||
return tasksCopy[i].CreatedAt.After(tasksCopy[j].CreatedAt)
|
||||
})
|
||||
|
||||
return tasks
|
||||
return tasksCopy
|
||||
}
|
||||
|
||||
// GetWorkers returns all registered workers
|
||||
func (mq *MaintenanceQueue) GetWorkers() []*MaintenanceWorker {
|
||||
mq.mutex.RLock()
|
||||
defer mq.mutex.RUnlock()
|
||||
|
||||
var workers []*MaintenanceWorker
|
||||
workers := make([]*MaintenanceWorker, 0, len(mq.workers))
|
||||
for _, worker := range mq.workers {
|
||||
workers = append(workers, worker)
|
||||
// Create a shallow copy to avoid data races
|
||||
workerCopy := *worker
|
||||
workers = append(workers, &workerCopy)
|
||||
}
|
||||
mq.mutex.RUnlock()
|
||||
|
||||
return workers
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user