Browse Source

lib/api, lib/model: Fixes around event request tracking (#6774)

Simon Frei 5 years ago
parent
commit
4f06708330
4 changed files with 22 additions and 3 deletions
  1. 4 1
      lib/api/api.go
  2. 2 0
      lib/api/mocked_events_test.go
  3. 14 0
      lib/events/events.go
  4. 2 2
      lib/model/folder_summary.go

+ 4 - 1
lib/api/api.go

@@ -1217,7 +1217,6 @@ func (s *service) postDBIgnores(w http.ResponseWriter, r *http.Request) {
 }
 
 func (s *service) getIndexEvents(w http.ResponseWriter, r *http.Request) {
-	s.fss.OnEventRequest()
 	mask := s.getEventMask(r.URL.Query().Get("events"))
 	sub := s.getEventSub(mask)
 	s.getEvents(w, r, sub)
@@ -1229,6 +1228,10 @@ func (s *service) getDiskEvents(w http.ResponseWriter, r *http.Request) {
 }
 
 func (s *service) getEvents(w http.ResponseWriter, r *http.Request, eventSub events.BufferedSubscription) {
+	if eventSub.Mask()&(events.FolderSummary|events.FolderCompletion) != 0 {
+		s.fss.OnEventRequest()
+	}
+
 	qs := r.URL.Query()
 	sinceStr := qs.Get("since")
 	limitStr := qs.Get("limit")

+ 2 - 0
lib/api/mocked_events_test.go

@@ -17,3 +17,5 @@ type mockedEventSub struct{}
 func (s *mockedEventSub) Since(id int, into []events.Event, timeout time.Duration) []events.Event {
 	select {}
 }
+
+func (s *mockedEventSub) Mask() events.EventType { return 0 }

+ 14 - 0
lib/events/events.go

@@ -238,6 +238,7 @@ type Event struct {
 type Subscription interface {
 	C() <-chan Event
 	Poll(timeout time.Duration) (Event, error)
+	Mask() EventType
 	Unsubscribe()
 }
 
@@ -432,6 +433,10 @@ func (s *subscription) C() <-chan Event {
 	return s.events
 }
 
+func (s *subscription) Mask() EventType {
+	return s.mask
+}
+
 func (s *subscription) Unsubscribe() {
 	select {
 	case s.toUnsubscribe <- s:
@@ -450,6 +455,7 @@ type bufferedSubscription struct {
 
 type BufferedSubscription interface {
 	Since(id int, into []Event, timeout time.Duration) []Event
+	Mask() EventType
 }
 
 func NewBufferedSubscription(s Subscription, size int) BufferedSubscription {
@@ -505,6 +511,10 @@ func (s *bufferedSubscription) Since(id int, into []Event, timeout time.Duration
 	return into
 }
 
+func (s *bufferedSubscription) Mask() EventType {
+	return s.sub.Mask()
+}
+
 // Error returns a string pointer suitable for JSON marshalling errors. It
 // retains the "null on success" semantics, but ensures the error result is a
 // string regardless of the underlying concrete error type.
@@ -540,4 +550,8 @@ func (*noopSubscription) Poll(timeout time.Duration) (Event, error) {
 	return Event{}, errNoop
 }
 
+func (s *noopSubscription) Mask() EventType {
+	return 0
+}
+
 func (*noopSubscription) Unsubscribe() {}

+ 2 - 2
lib/model/folder_summary.go

@@ -22,7 +22,7 @@ import (
 	"github.com/syncthing/syncthing/lib/util"
 )
 
-const minSummaryInterval = time.Minute
+const maxDurationSinceLastEventReq = time.Minute
 
 type FolderSummaryService interface {
 	suture.Service
@@ -303,7 +303,7 @@ func (c *folderSummaryService) foldersToHandle() []string {
 	c.lastEventReqMut.Lock()
 	last := c.lastEventReq
 	c.lastEventReqMut.Unlock()
-	if time.Since(last) > minSummaryInterval {
+	if time.Since(last) > maxDurationSinceLastEventReq {
 		return nil
 	}