Markdown Converter
Agent skill for markdown-converter
This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
Sign in to like and favorite skills
This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
A Go backend system for network device management and task scheduling, implementing a Lyapunov-based resource allocation algorithm for distributed task scheduling across communication devices. The system manages network topology, tasks, and real-time scheduling with performance metrics tracking.
# Install dependencies go mod tidy # Run the application go run cmd/server/main.go # Build binary go build -o ./tmp/main.exe ./cmd/server # Hot reload with Air (recommended for development) air
# Generate Swagger docs (run after API changes) swag init -g cmd/server/main.go -o ./docs # Access Swagger UI # http://localhost:8080/swagger/index.html
# Run all tests go test ./... # Run tests with coverage go test -cover ./... # Run tests for specific package go test ./internal/algorithm/...
internal/algorithm/)The heart of the system is a Lyapunov drift-plus-penalty based task scheduling algorithm that operates in discrete time slots.
Key Components:
System (
system.go): Singleton managing the entire scheduling system
Graph (
graph.go): Network topology and routing
TaskManager (
task_manager.go): Task lifecycle management
Tasks (ID→Task), UserTasks (UserID→TaskIDs)State (
state.go): Scheduling state snapshots
TaskSnapshots for all active tasks in current time slotData Flow:
User submits task → TaskManager → System starts loop → Each time slot: Create State → Graph.schedule() iterates to find best assignment → Update Task.MetricsHistory → Check completion → Next slot
Scheduling Logic:
Drift + V × Penalty where penalty = weighted sum of delay, energy, loadinternal/algorithm/define/)Critical Design Pattern:
MetricsHistory[], status, assignment)internal/api/)Standard layered architecture:
All endpoints under
/api/v1/ with JWT authentication (Authorization: Bearer <token>)
internal/repository/): Data access abstractioninternal/service/): Business logic layerinternal/models/): Database models (GORM)data.db)The algorithm runs in a goroutine with ticker-based scheduling. Always use:
s.mutex.Lock() // Write operations s.mutex.RLock() // Read operations
Race Condition Risks:
TaskManager.Tasks accessed from multiple goroutinesSystem.CurrentState read by API handlers during schedulinginternal/algorithm/constant/)All magic numbers must be defined here:
Slot: Time slot duration (seconds)Iters: Max scheduling iterations per time slotRho, C, Kappa: Task computation parametersV: Lyapunov penalty weightMetrics are computed at TaskSnapshot level then aggregated:
Transfer delay:
delay = Σ(data_transferred / link_speed) for each path segment
Compute delay:
delay = processed_data × Rho / (resource_fraction × C)
Energy: Similar formulas with power consumption parameters
Always update
Task.MetricsHistory after each time slot via TaskManager.syncFromState()
TaskPending → TaskQueued → TaskComputing → TaskCompleted ↓ TaskFailed
Status transitions happen in
TaskManager.syncFromState() based on queue and processing state.
From
.github/copilot-instructions.md:
NewSystem, TaskManager)addTask, syncFromState)Always return errors up the stack. Log at the point of handling, not at every return:
func (s *System) SubmitTask(req TaskBase) (*Task, error) { if _, exists := s.UserMap[req.UserID]; !exists { return nil, fmt.Errorf("用户不存在: %d", req.UserID) // Clear error message } // ... }
From
improvement.md:
When writing tests:
go test -raceWhen adding new endpoints:
internal/api/handlers/internal/api/routes.go// @Summary, // @Param, etc.)swag init to regenerate docsAuthentication: All
/api/v1/* routes (except /auth/login) require JWT:
curl -H "Authorization: Bearer <token>" http://localhost:8080/api/v1/...
Currently no formal migration system. Schema changes require:
internal/models/pkg/database/connection.godata.dbEdit
configs/config.yaml for:
JWT Secret: Change from default in production!
Algorithm not running?
System.IsRunning in /api/v1/algorithm/info/api/v1/algorithm/tasksScheduling slow?
constant.Iters (fewer random trials per slot)Task stuck in pending?
System.UserMapSystem.Graph initialized)See docs/architecture.md for detailed system architecture, data flow diagrams, and API endpoint listing.