mirror of
https://github.com/kopia/kopia.git
synced 2026-03-12 19:26:25 -04:00
Globally replaced all use of time with internal 'clock' package which provides indirection to time.Now() Added support for faking clock in Kopia via KOPIA_FAKE_CLOCK_ENDPOINT logfile: squelch annoying log message testenv: added faketimeserver which serves time over HTTP testing: added endurance test which tests kopia over long time scale This creates kopia repository and simulates usage of Kopia over multiple months (using accelerated fake time) to trigger effects that are only visible after long time passage (maintenance, compactions, expirations). The test is not used part of any test suite yet but will run in post-submit mode only, preferably 24/7. testing: refactored internal/clock to only support injection when 'testing' build tag is present
71 lines
1.3 KiB
Go
71 lines
1.3 KiB
Go
package cli
|
|
|
|
import (
|
|
"net/http"
|
|
"sync"
|
|
"sync/atomic"
|
|
"time"
|
|
|
|
"github.com/kopia/kopia/internal/clock"
|
|
)
|
|
|
|
type serverWatchdog struct {
|
|
inFlightRequests int32
|
|
inner http.Handler
|
|
stop func()
|
|
interval time.Duration
|
|
|
|
mu sync.Mutex
|
|
deadline time.Time
|
|
}
|
|
|
|
func (d *serverWatchdog) getDeadline() time.Time {
|
|
d.mu.Lock()
|
|
defer d.mu.Unlock()
|
|
|
|
return d.deadline
|
|
}
|
|
|
|
func (d *serverWatchdog) setDeadline(t time.Time) {
|
|
d.mu.Lock()
|
|
defer d.mu.Unlock()
|
|
|
|
if t.After(d.deadline) {
|
|
d.deadline = t
|
|
}
|
|
}
|
|
|
|
func (d *serverWatchdog) Run() {
|
|
for clock.Now().Before(d.getDeadline()) || atomic.LoadInt32(&d.inFlightRequests) > 0 {
|
|
// sleep for no less than 1 second to avoid burning CPU
|
|
sleepTime := clock.Until(d.getDeadline())
|
|
if sleepTime < time.Second {
|
|
sleepTime = time.Second
|
|
}
|
|
|
|
time.Sleep(sleepTime)
|
|
}
|
|
|
|
d.stop()
|
|
}
|
|
|
|
func (d *serverWatchdog) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
atomic.AddInt32(&d.inFlightRequests, 1)
|
|
defer atomic.AddInt32(&d.inFlightRequests, -1)
|
|
|
|
d.setDeadline(clock.Now().Add(d.interval))
|
|
d.inner.ServeHTTP(w, r)
|
|
}
|
|
|
|
func startServerWatchdog(h http.Handler, interval time.Duration, stop func()) http.Handler {
|
|
w := &serverWatchdog{
|
|
inner: h,
|
|
interval: interval,
|
|
deadline: clock.Now().Add(interval),
|
|
stop: stop,
|
|
}
|
|
go w.Run()
|
|
|
|
return w
|
|
}
|