Skip to content
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

Change return type of CLIENT INFO and CLIENT LIST #813

Merged
merged 9 commits into from
Apr 10, 2023

Conversation

drmarjanovic
Copy link
Collaborator

@drmarjanovic drmarjanovic commented Apr 9, 2023

Description:

  • Use ClientInfo and ClientsInfo output types for CLIENT INFO and CLIENT LIST commands instead of plain string.
  • Removed unused class.

@drmarjanovic drmarjanovic requested a review from a team as a code owner April 9, 2023 21:43
@drmarjanovic drmarjanovic force-pushed the improve-client-info-client-list branch from 1a48bad to 728938e Compare April 9, 2023 21:46
Comment on lines 86 to 107
def getFlags(flags: String): Set[ClientFlag] = {
def clientFlag(cf: Char): Option[ClientFlag] = cf match {
case 'A' => Some(ClientFlag.ToBeClosedAsap)
case 'b' => Some(ClientFlag.Blocked)
case 'B' => Some(ClientFlag.BroadcastTrackingMode)
case 'c' => Some(ClientFlag.ToBeClosedAfterReply)
case 'd' => Some(ClientFlag.WatchedKeysModified)
case 'M' => Some(ClientFlag.IsMaster)
case 'O' => Some(ClientFlag.MonitorMode)
case 'P' => Some(ClientFlag.PubSub)
case 'r' => Some(ClientFlag.ReadOnlyMode)
case 'R' => Some(ClientFlag.TrackingTargetClientInvalid)
case 'S' => Some(ClientFlag.Replica)
case 't' => Some(ClientFlag.KeysTrackingEnabled)
case 'u' => Some(ClientFlag.Unblocked)
case 'U' => Some(ClientFlag.UnixDomainSocket)
case 'x' => Some(ClientFlag.MultiExecContext)
case _ => None
}

flags.foldLeft(Set.empty[ClientFlag])((fs, f) => fs ++ clientFlag(f))
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move these two functions to companion object.

lines.map(from).toList
}

type ClientsInfo = List[ClientInfo]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need for this.

Comment on lines 110 to 134
val events = data("events")
new ClientInfo(
id = data("id").toLong,
name = data.get("name"),
address = data.get("addr").flatMap(Address.fromString),
localAddress = data.get("laddr").flatMap(Address.fromString),
fileDescriptor = data.get("fd").map(_.toLong),
age = data.get("age").map(s => Duration.fromSeconds(s.toLong)),
idle = data.get("idle").map(s => Duration.fromSeconds(s.toLong)),
flags = data.get("flags").fold(Set.empty[ClientFlag])(getFlags),
databaseId = data.get("id").map(_.toLong),
subscriptions = data("sub").toInt,
patternSubscriptions = data("psub").toInt,
multiCommands = data("multi").toInt,
queryBufferLength = data.get("qbuf").map(_.toInt),
queryBufferFree = data.get("qbuf-free").map(_.toInt),
outputListLength = data.get("oll").map(_.toInt),
outputBufferMem = data.get("omem").map(_.toLong),
events = ClientEvents(readable = events.contains("r"), writable = events.contains("w")),
lastCommand = data.get("cmd"),
argvMemory = data.get("argv-mem").map(_.toLong),
totalMemory = data.get("total-mem").map(_.toLong),
redirectionClientId = data.get("redir").map(_.toLong),
user = data.get("user")
)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Plenty of unsafe calls here.

Comment on lines 738 to 744
case object ClientsInfoOutput extends Output[ClientsInfo] {
protected def tryDecode(respValue: RespValue): ClientsInfo =
respValue match {
case RespValue.BulkString(s) => ClientInfo.from(s.asString.split("\r\n").filter(_.nonEmpty))
case other => throw ProtocolError(s"$other isn't a bulk string")
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's rename this to ClientListOutput. Also prefer Chunk to List, e.g.:

case object ClientListOutput extends Output[Chunk[ClientInfo]] { ... }

You might be able to reuse ClientInfoOutput in its implementation, though it's not necessary.

object Address {
private[redis] final def fromString(addr: String): Option[Address] =
addr.split(":").toList match {
case ip :: port :: Nil => Some(new Address(InetAddress.getByName(ip), port.toInt))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

toIntOption

Comment on lines 58 to 75
flag match {
case 'A' => Some(ClientFlag.ToBeClosedAsap)
case 'b' => Some(ClientFlag.Blocked)
case 'B' => Some(ClientFlag.BroadcastTrackingMode)
case 'c' => Some(ClientFlag.ToBeClosedAfterReply)
case 'd' => Some(ClientFlag.WatchedKeysModified)
case 'M' => Some(ClientFlag.IsMaster)
case 'O' => Some(ClientFlag.MonitorMode)
case 'P' => Some(ClientFlag.PubSub)
case 'r' => Some(ClientFlag.ReadOnlyMode)
case 'R' => Some(ClientFlag.TrackingTargetClientInvalid)
case 'S' => Some(ClientFlag.Replica)
case 't' => Some(ClientFlag.KeysTrackingEnabled)
case 'u' => Some(ClientFlag.Unblocked)
case 'U' => Some(ClientFlag.UnixDomainSocket)
case 'x' => Some(ClientFlag.MultiExecContext)
case _ => None
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can simplify via Map. It can be private and lazy.

mijicd
mijicd previously approved these changes Apr 10, 2023
@mijicd mijicd merged commit 7dabd5b into zio:master Apr 10, 2023
@drmarjanovic drmarjanovic deleted the improve-client-info-client-list branch April 10, 2023 18:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants