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() {
// Load configuration
cfg := config.LoadApi("config/server.yaml")
cfg := config.LoadApi("config/api.yaml")
if cfg.App.Env == "release" {
gin.SetMode(gin.ReleaseMode)

View File

@@ -4,11 +4,13 @@ storage:
dsn: "host=localhost port=54320 user=postgres password=password dbname=postgres sslmode=disable"
limit:
time: 10
cpu: 1
memory: 512
time: 10.0 # s
cpu: 1.0
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
Memory int
Time float32
Size int
}
type WorkerConfig struct {
@@ -28,6 +29,7 @@ func LoadWorker(configFile string) *WorkerConfig {
v.SetDefault("limit.cpu", 1.0)
v.SetDefault("limit.time", 10.0)
v.SetDefault("limit.memory", 512*1024)
v.SetDefault("limit.size", 1024)
v.SetDefault("process", 1)
v.SetDefault("name", "default name")
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
if logData, err := os.ReadFile(filepath.Join(tmpDir, "compile.txt")); err == nil {
logData = logData[:min(len(logData), cfg.Limit.Size)]
task.CompileLog = string(logData)
}
@@ -144,11 +145,13 @@ func runCpp(ctx context.Context, task *model.Paste, cli *client.Client, tmpDir s
usagePath := filepath.Join(tmpDir, "usage.json")
// Read program output
if outData, err := os.ReadFile(stdoutPath); err == nil {
task.Stdout = string(outData)
if stdoutData, err := os.ReadFile(stdoutPath); err == nil {
stdoutData = stdoutData[:min(len(stdoutData), cfg.Limit.Size)]
task.Stdout = string(stdoutData)
}
if outData, err := os.ReadFile(stderrPath); err == nil {
task.Stderr = string(outData)
if stderrData, err := os.ReadFile(stderrPath); err == nil {
stderrData = stderrData[:min(len(stderrData), cfg.Limit.Size)]
task.Stderr = string(stderrData)
}
if usageData, err := os.ReadFile(usagePath); err == nil {
var usage Usage
@@ -161,7 +164,7 @@ func runCpp(ctx context.Context, task *model.Paste, cli *client.Client, tmpDir s
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 {
return fmt.Errorf("create temp dir error: %v", err)
}