cmd/syncthing, lib/events, lib/sync: Add timeout to REST event API, remove Ping (fixes #3933)

GitHub-Pull-Request: https://github.com/syncthing/syncthing/pull/3941
This commit is contained in:
Antony Male
2017-01-31 12:04:29 +00:00
committed by Jakob Borg
parent 20f8b4fd57
commit ac510b26e2
11 changed files with 207 additions and 36 deletions

View File

@@ -10,7 +10,6 @@ package events
import (
"errors"
"runtime"
stdsync "sync"
"time"
"github.com/syncthing/syncthing/lib/sync"
@@ -19,8 +18,7 @@ import (
type EventType int
const (
Ping EventType = 1 << iota
Starting
Starting EventType = 1 << iota
StartupComplete
DeviceDiscovered
DeviceConnected
@@ -55,8 +53,6 @@ var runningTests = false
func (t EventType) String() string {
switch t {
case Ping:
return "Ping"
case Starting:
return "Starting"
case StartupComplete:
@@ -279,11 +275,11 @@ type bufferedSubscription struct {
next int
cur int // Current SubscriptionID
mut sync.Mutex
cond *stdsync.Cond
cond *sync.TimeoutCond
}
type BufferedSubscription interface {
Since(id int, into []Event) []Event
Since(id int, into []Event, timeout time.Duration) []Event
}
func NewBufferedSubscription(s *Subscription, size int) BufferedSubscription {
@@ -292,7 +288,7 @@ func NewBufferedSubscription(s *Subscription, size int) BufferedSubscription {
buf: make([]Event, size),
mut: sync.NewMutex(),
}
bs.cond = stdsync.NewCond(bs.mut)
bs.cond = sync.NewTimeoutCond(bs.mut)
go bs.pollingLoop()
return bs
}
@@ -319,12 +315,21 @@ func (s *bufferedSubscription) pollingLoop() {
}
}
func (s *bufferedSubscription) Since(id int, into []Event) []Event {
func (s *bufferedSubscription) Since(id int, into []Event, timeout time.Duration) []Event {
s.mut.Lock()
defer s.mut.Unlock()
for id >= s.cur {
s.cond.Wait()
// Check once first before generating the TimeoutCondWaiter
if id >= s.cur {
waiter := s.cond.SetupWait(timeout)
defer waiter.Stop()
for id >= s.cur {
if eventsAvailable := waiter.Wait(); !eventsAvailable {
// Timed out
return into
}
}
}
for i := s.next; i < len(s.buf); i++ {