Expert guide for a Go-based distributed task scheduling system with Lyapunov optimization algorithms, network topology management, and RESTful APIs.
Expert guidance for working with a Go backend system implementing Lyapunov-based distributed task scheduling across network devices.
This is a Go backend for network device management and task scheduling, implementing a Lyapunov drift-plus-penalty resource allocation algorithm. The system manages network topology, tasks, and real-time scheduling with performance metrics tracking.
**Install dependencies and prepare the environment:**
```bash
go mod tidy
```
**Run the application:**
```bash
go run cmd/server/main.go
air
go build -o ./tmp/main.exe ./cmd/server
```
**Access Swagger documentation:**
**Algorithm Engine (`internal/algorithm/`):**
The system has four core components:
1. **System** (`system.go`): Singleton orchestrator
- Loads network topology from database
- Runs scheduling loop (1 second per time slot)
- Coordinates Graph, TaskManager, and State
2. **Graph** (`graph.go`): Network topology
- Floyd-Warshall for shortest paths
- Runs once at initialization (static topology)
- Computes routes for task data transfer
3. **TaskManager** (`task_manager.go`): Task lifecycle
- Tracks submission, execution, completion
- Maintains `Tasks` (ID→Task) and `UserTasks` (UserID→TaskIDs)
- Filters active tasks by status
4. **State** (`state.go`): Scheduling snapshots
- Contains `TaskSnapshots` for current time slot
- Computes system metrics (delay, energy, queues)
- Used for optimization cost calculation
**Scheduling Flow:**
```
User submits task → TaskManager → System loop starts →
Each time slot: Create State → Graph.schedule() finds best assignment →
Update Task.MetricsHistory → Check completion → Next slot
```
**Data Model Separation (CRITICAL):**
**When adding new endpoints:**
1. Define handler in `internal/api/handlers/`:
```go
// @Summary Brief description
// @Tags category
// @Param name path type true "description"
// @Success 200 {object} ResponseType
// @Router /api/v1/endpoint [method]
func HandlerFunc(c *gin.Context) {
// Implementation
}
```
2. Register route in `internal/api/routes.go`
3. Regenerate Swagger docs:
```bash
swag init -g cmd/server/main.go -o ./docs
```
4. Test with Swagger UI or curl with JWT:
```bash
curl -H "Authorization: Bearer <token>" http://localhost:8080/api/v1/...
```
**Authentication:** All `/api/v1/*` routes (except `/auth/login`) require JWT bearer token.
**Constants (`internal/algorithm/constant/`):**
All algorithm parameters must be defined here:
**Scheduling Cost Function:**
```
Cost = Drift + V × Penalty
Penalty = weighted sum of (delay + energy + load)
```
**Performance Metrics:**
**The algorithm runs in a goroutine. Always use locks:**
```go
s.mutex.Lock() // Write operations
defer s.mutex.Unlock()
s.mutex.RLock() // Read operations
defer s.mutex.RUnlock()
```
**Race condition hotspots:**
```bash
go test ./...
go test -cover ./...
go test ./internal/algorithm/...
go test -race ./...
```
**Test patterns:**
**Schema changes:**
1. Update model in `internal/models/`
2. GORM auto-migration runs in `pkg/database/connection.go`
3. Test with fresh `data.db`
**Note:** No formal migration system. SQLite database stored at `data.db`.
Edit `configs/config.yaml` for:
**Security:** Change JWT secret from default in production!
**Known Limitations:**
1. No dynamic topology updates (Floyd-Warshall runs once at init)
2. Deep copy overhead in State (optimization opportunity)
3. No task priorities or SLA/deadline support
4. No task cancellation for running tasks
5. No automatic retry mechanism for failed tasks
**Task Status Lifecycle:**
```
TaskPending → TaskQueued → TaskComputing → TaskCompleted
↓
TaskFailed
```
Transitions happen in `TaskManager.syncFromState()` based on queue/processing state.
**Algorithm not running?**
**Scheduling too slow?**
**Task stuck in pending?**
**From project conventions:**
**Naming:**
**Error Handling:**
Return errors up the stack. Log at handling point, not every return:
```go
if _, exists := s.UserMap[req.UserID]; !exists {
return nil, fmt.Errorf("用户不存在: %d", req.UserID)
}
```
**API Prefix:** `/api/v1/`
**Auth:** JWT Bearer token (except `/auth/login`)
**Swagger:** `http://localhost:8080/swagger/index.html`
**Database:** SQLite (`data.db`)
**Hot Reload:** `air` command
Leave a review
No reviews yet. Be the first to review this skill!
# Download SKILL.md from killerskills.ai/api/skills/go-backend-developer-network-scheduling/raw