redis / ioredis

🚀 A robust, performance-focused, and full-featured Redis client for Node.js.
MIT License
14.38k stars 1.2k forks source link

Aborting command execution on demand? #1914

Open slavafomin opened 2 months ago

slavafomin commented 2 months ago

Hello!

Thank you for this great library!

However, I can't find a way to abort the pending command execution. For example: I'm using XREAD BLOCK 60000 … (XREAD) command to listen for Redis stream. It blocks execution for e.g. 60 seconds. What if I need to stop this command and abort it in the middle? E.g. when I receive a signal from the OS to terminate the process or something.

It should either return the "Cancelable Promise" or accept an AbortSignal. Am I missing something or is it not implemented yet?

Thanks!

slavafomin commented 1 month ago

I hope someone would find this helpful: what I've found out (the hard way) is that blocking connections (like XREAD BLOCK) should be executed by it's own instance of ioredis, because the library won't create a new connection for you, you must do this manually. Otherwise your blocking command could block the other parts of your application that relies on Redis.

Then, you can use the Redis.disconnect() method to terminate such blocking commands (do not use quit()).