Merge pull request #5 from dreamstarsky/dev

feat: output limit
This commit is contained in:
xkm
2025-05-02 22:57:33 +08:00
committed by GitHub
5 changed files with 19 additions and 12 deletions

View File

@@ -17,7 +17,7 @@ import (
func main() { func main() {
// Load configuration // Load configuration
cfg := config.LoadApi("config/server.yaml") cfg := config.LoadApi("config/api.yaml")
if cfg.App.Env == "release" { if cfg.App.Env == "release" {
gin.SetMode(gin.ReleaseMode) gin.SetMode(gin.ReleaseMode)

View File

@@ -4,11 +4,13 @@ storage:
dsn: "host=localhost port=54320 user=postgres password=password dbname=postgres sslmode=disable" dsn: "host=localhost port=54320 user=postgres password=password dbname=postgres sslmode=disable"
limit: limit:
time: 10 time: 10.0 # s
cpu: 1 cpu: 1.0
memory: 512 memory: 512 # MB
size: 1024 # KB
process: 4 process: 1
name: "猫猫" name: "default name"
compilerimage: "cpp_gcc-latest:latest"

View File

@@ -10,6 +10,7 @@ type LimitConfig struct {
Cpu float32 Cpu float32
Memory int Memory int
Time float32 Time float32
Size int
} }
type WorkerConfig struct { type WorkerConfig struct {
@@ -28,6 +29,7 @@ func LoadWorker(configFile string) *WorkerConfig {
v.SetDefault("limit.cpu", 1.0) v.SetDefault("limit.cpu", 1.0)
v.SetDefault("limit.time", 10.0) v.SetDefault("limit.time", 10.0)
v.SetDefault("limit.memory", 512*1024) v.SetDefault("limit.memory", 512*1024)
v.SetDefault("limit.size", 1024)
v.SetDefault("process", 1) v.SetDefault("process", 1)
v.SetDefault("name", "default name") v.SetDefault("name", "default name")
v.SetDefault("compilerimage", "cpp_gcc-latest:latest") v.SetDefault("compilerimage", "cpp_gcc-latest:latest")

View File

@@ -64,6 +64,7 @@ func compileCpp(ctx context.Context, task *model.Paste, cli *client.Client, tmpD
// Read compilation log // Read compilation log
if logData, err := os.ReadFile(filepath.Join(tmpDir, "compile.txt")); err == nil { if logData, err := os.ReadFile(filepath.Join(tmpDir, "compile.txt")); err == nil {
logData = logData[:min(len(logData), cfg.Limit.Size)]
task.CompileLog = string(logData) task.CompileLog = string(logData)
} }
@@ -144,24 +145,26 @@ func runCpp(ctx context.Context, task *model.Paste, cli *client.Client, tmpDir s
usagePath := filepath.Join(tmpDir, "usage.json") usagePath := filepath.Join(tmpDir, "usage.json")
// Read program output // Read program output
if outData, err := os.ReadFile(stdoutPath); err == nil { if stdoutData, err := os.ReadFile(stdoutPath); err == nil {
task.Stdout = string(outData) stdoutData = stdoutData[:min(len(stdoutData), cfg.Limit.Size)]
task.Stdout = string(stdoutData)
} }
if outData, err := os.ReadFile(stderrPath); err == nil { if stderrData, err := os.ReadFile(stderrPath); err == nil {
task.Stderr = string(outData) stderrData = stderrData[:min(len(stderrData), cfg.Limit.Size)]
task.Stderr = string(stderrData)
} }
if usageData, err := os.ReadFile(usagePath); err == nil { if usageData, err := os.ReadFile(usagePath); err == nil {
var usage Usage var usage Usage
json.Unmarshal(usageData, &usage) json.Unmarshal(usageData, &usage)
task.MemoryUsageKb = int(usage.MaxMemory) task.MemoryUsageKb = int(usage.MaxMemory)
task.ExecutionTimeMs = int(usage.RealTime * 1000) task.ExecutionTimeMs = int(usage.RealTime * 1000)
} }
return nil return nil
} }
func (w *Worker) RunCppTask(ctx context.Context, task *model.Paste, cli *client.Client) error { func (w *Worker) RunCppTask(ctx context.Context, task *model.Paste, cli *client.Client) error {
// 临时文件夹 // 临时文件夹
tmpDir, err := os.MkdirTemp("", "cpp_compile_") tmpDir, err := os.MkdirTemp("/dev/shm/", "cpp_compile_")
if err != nil { if err != nil {
return fmt.Errorf("create temp dir error: %v", err) return fmt.Errorf("create temp dir error: %v", err)
} }