mirror of
https://github.com/criyle/go-judge.git
synced 2025-09-26 22:39:12 +08:00

Some checks failed
Build / Goreleaser (push) Has been cancelled
Build / Upload artifacts-${{ matrix.os }}-${{ matrix.arch }} (amd64_v3, darwin) (push) Has been cancelled
Build / Upload artifacts-${{ matrix.os }}-${{ matrix.arch }} (amd64_v3, linux) (push) Has been cancelled
Build / Upload artifacts-${{ matrix.os }}-${{ matrix.arch }} (amd64_v3, windows) (push) Has been cancelled
Build / Upload artifacts-${{ matrix.os }}-${{ matrix.arch }} (arm64_v8.0, darwin) (push) Has been cancelled
Build / Upload artifacts-${{ matrix.os }}-${{ matrix.arch }} (arm64_v8.0, linux) (push) Has been cancelled
Build / Upload artifacts-${{ matrix.os }}-${{ matrix.arch }} (arm64_v8.0, windows) (push) Has been cancelled
55 lines
905 B
Go
55 lines
905 B
Go
package worker
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"github.com/criyle/go-judge/envexec"
|
|
)
|
|
|
|
// default tick interval 100 ms
|
|
const defaultTickInterval = 100 * time.Millisecond
|
|
|
|
type waiter struct {
|
|
tickInterval time.Duration
|
|
timeLimit time.Duration
|
|
clockTimeLimit time.Duration
|
|
}
|
|
|
|
func (w *waiter) Wait(ctx context.Context, u envexec.Process) bool {
|
|
clockTimeLimit := w.clockTimeLimit
|
|
timeLimit := w.timeLimit
|
|
if clockTimeLimit == 0 {
|
|
clockTimeLimit = timeLimit
|
|
}
|
|
|
|
start := time.Now()
|
|
|
|
tickInterval := w.tickInterval
|
|
if tickInterval == 0 {
|
|
tickInterval = defaultTickInterval
|
|
}
|
|
|
|
ticker := time.NewTicker(tickInterval)
|
|
defer ticker.Stop()
|
|
|
|
for {
|
|
select {
|
|
case <-ctx.Done():
|
|
return false
|
|
|
|
case <-u.Done():
|
|
return false
|
|
|
|
case <-ticker.C:
|
|
if time.Since(start) > clockTimeLimit {
|
|
return true
|
|
}
|
|
u := u.Usage()
|
|
if u.Time > timeLimit {
|
|
return true
|
|
}
|
|
}
|
|
}
|
|
}
|