Created
April 24, 2016 18:10
-
-
Save notxcain/3a73bd1a423345f39abeae5d43eeaa74 to your computer and use it in GitHub Desktop.
Ordered At-Least-Once delivery for Akka
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
trait OrderedAtLeastOnceDelivery extends AtLeastOnceDelivery { | |
type DeliveryId = Long | |
private case class Delivery(destination: ActorPath, deliveryIdToMessage: (DeliveryId) => Any) | |
private val deliveryQueue = scala.collection.mutable.Queue.empty[Delivery] | |
override def deliver(destination: ActorPath)(deliveryIdToMessage: (DeliveryId) => Any): Unit = { | |
if (super.numberOfUnconfirmed == 0) { | |
super.deliver(destination)(deliveryIdToMessage) | |
} else { | |
deliveryQueue.enqueue(Delivery(destination, deliveryIdToMessage)) | |
} | |
} | |
override def numberOfUnconfirmed: Int = deliveryQueue.length + super.numberOfUnconfirmed | |
override def confirmDelivery(deliveryId: DeliveryId): Boolean = { | |
if (deliveryQueue.nonEmpty) { | |
val Delivery(destination, deliveryIdToMessage) = deliveryQueue.dequeue() | |
super.deliver(destination)(deliveryIdToMessage) | |
} | |
super.confirmDelivery(deliveryId) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment