Akka TCP Comand Error

I write client and server applications using Akka Tcp and I have a high bandwidth problem. When I write too many messages on the client side, I have too many messages with CommandFailed, and I can not understand why ... Here is my server:

class Server(listener: ActorRef) extends Actor {

  import Tcp._
  import context.system

  IO(Tcp) ! Bind(self, new InetSocketAddress("localhost", 9090))

  def receive = {
    case CommandFailed(_: Bind) => {
      println("command failed error")
      context stop self
    }

    case c@Tcp.Connected(remote, local) =>
      listener ! GatlingConnected(c.remoteAddress.toString)
      println("Connected: " + c.remoteAddress)
      val handler = context.actorOf(Props(classOf[ServerHandler], listener, c.remoteAddress.toString))
      val connection = sender
      connection ! Register(handler)
  }
}

class ServerHandler(listener: ActorRef, remote: String) extends Actor {

  import Tcp._

  override def receive: Receive = {
    case Received(data) => listener ! data.utf8String
    case PeerClosed => {
      listener ! Finished(remote)
      context stop self
    }
  }
}

Message and done - these are just the class classes that I created. Here's the client (where I consider him the source of this problem):

private class TCPMessageSender(listener: ActorRef) extends BaseActor {
    final val MESSAGE_DELIMITER = "\n"
    val buffer = new ListBuffer[Any]
    val failedMessages = new ListBuffer[Write]
    IO(Tcp) ! Connect(new InetSocketAddress(configuration.data.tcp.host, configuration.data.tcp.port))

    override def receive = {
      case msg @ (_: UserMessage | _: GroupMessage | _: RequestMessage) =>
        logger.warn(s"Received message ($msg) before connected. Buffering...")
        buffer += msg
      case CommandFailed(_: Connect) =>
        logger.warn("Can't connect. All messages will be ignored")
        listener ! Terminate
        context stop self
      case c @ Connected(remote, local) =>
        logger.info("Connected to " + c.remoteAddress)
        val connection = sender
        connection ! Register(self)
        logger.info("Sending previous received messages: " + buffer.size)
        buffer.foreach(msg => {
          val msgString: String = JsonHelper.toJson(Map[String, Any]("message_type" -> msg.getClass.getSimpleName, "message" -> msg))
          connection ! Write(ByteString(msgString + MESSAGE_DELIMITER))
        })
        buffer.clear
        logger.info("Sent")
        context become {
          case msg @ (_: UserMessage | _: GroupMessage | _: RequestMessage) =>
            val msgString: String = JsonHelper.toJson(Map[String, Any]("message_type" -> msg.getClass.getSimpleName, "message" -> msg))
            logger.trace(s"Sending message: $msgString")
            connection ! Write(ByteString(msgString + MESSAGE_DELIMITER))
          case CommandFailed(w: Write) =>
            logger.error("Command failed. Buffering message...")
            failedMessages += w
            connection ! ResumeWriting
          case CommandFailed(c) => logger.error(s"Command $c failed. I don't know what to do...")
          case Received(data) =>
            logger.warn(s"I am not supposed to receive this data: $data")
          case "close" =>
            connection ! Close
          case _: ConnectionClosed =>
            logger.info("Connection closed")
            context stop self
          case WritingResumed => {
            logger.info("Sending failed messages")
            failedMessages.foreach(write => connection ! write)
            failedMessages.clear
          }
        }
    }
  }

Sometimes I get a lot of messages with CommandFailed, and I call ResumeWrite and never get WritingResumed (and the connection never closes in these cases). Am I doing something wrong?

+4
source share
1 answer

, , , Register, useResumeWriting true:

connection ! Register(handler, false, true)

resumeWriting :

When `useResumeWriting` is in effect as was indicated in the [[Tcp.Register]] message
then this command needs to be sent to the connection actor in order to re-enable
writing after a [[Tcp.CommandFailed]] event. All [[Tcp.WriteCommand]] processed by the
connection actor between the first [[Tcp.CommandFailed]] and subsequent reception of
this message will also be rejected with [[Tcp.CommandFailed]].
+2

Source: https://habr.com/ru/post/1536794/


All Articles