Augusto
Augusto

Reputation: 1336

Akka TCP Comand Failed

I'm writing a client and server applications using Akka Tcp and I'm having a issue with a high throughput. When I write too many messages on the client side, I'm having too many CommandFailed messages and I can't figure out why... Here's 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 [email protected](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 Finished are just case classes that I've createad. Here's the client (where I think its 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 receive a lot of CommandFailed messages and I call ResumeWrite and never receive a WritingResumed (and the connection never closes in this cases). Am I doing something wrong?

Upvotes: 4

Views: 1416

Answers (1)

Pierpaolo Follia
Pierpaolo Follia

Reputation: 3050

I think the problem is that when you register your actor sending the Register message, you also have to set the useResumeWriting parameter to true:

connection ! Register(handler, false, true)

The doc of resumeWriting command states:

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]].

Upvotes: 2

Related Questions