-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from Miaxos/feat-add-set-name
Feat: Add client setname
- Loading branch information
Showing
11 changed files
with
154 additions
and
48 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,62 @@ | ||
use bytestring::ByteString; | ||
|
||
use super::super::parse::Parse; | ||
use crate::application::server::connection::WriteConnection; | ||
use crate::application::server::context::Context; | ||
use crate::application::server::frame::Frame; | ||
|
||
/// The CLIENT SETNAME command assigns a name to the current connection. | ||
/// | ||
/// The assigned name is displayed in the output of CLIENT LIST so that it is | ||
/// possible to identify the client that performed a given connection. | ||
/// | ||
/// For instance when Redis is used in order to implement a queue, producers and | ||
/// consumers of messages may want to set the name of the connection according | ||
/// to their role. | ||
/// | ||
/// There is no limit to the length of the name that can be assigned if not the | ||
/// usual limits of the Redis string type (512 MB). However it is not possible | ||
/// to use spaces in the connection name as this would violate the format of the | ||
/// CLIENT LIST reply. | ||
/// | ||
/// It is possible to entirely remove the connection name setting it to the | ||
/// empty string, that is not a valid connection name since it serves to this | ||
/// specific purpose. | ||
/// | ||
/// The connection name can be inspected using CLIENT GETNAME. | ||
/// | ||
/// Every new connection starts without an assigned name. | ||
/// | ||
/// Tip: setting names to connections is a good way to debug connection leaks | ||
/// due to bugs in the application using Redis. | ||
#[derive(Debug, Default)] | ||
pub struct ClientSetName { | ||
name: ByteString, | ||
} | ||
|
||
impl ClientSetName { | ||
/// Create a new `ClientSetName` command with optional `msg`. | ||
pub fn new(name: ByteString) -> ClientSetName { | ||
ClientSetName { name } | ||
} | ||
|
||
pub(crate) fn parse_frames( | ||
parse: &mut Parse, | ||
) -> anyhow::Result<ClientSetName> { | ||
let name = parse.next_string()?; | ||
Ok(ClientSetName::new(name)) | ||
} | ||
|
||
pub(crate) async fn apply( | ||
self, | ||
dst: &mut WriteConnection, | ||
ctx: Context, | ||
) -> anyhow::Result<()> { | ||
ctx.connection.set_name(self.name).await; | ||
|
||
let response = Frame::Simple(ByteString::from_static("OK")); | ||
dst.write_frame(&response).await?; | ||
|
||
Ok(()) | ||
} | ||
} |
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,39 @@ | ||
mod utils; | ||
use redis_async::resp_array; | ||
use regex::Regex; | ||
|
||
#[tokio::test] | ||
pub async fn client_setname() { | ||
let test_re: Regex = | ||
Regex::new(r"^id=0 addr=.*? laddr=.*? fd=.*? name=$").unwrap(); | ||
let addr = utils::start_simple_server(); | ||
|
||
let connection = utils::connect_without_auth(addr).await; | ||
|
||
let mut res_f: Vec<String> = connection | ||
.send(resp_array!["CLIENT", "LIST"]) | ||
.await | ||
.unwrap(); | ||
|
||
assert_eq!(res_f.len(), 1); | ||
let first_value = res_f.pop().unwrap(); | ||
assert!(test_re.is_match(&first_value)); | ||
|
||
let res_ok: String = connection | ||
.send(resp_array!["CLIENT", "SETNAME", "newname"]) | ||
.await | ||
.unwrap(); | ||
|
||
assert_eq!(res_ok, "OK"); | ||
|
||
let mut res_f: Vec<String> = connection | ||
.send(resp_array!["CLIENT", "LIST"]) | ||
.await | ||
.unwrap(); | ||
|
||
let test_re: Regex = | ||
Regex::new(r"^id=0 addr=.*? laddr=.*? fd=.*? name=newname$").unwrap(); | ||
assert_eq!(res_f.len(), 1); | ||
let first_value = res_f.pop().unwrap(); | ||
assert!(test_re.is_match(&first_value)); | ||
} |
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