From 308a3b51b39227e4d14d330a8c285bcf2a18ebc9 Mon Sep 17 00:00:00 2001 From: Jesse Duffield Date: Tue, 2 Nov 2021 21:23:04 +1100 Subject: [PATCH] some more throttling stuff --- pkg/tasks/tasks.go | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/pkg/tasks/tasks.go b/pkg/tasks/tasks.go index 62237f22f..479006808 100644 --- a/pkg/tasks/tasks.go +++ b/pkg/tasks/tasks.go @@ -16,6 +16,9 @@ import ( const THROTTLE_TIME = time.Millisecond * 30 +// we use this to check if the system is under stress right now. Hopefully this makes sense on other machines +const COMMAND_START_THRESHOLD = time.Millisecond * 10 + type Task struct { stop chan struct{} stopped bool @@ -91,12 +94,16 @@ func (m *ViewBufferManager) NewCmdTask(start func() (*exec.Cmd, io.Reader), pref } startTime := time.Now() - cmd, r := start() + timeToStart := time.Since(startTime) go utils.Safe(func() { <-stop - m.throttle = time.Since(startTime) < THROTTLE_TIME + // we use the time it took to start the program as a way of checking if things + // are running slow at the moment. This is admittedly a crude estimate, but + // the point is that we only want to throttle when things are running slow + // and the user is flicking through a bunch of items. + m.throttle = time.Since(startTime) < THROTTLE_TIME && timeToStart > COMMAND_START_THRESHOLD if err := oscommands.Kill(cmd); err != nil { if !strings.Contains(err.Error(), "process already finished") { m.Log.Errorf("error when running cmd task: %v", err)