Canceling a search from the grep crate #2294
-
I'm looking into adding search capabilities to https://github.com/chipsenkbeil/distant and am investigating using the crates that power ripgrep to do this for the reference implementation. A feature I'd like to support is the ability to cancel an ongoing search since the files I'm querying could be large. Is there a way to do this in the current crate implementations? I know I can write logic to cancel in between file searches, but wasn't sure if there was a way to cancel a searcher itself. I'm going to spawn this in a tokio blocking task, so being able to still cancel would be helpful. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 5 replies
-
The only way is to send a signal somehow, but I honestly don't know how that would work in practice. Whether something more deliberate could be added is not entirely clear. Certainly, it simply cannot be done in all cases. For example, a multi-line search requires that the entire file be in memory (whether via memory map or heap), and then let's the regex engine run on it. There is no way, and probably never will be, a way to stop the regex engine once a search has started. However, a line oriented search could in theory be stopped because it can search the file in segments via buffering. You couldn't interrupt a search on a single buffer, but you could, in theory, implement something to prevent search from buffering more contents and thus quitting the search there. However, my experience tells me that adding cancellation to something that hasn't been designed for it initially is difficult, to say the least. The refactor to achieve it probably quite gnarly, particularly given that it cannot be done in all cases unless multi-line search is banned. So in summary, I would say this is unlikely to be possible. |
Beta Was this translation helpful? Give feedback.
The only way is to send a signal somehow, but I honestly don't know how that would work in practice.
Whether something more deliberate could be added is not entirely clear. Certainly, it simply cannot be done in all cases. For example, a multi-line search requires that the entire file be in memory (whether via memory map or heap), and then let's the regex engine run on it. There is no way, and probably never will be, a way to stop the regex engine once a search has started.
However, a line oriented search could in theory be stopped because it can search the file in segments via buffering. You couldn't interrupt a search on a single buffer, but you could, in theory, implement something to …