Skip to content

Commit

Permalink
reduce cpu usage caused by tickers
Browse files Browse the repository at this point in the history
  • Loading branch information
jesseduffield committed Feb 2, 2020
1 parent 012f8d6 commit cea67bc
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 19 deletions.
5 changes: 4 additions & 1 deletion pkg/commands/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ func NewDockerCommand(log *logrus.Entry, osCommand *OSCommand, tr *i18n.Translat

// MonitorContainerStats is a function
func (c *DockerCommand) MonitorContainerStats() {
// TODO: pass in a stop channel to these so we don't restart every time we come back from a subprocess
go c.MonitorCLIContainerStats()
go c.MonitorClientContainerStats()
}
Expand Down Expand Up @@ -150,7 +151,9 @@ func (c *DockerCommand) MonitorCLIContainerStats() {
func (c *DockerCommand) MonitorClientContainerStats() {
// periodically loop through running containers and see if we need to create a monitor goroutine for any
// every second we check if we need to spawn a new goroutine
for range time.Tick(time.Second) {
ticker := time.NewTicker(time.Second)
defer ticker.Stop()
for range ticker.C {
for _, container := range c.Containers {
if !container.MonitoringStats {
go c.createClientStatMonitor(container)
Expand Down
26 changes: 19 additions & 7 deletions pkg/gui/app_status_manager.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package gui

import (
"time"

"github.com/jesseduffield/gocui"
"github.com/jesseduffield/lazydocker/pkg/utils"
)
Expand Down Expand Up @@ -49,15 +51,25 @@ func (m *statusManager) getStatusString() string {
// WithWaitingStatus wraps a function and shows a waiting status while the function is still executing
func (gui *Gui) WithWaitingStatus(name string, f func() error) error {
go func() {
gui.g.Update(func(g *gocui.Gui) error {
gui.statusManager.addWaitingStatus(name)
return nil
})
gui.statusManager.addWaitingStatus(name)

defer gui.g.Update(func(g *gocui.Gui) error {
defer func() {
gui.statusManager.removeStatus(name)
return nil
})
}()

go func() {
ticker := time.NewTicker(time.Millisecond * 50)
defer ticker.Stop()
for range ticker.C {
appStatus := gui.statusManager.getStatusString()
if appStatus == "" {
return
}
if err := gui.renderString(gui.g, "appStatus", appStatus); err != nil {
gui.Log.Warn(err)
}
}
}()

if err := f(); err != nil {
gui.g.Update(func(g *gocui.Gui) error {
Expand Down
1 change: 1 addition & 0 deletions pkg/gui/confirmation_panel.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ func (gui *Gui) prepareConfirmationPanel(currentView *gocui.View, title, prompt
return nil, err
}
confirmationView.HasLoader = hasLoader
gui.g.StartTicking()
confirmationView.Title = title
confirmationView.Wrap = true
confirmationView.FgColor = gocui.ColorWhite
Expand Down
15 changes: 4 additions & 11 deletions pkg/gui/gui.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,14 +214,6 @@ func (gui *Gui) promptAnonymousReporting() error {
})
}

func (gui *Gui) renderAppStatus() error {
appStatus := gui.statusManager.getStatusString()
if appStatus != "" {
return gui.renderString(gui.g, "appStatus", appStatus)
}
return nil
}

func (gui *Gui) renderGlobalOptions() error {
return gui.renderOptionsMap(map[string]string{
"PgUp/PgDn": gui.Tr.Scroll,
Expand All @@ -236,7 +228,9 @@ func (gui *Gui) goEvery(interval time.Duration, function func() error) {
currentSessionIndex := gui.State.SessionIndex
_ = function() // time.Tick doesn't run immediately so we'll do that here // TODO: maybe change
go func() {
for range time.Tick(interval) {
ticker := time.NewTicker(interval)
defer ticker.Stop()
for range ticker.C {
if gui.State.SessionIndex > currentSessionIndex {
return
}
Expand Down Expand Up @@ -276,7 +270,6 @@ func (gui *Gui) Run() error {
dockerRefreshInterval := gui.Config.UserConfig.Update.DockerRefreshInterval
go func() {
gui.waitForIntro.Wait()
gui.goEvery(time.Millisecond*50, gui.renderAppStatus)
gui.goEvery(time.Millisecond*30, gui.reRenderMain)
gui.goEvery(dockerRefreshInterval, gui.refreshProject)
gui.goEvery(dockerRefreshInterval, gui.refreshContainersAndServices)
Expand All @@ -285,7 +278,7 @@ func (gui *Gui) Run() error {
gui.goEvery(time.Millisecond*1000, gui.checkForContextChange)
}()

go gui.DockerCommand.MonitorContainerStats()
gui.DockerCommand.MonitorContainerStats()

go func() {
for err := range gui.ErrorChan {
Expand Down

0 comments on commit cea67bc

Please sign in to comment.