Use readLines channel only for command tasks

And only while the task is running.

This avoids accumulating lots of blocked goroutines when scrolling a view down
more than 1024 times (the capacity of the readLines channel).
This commit is contained in:
Stefan Haller 2025-04-02 12:11:38 +02:00
parent 7b17f33e9e
commit 60887eddd0

View file

@ -91,16 +91,18 @@ func NewViewBufferManager(
beforeStart: beforeStart,
refreshView: refreshView,
onEndOfInput: onEndOfInput,
readLines: make(chan LinesToRead, 1024),
readLines: nil,
onNewKey: onNewKey,
newGocuiTask: newGocuiTask,
}
}
func (self *ViewBufferManager) ReadLines(n int) {
go utils.Safe(func() {
self.readLines <- LinesToRead{Total: n, InitialRefreshAfter: -1}
})
if self.readLines != nil {
go utils.Safe(func() {
self.readLines <- LinesToRead{Total: n, InitialRefreshAfter: -1}
})
}
}
func (self *ViewBufferManager) NewCmdTask(start func() (*exec.Cmd, io.Reader), prefix string, linesToRead LinesToRead, onDoneFn func()) func(TaskOpts) error {
@ -166,7 +168,6 @@ func (self *ViewBufferManager) NewCmdTask(start func() (*exec.Cmd, io.Reader), p
loadingMutex := deadlock.Mutex{}
// not sure if it's the right move to redefine this or not
self.readLines = make(chan LinesToRead, 1024)
scanner := bufio.NewScanner(r)
@ -274,6 +275,8 @@ func (self *ViewBufferManager) NewCmdTask(start func() (*exec.Cmd, io.Reader), p
}
}
self.readLines = nil
refreshViewIfStale()
if err := cmd.Wait(); err != nil {
@ -349,6 +352,8 @@ func (self *ViewBufferManager) NewTask(f func(TaskOpts) error, key string) error
go utils.Safe(func() {
defer completeGocuiTask()
self.readLines = nil
self.taskIDMutex.Lock()
self.newTaskID++
taskID := self.newTaskID