Refactor runner and API components to remove IP address handling. Update client and server logic to streamline runner registration and task distribution. Introduce write mutexes for connection management to enhance concurrency control. Clean up whitespace and improve code readability across multiple files.

This commit is contained in:
2025-11-24 22:58:56 -06:00
parent 3217bbfe4d
commit a53ea4dce7
9 changed files with 133 additions and 67 deletions

View File

@@ -38,6 +38,9 @@ type Server struct {
wsUpgrader websocket.Upgrader
runnerConns map[int64]*websocket.Conn
runnerConnsMu sync.RWMutex
// Mutexes for each runner connection to serialize writes
runnerConnsWriteMu map[int64]*sync.Mutex
runnerConnsWriteMuMu sync.RWMutex
frontendConns map[string]*websocket.Conn // key: "jobId:taskId"
frontendConnsMu sync.RWMutex
// Mutexes for each frontend connection to serialize writes
@@ -55,6 +58,8 @@ type Server struct {
// Throttling for progress updates (per job)
progressUpdateTimes map[int64]time.Time // key: jobID
progressUpdateTimesMu sync.RWMutex
// Task distribution serialization
taskDistMu sync.Mutex // Mutex to prevent concurrent distribution
}
// NewServer creates a new API server
@@ -78,6 +83,7 @@ func NewServer(db *database.DB, auth *authpkg.Auth, storage *storage.Storage) (*
WriteBufferSize: 1024,
},
runnerConns: make(map[int64]*websocket.Conn),
runnerConnsWriteMu: make(map[int64]*sync.Mutex),
frontendConns: make(map[string]*websocket.Conn),
frontendConnsWriteMu: make(map[string]*sync.Mutex),
jobListConns: make(map[int64]*websocket.Conn),
@@ -611,7 +617,7 @@ func (s *Server) recoverStuckTasks() {
go func() {
for range distributeTicker.C {
s.distributeTasksToRunners()
s.triggerTaskDistribution()
}
}()
@@ -675,7 +681,7 @@ func (s *Server) recoverStuckTasks() {
s.recoverTaskTimeouts()
// Distribute newly recovered tasks
s.distributeTasksToRunners()
s.triggerTaskDistribution()
}()
}
}