// Command usacorunner compiles and runs solutions against their tests, and // lints them, for the USACO site's solve page (internal/runner). It runs // untrusted code, so it is its own service: the site holds secrets and // this holds none, and in production its container has no outside network, // a read-only root, an exec-able tmpfs at /tmp and tight memory, CPU and // pids limits (Dockerfile.runner lists them). Only the site talks to it. // // It reads PORT (default 8081) and MAX_PARALLEL (default 2): how many runs // and lints may be in progress at once. More only makes each slower, and // a timing that depends on the neighbours is no use for a time limit. package main import ( "log" "net/http" "os" "sort" "strconv" "strings" "time" "adam-french.co.uk/usaco-site/internal/runner" ) func main() { maxParallel := 2 if v := os.Getenv("MAX_PARALLEL"); v != "" { n, err := strconv.Atoi(v) if err != nil || n < 1 { log.Fatalf("MAX_PARALLEL=%q is not a positive number", v) } maxParallel = n } // The compilers are what the image is for: without g++ or python3 the // runner still answers, but every run in that language fails to build. var have, missing []string for bin, ok := range runner.Tools() { if ok { have = append(have, bin) } else { missing = append(missing, bin) } } sort.Strings(have) sort.Strings(missing) log.Printf("[runner] tools: %s", strings.Join(have, ", ")) if len(missing) > 0 { log.Printf("[runner] not installed: %s", strings.Join(missing, ", ")) } addr := ":" + getenv("PORT", "8081") srv := &http.Server{ Addr: addr, Handler: runner.Handler(maxParallel), ReadHeaderTimeout: 10 * time.Second, // Above the runner's own 60s budget per request, which is what // actually bounds a response. WriteTimeout: 75 * time.Second, } log.Printf("listening on %s, %d at a time", addr, maxParallel) log.Fatal(srv.ListenAndServe()) } func getenv(key, def string) string { if v := os.Getenv(key); v != "" { return v } return def }