Skip to content

Commit

Permalink
Reduce allocation in ChannelLocalActor.
Browse files Browse the repository at this point in the history
  • Loading branch information
He-Pin committed Sep 12, 2023
1 parent 3f1d651 commit a92c2d1
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
#migrate the classic transport to Netty4
ProblemFilters.exclude[IncompatibleMethTypeProblem]("org.apache.pekko.remote.transport.netty.NettyFutureBridge.apply")
ProblemFilters.exclude[MissingClassProblem]("org.apache.pekko.remote.transport.netty.ChannelLocalActor")
ProblemFilters.exclude[MissingClassProblem]("org.apache.pekko.remote.transport.netty.ChannelLocalActor$")
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,10 @@ import pekko.remote.transport.AssociationHandle.{ Disassociated, HandleEvent, Ha
import pekko.remote.transport.Transport.AssociationEventListener
import pekko.util.ByteString
import io.netty.buffer.{ ByteBuf, ByteBufUtil, Unpooled }
import io.netty.util.AttributeKey

import java.util.concurrent.ConcurrentHashMap

/**
* INTERNAL API
*/
private[remote] object ChannelLocalActor {
private val map = new ConcurrentHashMap[Channel, Option[HandleEventListener]]()

def notifyListener(channel: Channel, msg: HandleEvent): Unit = {
map.getOrDefault(channel, None).foreach {
_.notify(msg)
}
}

def set(channel: Channel, listener: Option[HandleEventListener]): Unit = {
map.put(channel, listener)
}
private[remote] object TcpHandlers {
private val LISTENER = AttributeKey.valueOf[HandleEventListener]("listener")
}

/**
Expand All @@ -52,14 +38,12 @@ private[remote] object ChannelLocalActor {
@nowarn("msg=deprecated")
private[remote] trait TcpHandlers extends CommonHandlers {
protected def log: LoggingAdapter

import ChannelLocalActor._
import TcpHandlers._

override def registerListener(
channel: Channel,
listener: HandleEventListener,
remoteSocketAddress: InetSocketAddress): Unit =
ChannelLocalActor.set(channel, Some(listener))
remoteSocketAddress: InetSocketAddress): Unit = channel.attr(LISTENER).set(listener)

override def createHandle(channel: Channel, localAddress: Address, remoteAddress: Address): AssociationHandle =
new TcpAssociationHandle(localAddress, remoteAddress, transport, channel)
Expand All @@ -79,6 +63,13 @@ private[remote] trait TcpHandlers extends CommonHandlers {
log.warning("Remote connection to [{}] failed with {}", ctx.channel().remoteAddress(), e.getCause)
ctx.channel().close() // No graceful close here
}

private def notifyListener(channel: Channel, event: HandleEvent): Unit = {
val listener = channel.attr(LISTENER).get()
if (listener ne null) {
listener.notify(event)
}
}
}

/**
Expand Down

0 comments on commit a92c2d1

Please sign in to comment.