-
-
Notifications
You must be signed in to change notification settings - Fork 46
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: implement monitor command #483
Open
ArturSharapov
wants to merge
2
commits into
denodrivers:master
Choose a base branch
from
ArturSharapov:monitor
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import type { CommandExecutor } from "./executor.ts"; | ||
import { isRetriableError } from "./errors.ts"; | ||
import { kUnstableReadReply } from "./internal/symbols.ts"; | ||
|
||
export interface RedisMonitorLog { | ||
timestamp: string; | ||
args: string[]; | ||
source: string; | ||
database: string; | ||
} | ||
|
||
export interface RedisMonitor { | ||
readonly isConnected: boolean; | ||
readonly isClosed: boolean; | ||
receive(): AsyncIterableIterator<RedisMonitorLog>; | ||
close(): void; | ||
} | ||
|
||
export class RedisMonitorImpl implements RedisMonitor { | ||
get isConnected(): boolean { | ||
return this.executor.connection.isConnected; | ||
} | ||
|
||
get isClosed(): boolean { | ||
return this.executor.connection.isClosed; | ||
} | ||
|
||
constructor(private executor: CommandExecutor) {} | ||
|
||
receive(): AsyncIterableIterator<RedisMonitorLog> { | ||
return this.#receive(); | ||
} | ||
|
||
/** | ||
* Non-standard return value. Dumps the received commands in an infinite flow. | ||
* @see https://redis.io/docs/latest/commands/monitor | ||
*/ | ||
async *#receive(): AsyncIterableIterator<RedisMonitorLog> { | ||
let forceReconnect = false; | ||
const connection = this.executor.connection; | ||
while (this.isConnected) { | ||
try { | ||
let reply: string; | ||
try { | ||
reply = await connection[kUnstableReadReply]() as typeof reply; | ||
} catch (err) { | ||
if (this.isClosed) { | ||
// Connection already closed by the user. | ||
break; | ||
} | ||
throw err; // Connection may have been unintentionally closed. | ||
} | ||
|
||
// Reply example: 1735135615.9063666 [0 127.0.0.1:52848] "XRANGE" "foo" "-" "+" "COUNT" "3" | ||
const len = reply.indexOf(" "); | ||
const timestamp = reply.slice(0, len); | ||
const argIndex = reply.indexOf('"'); | ||
const args = reply | ||
.slice(argIndex + 1, -1) | ||
.split('" "') | ||
.map((elem) => elem.replace(/\\"/g, '"')); | ||
const [database, source] = reply.slice(len + 2, argIndex - 2).split( | ||
" ", | ||
); | ||
|
||
yield { timestamp, args, source, database }; | ||
} catch (error) { | ||
if (isRetriableError(error)) { | ||
forceReconnect = true; | ||
} else throw error; | ||
} finally { | ||
if ((!this.isClosed && !this.isConnected) || forceReconnect) { | ||
forceReconnect = false; | ||
await connection.reconnect(); | ||
} | ||
} | ||
} | ||
} | ||
|
||
close() { | ||
this.executor.connection.close(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it would be better not to duplicate the connection here for the following reasons:
subscribe()
What do you think about this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've been using ioredis historically (which also seems to be 1.5+ times more popular than node-redis on npm).
Unlike node-redis, it does duplicate the connection by default on
monitor()
which I think was rational because all other commands become unavailable then.The only positive side of using a single connection I can see is to cover the case when one would want to create redis connection only to perform monitoring and nothing else.
But this comes with a cost, making it dangerous, as it easily leads to unexpected behavior, blocking execution of all other redis commands. For those who are not experienced with redis much or for those coming from
ioredis
, it would become unclear, why some redis commands just get stuck and take forever to execute; in a large codebase it would be especially challenging to debug. It's also unclear whether the commands you call are accumulated and executed once you terminate the monitor (which can only be done manually), or they are just ignored entirely.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would advocate for the duplication of connection as it make it simpler and doesn't "break all things", as well as eliminates potential issues and unforeseen behavior.
That said, what about brining an argument (option) to the
monitor()
that could make it use the same connection?To cover the case when one needs to have redis only for monitoring and doesn't need a regular working redis instance to use the rest of the API.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ArturSharapov I'm in favor of adding an option to the
monitor()
👍We are planning to support RESP3 in the future, and it would be ideal to have an option of not duplicating a connection to
monitor()
.I have two suggestions:
reuseConnection
option tomonitor()
.Connection#duplicate
a private API for nowConnection#duplicate
private until thenredis/connection.ts
Line 51 in ad96f2c
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good call 👍