NATS流媒体服务器用户速率限制和正好一次传送

时间:2017-07-28 05:40:02

标签: scala nats.io nats-streaming-server

我正在使用NATS流媒体播放,我遇到了用户速率限制问题。当我将飞行中的最大值设置为1并将超时设置为1秒并且我有一个基本上是Thread.sleep(1000)的消费者时,我得到同一事件的多次。我想通过限制飞行和使用手动确认这不应该发生。一旦交付非常缓慢的消费者,我怎样才能得到漂亮的呢?

  case class EventBus[I, O](inputTopic: String, outputTopic: String, connection: Connection, eventProcessor: StatefulEventProcessor[I, O]) {
    // the event bus could be some abstract class while the `Connection` coulbd be injected using DI
    val substritionOptions: SubscriptionOptions = new SubscriptionOptions.Builder()
                                                                         .setManualAcks(true)
                                                                         .setDurableName("foo")
                                                                         .setMaxInFlight(1)
                                                                         .setAckWait(1, TimeUnit.SECONDS)
                                                                         .build()

    if (!inputTopic.isEmpty) {
      connection.subscribe(inputTopic, new MessageHandler() {
        override def onMessage(m: Message) {
          m.ack()
          try {
            val event = eventProcessor.deserialize(m.getData)
            eventProcessor.onEvent(event)
          } catch {
            case any =>
              try {
                val command = new String(m.getData)
                eventProcessor.onCommand(command)
              } catch {
                case any => println(s"de-serialization error: $any")
              }
          } finally {
            println("got event")
          }
        }
      }, substritionOptions)
    }

    if (!outputTopic.isEmpty) {
      eventProcessor.setBus(e => {
        try {
          connection.publish(outputTopic, eventProcessor.serialize(e))
        } catch {
          case ex => println(s"serialization error $ex")
        }
      })
    }
  }


  abstract class StatefulEventProcessor[I, O] {
    private var bus: Option[O => Unit] = None
    def onEvent(event: I): Unit
    def onCommand(command: String): Unit

    def serialize(o: O): Array[Byte] =
      SerializationUtils.serialize(o.asInstanceOf[java.io.Serializable])

    def deserialize(in: Array[Byte]): I =
      SerializationUtils.deserialize[I](in)

    def setBus(push: O => Unit): Unit = {
      if (bus.isDefined) {
        throw new IllegalStateException("bus already set")
      } else {
        bus = Some(push)
      }
    }

    def push(event: O) =
      bus.get.apply(event)
  }


  EventBus("out-1", "out-2", sc, new StatefulEventProcessor[String, String] {
    override def onEvent(event: String): Unit = {
      Thread.sleep(1000)
      push("!!!" + event)
    }

    override def onCommand(command: String): Unit = {}
  })

  (0 until 100).foreach(i => sc.publish("out-1", SerializationUtils.serialize(s"test-$i")))

1 个答案:

答案 0 :(得分:2)

首先,NATS Streaming没有确切的一次(重新)交付保证。 MaxInflight为您提供的保证是,在未确认消息的数量低于该数量之前,服务器不会向订户发送消息。因此,在MaxInflight(1)的情况下,您要求服务器仅在从先前传递的消息接收到ack之后才发送下一个新消息。但是,这不会阻止重新传递未确认的邮件。

服务器无法保证或不知道订户实际收到消息。这就是ACK的用途,让服务器知道该消息是由订户正确处理的。如果服务器不尊重重新传递(即使达到MaxInflight),那么"丢失"消息将永久停止您的订阅。请记住,NATS Streaming服务器和客户端没有通过TCP连接直接相互连接(它们都连接到NATS服务器,也就是gnatsd)。