From 1ac40a3043c54b949a895ec694614772038c7d62 Mon Sep 17 00:00:00 2001 From: Jakob Borg Date: Tue, 16 Jun 2015 09:17:58 +0200 Subject: [PATCH] Fix API event subscription The API never got the first few events ("Starting" etc) as it subscribed too late. Instead, set up a subscription for it early on. If the API is configured not to run this is unnecessary but doesn't hurt very much. --- cmd/syncthing/gui.go | 10 ++++------ cmd/syncthing/main.go | 9 ++++++--- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/cmd/syncthing/gui.go b/cmd/syncthing/gui.go index a4f70c002..460a92ad1 100644 --- a/cmd/syncthing/gui.go +++ b/cmd/syncthing/gui.go @@ -49,7 +49,6 @@ var ( guiErrors = []guiError{} guiErrorsMut = sync.NewMutex() startTime = time.Now() - eventSub *events.BufferedSubscription ) type apiSvc struct { @@ -60,14 +59,16 @@ type apiSvc struct { fss *folderSummarySvc stop chan struct{} systemConfigMut sync.Mutex + eventSub *events.BufferedSubscription } -func newAPISvc(cfg config.GUIConfiguration, assetDir string, m *model.Model) (*apiSvc, error) { +func newAPISvc(cfg config.GUIConfiguration, assetDir string, m *model.Model, eventSub *events.BufferedSubscription) (*apiSvc, error) { svc := &apiSvc{ cfg: cfg, assetDir: assetDir, model: m, systemConfigMut: sync.NewMutex(), + eventSub: eventSub, } var err error @@ -125,9 +126,6 @@ func (s *apiSvc) Serve() { s.stop = make(chan struct{}) l.AddHandler(logger.LevelWarn, s.showGuiError) - sub := events.Default.Subscribe(events.AllEvents) - eventSub = events.NewBufferedSubscription(sub, 1000) - defer events.Default.Unsubscribe(sub) // The GET handlers getRestMux := http.NewServeMux() @@ -743,7 +741,7 @@ func (s *apiSvc) getEvents(w http.ResponseWriter, r *http.Request) { f := w.(http.Flusher) f.Flush() - evs := eventSub.Since(since, nil) + evs := s.eventSub.Since(since, nil) if 0 < limit && limit < len(evs) { evs = evs[len(evs)-limit:] } diff --git a/cmd/syncthing/main.go b/cmd/syncthing/main.go index 2e7910b1b..3804f4246 100644 --- a/cmd/syncthing/main.go +++ b/cmd/syncthing/main.go @@ -447,6 +447,9 @@ func syncthingMain() { mainSvc.Add(newVerboseSvc()) } + // Event subscription for the API; must start early to catch the early events. + apiSub := events.NewBufferedSubscription(events.Default.Subscribe(events.AllEvents), 1000) + if len(os.Getenv("GOMAXPROCS")) == 0 { runtime.GOMAXPROCS(runtime.NumCPU()) } @@ -628,7 +631,7 @@ func syncthingMain() { // GUI - setupGUI(mainSvc, cfg, m) + setupGUI(mainSvc, cfg, m, apiSub) // The default port we announce, possibly modified by setupUPnP next. @@ -768,7 +771,7 @@ func startAuditing(mainSvc *suture.Supervisor) { l.Infoln("Audit log in", auditFile) } -func setupGUI(mainSvc *suture.Supervisor, cfg *config.Wrapper, m *model.Model) { +func setupGUI(mainSvc *suture.Supervisor, cfg *config.Wrapper, m *model.Model, apiSub *events.BufferedSubscription) { opts := cfg.Options() guiCfg := overrideGUIConfig(cfg.GUI(), guiAddress, guiAuthentication, guiAPIKey) @@ -797,7 +800,7 @@ func setupGUI(mainSvc *suture.Supervisor, cfg *config.Wrapper, m *model.Model) { urlShow := fmt.Sprintf("%s://%s/", proto, net.JoinHostPort(hostShow, strconv.Itoa(addr.Port))) l.Infoln("Starting web GUI on", urlShow) - api, err := newAPISvc(guiCfg, guiAssets, m) + api, err := newAPISvc(guiCfg, guiAssets, m, apiSub) if err != nil { l.Fatalln("Cannot start GUI:", err) }