Skip to content

Commit

Permalink
remove new
Browse files Browse the repository at this point in the history
  • Loading branch information
schlawg committed Dec 31, 2023
1 parent c832d40 commit 61cfe67
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
16 changes: 12 additions & 4 deletions src/main/scala/netty/FrameHandler.scala
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ final private class FrameHandler(connector: ActorChannelConnector)(using Executo
if txt.nonEmpty then
val endpoint = ctx.channel.attr(key.endpoint).get
if endpoint != null && !endpoint.rateLimit(txt) then
ctx.channel
.writeAndFlush(new CloseWebSocketFrame(1008, "rate limit exceeded"))
.addListener(_ => ctx.channel.close())
shutdown(ctx.channel, 1008, "rate limit exceeded")
else
ClientOut parse txt foreach:

Expand All @@ -37,25 +35,35 @@ final private class FrameHandler(connector: ActorChannelConnector)(using Executo
case ClientOut.Unexpected(msg) =>
Monitor.clientOutUnexpected.increment()
logger.info(s"Unexpected $msg")
shutdown(ctx.channel, 1011, "unexpected message")

case ClientOut.WrongHole =>
Monitor.clientOutWrongHole.increment()
// choice of hole is a personal decision

case out => withClientOf(ctx.channel)(_ tell out)

case frame: PongWebSocketFrame =>
val lagMillis = (System.currentTimeMillis() - frame.content().getLong(0)).toInt
val pong = ClientOut.RoundPongFrame(lagMillis)
Option(ctx.channel.attr(key.client).get) foreach { _.value foreach { _ foreach (_ ! pong) } }

case frame =>
logger.info("unsupported frame type: " + frame.getClass.getName)
shutdown(ctx.channel, 1003, s"${frame.getClass.getName} unsupported")

private def withClientOf(channel: Channel)(f: Client => Unit): Unit =
Option(channel.attr(key.client).get) match
case Some(clientFu) =>
clientFu.value match
case Some(client) => client foreach f
case None => clientFu foreach f
case None => logger.warn(s"No client actor on channel $channel")
case None =>
logger.warn(s"No client actor on channel $channel")
shutdown(channel, 1011, s"no actor on $channel")

private def shutdown(channel: Channel, code: Int, reason: String): Unit =
channel.writeAndFlush(CloseWebSocketFrame(code, reason)).addListener(_ => channel.close())

private object FrameHandler:

Expand Down
3 changes: 2 additions & 1 deletion src/main/scala/util/RateLimit.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ final class RateLimit(
private var logged: Boolean = false

def apply(msg: => String = ""): Boolean =
if credits > 0 then
if msg == "null" then true
else if credits > 0 then
credits -= 1
true
else if clearAt < nowMillis then
Expand Down

0 comments on commit 61cfe67

Please sign in to comment.