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

@@ -20,13 +20,13 @@ func TestAuditService(t *testing.T) {
service := newAuditService(buf)
// Event sent before start, will not be logged
events.Default.Log(events.Ping, "the first event")
events.Default.Log(events.ConfigSaved, "the first event")
go service.Serve()
service.WaitForStart()
// Event that should end up in the audit log
events.Default.Log(events.Ping, "the second event")
events.Default.Log(events.ConfigSaved, "the second event")
// We need to give the events time to arrive, since the channels are buffered etc.
time.Sleep(10 * time.Millisecond)
@@ -35,7 +35,7 @@ func TestAuditService(t *testing.T) {
service.WaitForStop()
// This event should not be logged, since we have stopped.
events.Default.Log(events.Ping, "the third event")
events.Default.Log(events.ConfigSaved, "the third event")
result := string(buf.Bytes())
t.Log(result)

View File

@@ -233,8 +233,8 @@ func (s *apiService) Serve() {
getRestMux.HandleFunc("/rest/db/need", s.getDBNeed) // folder [perpage] [page]
getRestMux.HandleFunc("/rest/db/status", s.getDBStatus) // folder
getRestMux.HandleFunc("/rest/db/browse", s.getDBBrowse) // folder [prefix] [dirsonly] [levels]
getRestMux.HandleFunc("/rest/events", s.getIndexEvents) // since [limit]
getRestMux.HandleFunc("/rest/events/disk", s.getDiskEvents) // since [limit]
getRestMux.HandleFunc("/rest/events", s.getIndexEvents) // since [limit] [timeout]
getRestMux.HandleFunc("/rest/events/disk", s.getDiskEvents) // since [limit] [timeout]
getRestMux.HandleFunc("/rest/stats/device", s.getDeviceStats) // -
getRestMux.HandleFunc("/rest/stats/folder", s.getFolderStats) // -
getRestMux.HandleFunc("/rest/svc/deviceid", s.getDeviceID) // id
@@ -1019,9 +1019,15 @@ func (s *apiService) getEvents(w http.ResponseWriter, r *http.Request, eventSub
qs := r.URL.Query()
sinceStr := qs.Get("since")
limitStr := qs.Get("limit")
timeoutStr := qs.Get("timeout")
since, _ := strconv.Atoi(sinceStr)
limit, _ := strconv.Atoi(limitStr)
timeout := defaultEventTimeout
if timeoutSec, timeoutErr := strconv.Atoi(timeoutStr); timeoutErr == nil && timeoutSec >= 0 { // 0 is a valid timeout
timeout = time.Duration(timeoutSec) * time.Second
}
// Flush before blocking, to indicate that we've received the request and
// that it should not be retried. Must set Content-Type header before
// flushing.
@@ -1029,7 +1035,8 @@ func (s *apiService) getEvents(w http.ResponseWriter, r *http.Request, eventSub
f := w.(http.Flusher)
f.Flush()
evs := eventSub.Since(since, nil)
// If there are no events available return an empty slice, as this gets serialized as `[]`
evs := eventSub.Since(since, []events.Event{}, timeout)
if 0 < limit && limit < len(evs) {
evs = evs[len(evs)-limit:]
}

View File

@@ -79,7 +79,7 @@ const (
tlsDefaultCommonName = "syncthing"
httpsRSABits = 2048
bepRSABits = 0 // 384 bit ECDSA used instead
pingEventInterval = time.Minute
defaultEventTimeout = time.Minute
maxSystemErrors = 5
initialSystemLog = 10
maxSystemLog = 250
@@ -594,7 +594,7 @@ func syncthingMain(runtimeOptions RuntimeOptions) {
// events. The LocalChangeDetected event might overwhelm the event
// receiver in some situations so we will not subscribe to it here.
apiSub := events.NewBufferedSubscription(events.Default.Subscribe(events.AllEvents&^events.LocalChangeDetected&^events.RemoteChangeDetected), 1000)
diskSub := events.NewBufferedSubscription(events.Default.Subscribe(events.LocalChangeDetected|events.RemoteChangeDetected|events.Ping), 1000)
diskSub := events.NewBufferedSubscription(events.Default.Subscribe(events.LocalChangeDetected|events.RemoteChangeDetected), 1000)
if len(os.Getenv("GOMAXPROCS")) == 0 {
runtime.GOMAXPROCS(runtime.NumCPU())
@@ -877,7 +877,6 @@ func syncthingMain(runtimeOptions RuntimeOptions) {
events.Default.Log(events.StartupComplete, map[string]string{
"myID": myID.String(),
})
go generatePingEvents()
cleanConfigDirectory()
@@ -1091,13 +1090,6 @@ func defaultConfig(myName string) config.Configuration {
return newCfg
}
func generatePingEvents() {
for {
time.Sleep(pingEventInterval)
events.Default.Log(events.Ping, nil)
}
}
func resetDB() error {
return os.RemoveAll(locations[locDatabase])
}

View File

@@ -6,10 +6,14 @@
package main
import "github.com/syncthing/syncthing/lib/events"
import (
"time"
"github.com/syncthing/syncthing/lib/events"
)
type mockedEventSub struct{}
func (s *mockedEventSub) Since(id int, into []events.Event) []events.Event {
func (s *mockedEventSub) Since(id int, into []events.Event, timeout time.Duration) []events.Event {
select {}
}

View File

@@ -169,7 +169,7 @@ func (c *folderSummaryService) foldersToHandle() []string {
c.lastEventReqMut.Lock()
last := c.lastEventReq
c.lastEventReqMut.Unlock()
if time.Since(last) > pingEventInterval {
if time.Since(last) > defaultEventTimeout {
return nil
}

View File

@@ -66,7 +66,7 @@ func (s *verboseService) WaitForStart() {
func (s *verboseService) formatEvent(ev events.Event) string {
switch ev.Type {
case events.Ping, events.DownloadProgress, events.LocalIndexUpdated:
case events.DownloadProgress, events.LocalIndexUpdated:
// Skip
return ""