Redis tests now passes with new STM + misc minor changes to Cluster
This commit is contained in:
parent
73a0648292
commit
36c0266a5d
11 changed files with 131 additions and 96 deletions
|
|
@ -3,14 +3,14 @@ package se.scalablesolutions.akka.actor.patterns
|
|||
import se.scalablesolutions.akka.actor.Actor
|
||||
|
||||
object Patterns {
|
||||
type PF[A,B] = PartialFunction[A,B]
|
||||
type PF[A, B] = PartialFunction[A, B]
|
||||
|
||||
/**
|
||||
* Creates a new PartialFunction whose isDefinedAt is a combination
|
||||
* of the two parameters, and whose apply is first to call filter.apply and then filtered.apply
|
||||
*/
|
||||
def filter[A,B](filter : PF[A,Unit],filtered : PF[A,B]) : PF[A,B] = {
|
||||
case a : A if filtered.isDefinedAt(a) && filter.isDefinedAt(a) =>
|
||||
def filter[A, B](filter: PF[A, Unit], filtered: PF[A, B]): PF[A, B] = {
|
||||
case a: A if filtered.isDefinedAt(a) && filter.isDefinedAt(a) =>
|
||||
filter(a)
|
||||
filtered(a)
|
||||
}
|
||||
|
|
@ -18,39 +18,42 @@ object Patterns {
|
|||
/**
|
||||
* Interceptor is a filter(x,y) where x.isDefinedAt is considered to be always true
|
||||
*/
|
||||
def intercept[A,B](interceptor : (A) => Unit, interceptee : PF[A,B]) : PF[A,B] = filter(
|
||||
{ case a if a.isInstanceOf[A] => interceptor(a) },
|
||||
interceptee
|
||||
def intercept[A, B](interceptor: (A) => Unit, interceptee: PF[A, B]): PF[A, B] = filter(
|
||||
{case a if a.isInstanceOf[A] => interceptor(a)},
|
||||
interceptee
|
||||
)
|
||||
|
||||
|
||||
//FIXME 2.8, use default params with CyclicIterator
|
||||
def loadBalancerActor(actors : => InfiniteIterator[Actor]) : Actor = new Actor with LoadBalancer {
|
||||
def loadBalancerActor(actors: => InfiniteIterator[Actor]): Actor = new Actor with LoadBalancer {
|
||||
val seq = actors
|
||||
}
|
||||
|
||||
def dispatcherActor(routing : PF[Any,Actor], msgTransformer : (Any) => Any) : Actor = new Actor with Dispatcher {
|
||||
override def transform(msg : Any) = msgTransformer(msg)
|
||||
def dispatcherActor(routing: PF[Any, Actor], msgTransformer: (Any) => Any): Actor = new Actor with Dispatcher {
|
||||
override def transform(msg: Any) = msgTransformer(msg)
|
||||
|
||||
def routes = routing
|
||||
}
|
||||
|
||||
def dispatcherActor(routing : PF[Any,Actor]) : Actor = new Actor with Dispatcher {
|
||||
def routes = routing
|
||||
|
||||
def dispatcherActor(routing: PF[Any, Actor]): Actor = new Actor with Dispatcher {
|
||||
def routes = routing
|
||||
}
|
||||
|
||||
def loggerActor(actorToLog : Actor, logger : (Any) => Unit) : Actor = dispatcherActor (
|
||||
{ case _ => actorToLog },
|
||||
def loggerActor(actorToLog: Actor, logger: (Any) => Unit): Actor = dispatcherActor(
|
||||
{case _ => actorToLog},
|
||||
logger
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
trait Dispatcher { self : Actor =>
|
||||
trait Dispatcher {
|
||||
self: Actor =>
|
||||
|
||||
protected def transform(msg : Any) : Any = msg
|
||||
protected def routes : PartialFunction[Any,Actor]
|
||||
|
||||
protected def dispatch : PartialFunction[Any,Unit] = {
|
||||
protected def transform(msg: Any): Any = msg
|
||||
|
||||
protected def routes: PartialFunction[Any, Actor]
|
||||
|
||||
protected def dispatch: PartialFunction[Any, Unit] = {
|
||||
case a if routes.isDefinedAt(a) => {
|
||||
if(self.sender.isDefined)
|
||||
if (self.sender.isDefined)
|
||||
routes(a) forward transform(a)
|
||||
else
|
||||
routes(a) send transform(a)
|
||||
|
|
@ -60,19 +63,22 @@ trait Dispatcher { self : Actor =>
|
|||
def receive = dispatch
|
||||
}
|
||||
|
||||
trait LoadBalancer extends Dispatcher { self : Actor =>
|
||||
protected def seq : InfiniteIterator[Actor]
|
||||
trait LoadBalancer extends Dispatcher {
|
||||
self: Actor =>
|
||||
protected def seq: InfiniteIterator[Actor]
|
||||
|
||||
protected def routes = { case x if seq.hasNext => seq.next }
|
||||
protected def routes = {case x if seq.hasNext => seq.next}
|
||||
}
|
||||
|
||||
trait InfiniteIterator[T] extends Iterator[T]
|
||||
|
||||
class CyclicIterator[T](items : List[T]) extends InfiniteIterator[T] {
|
||||
@volatile private[this] var current : List[T] = items
|
||||
class CyclicIterator[T](items: List[T]) extends InfiniteIterator[T] {
|
||||
@volatile private[this] var current: List[T] = items
|
||||
|
||||
def hasNext = items != Nil
|
||||
|
||||
def next = {
|
||||
val nc = if(current == Nil) items else current
|
||||
val nc = if (current == Nil) items else current
|
||||
current = nc.tail
|
||||
nc.head
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue