96 lines
4.5 KiB
Go
96 lines
4.5 KiB
Go
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
|
|
}
|