feat(practice): queue practice runs with one running and ten waiting

This commit is contained in:
shira committed 2026-10-11 15:01:07 +03:00
1 parent 51770eea6c
commit 754c745fba
40 files changed
+3040 -70

No files matched your search

+7
View File
@@ -162,6 +162,8 @@ type Config struct {
// CodeRunner configures the client of the own code runner. It is disabled
// unless CODE_RUNNER_URL is set.
CodeRunner CodeRunnerSettings
// Practice configures the debug run limits and the practice queue worker.
Practice PracticeSettings
// TrustedProxyCIDRs are the reverse proxies in front of the API. The
// client address is taken from X-Forwarded-For only through them (see
@@ -267,6 +269,11 @@ func (config *Config) loadIdentityAndProxySettings(lookup LookupEnv) error {
return err
}
config.CodeRunner = codeRunner
practice, err := LoadPracticeSettings(lookup, codeRunner.RequestTimeout)
if err != nil {
return err
}
config.Practice = practice
return config.loadTrustedProxies(lookup)
}
+95
View File
@@ -0,0 +1,95 @@
package config
import (
"fmt"
"time"
)
const (
// Debug runs of one candidate are a technical abuse brake on the shared
// sandbox, not a product quota: a few runs a minute and a working
// session's worth an hour.
defaultPracticeRunsPerMinute = 6
defaultPracticeRunsPerHour = 60
maxPracticeRunsPerMinute = 600
maxPracticeRunsPerHour = 36000
// defaultPracticePollInterval is the wait between two reads of a running
// run and after a pass that found nothing to run. It is also the usual
// delay before a queued run starts.
defaultPracticePollInterval = time.Second
minPracticePollInterval = 10 * time.Millisecond
maxPracticePollInterval = 30 * time.Second
// defaultPracticeJobLease is how long a claimed practice job belongs to
// one worker without a renewal; every poll renews it. A worker that
// crashed delays its job by at most this long.
defaultPracticeJobLease = 30 * time.Second
maxPracticeJobLease = 10 * time.Minute
// defaultPracticeJobMaxAttempts bounds the failed tries and expired
// leases of a job before it fails without a verdict.
defaultPracticeJobMaxAttempts = 5
maxPracticeJobMaxAttempts = 100
)
// PracticeSettings configure code practice: the rate limit of debug runs and
// the practice queue worker. The queue limits (one running, ten queued) are
// product rules in the practice usecase, not settings.
type PracticeSettings struct {
// RunsPerMinute and RunsPerHour bound the debug runs of one candidate.
RunsPerMinute int64
RunsPerHour int64
// PollInterval is the wait between two reads of a running run and after
// an empty pass of the queue worker.
PollInterval time.Duration
// JobLease is how long a claimed job belongs to one worker without a
// renewal.
JobLease time.Duration
// JobMaxAttempts bounds the failed tries of a job.
JobMaxAttempts int
}
// LoadPracticeSettings reads the practice settings. requestTimeout is the
// code runner request timeout: a lease must outlast one runner call and one
// poll, or another worker could take a job over while its call is still
// running.
func LoadPracticeSettings(lookup LookupEnv, requestTimeout time.Duration) (PracticeSettings, error) {
settings := PracticeSettings{}
perMinute, err := intOrDefault(lookup, "PRACTICE_RUNS_PER_MINUTE", defaultPracticeRunsPerMinute)
if err != nil {
return PracticeSettings{}, err
}
perHour, err := intOrDefault(lookup, "PRACTICE_RUNS_PER_HOUR", defaultPracticeRunsPerHour)
if err != nil {
return PracticeSettings{}, err
}
settings.RunsPerMinute, settings.RunsPerHour = int64(perMinute), int64(perHour)
if settings.PollInterval, err = durationOrDefault(lookup, "PRACTICE_POLL_INTERVAL", defaultPracticePollInterval); err != nil {
return PracticeSettings{}, err
}
if settings.JobLease, err = durationOrDefault(lookup, "PRACTICE_JOB_LEASE", defaultPracticeJobLease); err != nil {
return PracticeSettings{}, err
}
if settings.JobMaxAttempts, err = intOrDefault(lookup, "PRACTICE_JOB_MAX_ATTEMPTS", defaultPracticeJobMaxAttempts); err != nil {
return PracticeSettings{}, err
}
return settings, settings.Validate(requestTimeout)
}
// Validate checks the ranges. They are checked even without a code runner, so
// enabling the runner later cannot surface an old mistake.
func (settings PracticeSettings) Validate(requestTimeout time.Duration) error {
const location = "PracticeSettings.Validate"
switch {
case settings.RunsPerMinute < 1 || settings.RunsPerMinute > maxPracticeRunsPerMinute:
return fmt.Errorf("%s - PRACTICE_RUNS_PER_MINUTE must be between 1 and %d", location, maxPracticeRunsPerMinute)
case settings.RunsPerHour < settings.RunsPerMinute || settings.RunsPerHour > maxPracticeRunsPerHour:
return fmt.Errorf("%s - PRACTICE_RUNS_PER_HOUR must be between PRACTICE_RUNS_PER_MINUTE and %d", location, maxPracticeRunsPerHour)
case settings.PollInterval < minPracticePollInterval || settings.PollInterval > maxPracticePollInterval:
return fmt.Errorf("%s - PRACTICE_POLL_INTERVAL must be between %s and %s", location, minPracticePollInterval, maxPracticePollInterval)
case settings.JobLease <= requestTimeout+settings.PollInterval || settings.JobLease > maxPracticeJobLease:
return fmt.Errorf("%s - PRACTICE_JOB_LEASE must exceed CODE_RUNNER_REQUEST_TIMEOUT plus PRACTICE_POLL_INTERVAL and be at most %s",
location, maxPracticeJobLease)
case settings.JobMaxAttempts < 1 || settings.JobMaxAttempts > maxPracticeJobMaxAttempts:
return fmt.Errorf("%s - PRACTICE_JOB_MAX_ATTEMPTS must be between 1 and %d", location, maxPracticeJobMaxAttempts)
}
return nil
}
+43
View File
@@ -0,0 +1,43 @@
package config
import (
"strings"
"testing"
"time"
)
func TestLoadFromEnvPracticeDefaults(t *testing.T) {
settings, err := LoadFromEnv(testEnvironment(nil))
if err != nil {
t.Fatalf("LoadFromEnv() error = %v", err)
}
want := PracticeSettings{RunsPerMinute: 6, RunsPerHour: 60, PollInterval: time.Second, JobLease: 30 * time.Second, JobMaxAttempts: 5}
if settings.Practice != want {
t.Fatalf("practice defaults = %+v, want %+v", settings.Practice, want)
}
}
func TestLoadFromEnvRejectsInvalidPracticeSettings(t *testing.T) {
cases := map[string]struct {
values map[string]string
message string
}{
"zero runs per minute": {map[string]string{"PRACTICE_RUNS_PER_MINUTE": "0"}, "PRACTICE_RUNS_PER_MINUTE"},
"hour below minute": {
map[string]string{"PRACTICE_RUNS_PER_MINUTE": "10", "PRACTICE_RUNS_PER_HOUR": "5"}, "PRACTICE_RUNS_PER_HOUR",
},
"poll too short": {map[string]string{"PRACTICE_POLL_INTERVAL": "1ms"}, "PRACTICE_POLL_INTERVAL"},
// The lease must outlast one runner request (10s by default) and a poll.
"lease within a request": {map[string]string{"PRACTICE_JOB_LEASE": "10s"}, "PRACTICE_JOB_LEASE"},
"no attempts": {map[string]string{"PRACTICE_JOB_MAX_ATTEMPTS": "0"}, "PRACTICE_JOB_MAX_ATTEMPTS"},
"malformed duration": {map[string]string{"PRACTICE_JOB_LEASE": "soon"}, "PRACTICE_JOB_LEASE"},
}
for name, testCase := range cases {
t.Run(name, func(t *testing.T) {
_, err := LoadFromEnv(testEnvironment(testCase.values))
if err == nil || !strings.Contains(err.Error(), testCase.message) {
t.Fatalf("LoadFromEnv() error = %v, want one naming %s", err, testCase.message)
}
})
}
}