/
ConcurrentLoadBalancerFactory.scala
50 lines (45 loc) · 1.64 KB
/
ConcurrentLoadBalancerFactory.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package com.twitter.finagle.loadbalancer
import com.twitter.finagle._
import com.twitter.util.Activity
import java.net.SocketAddress
import java.util.logging.Level
/**
* A load balancer that balances among multiple connections,
* useful for managing concurrency in pipelining protocols.
*
* Each endpoint can open multiple connections. For N endpoints,
* each opens M connections, load balancer balances among N*M
* options. Thus, it increases concurrency of each endpoint.
*/
private[finagle] object ConcurrentLoadBalancerFactory {
import LoadBalancerFactory._
/**
* A class eligible for configuring the number of connections
* a single endpoint has.
*/
case class Param(numConnections: Int) {
def mk(): (Param, Stack.Param[Param]) = (this, Param.param)
}
object Param {
implicit val param = Stack.Param(Param(4))
}
def module[Req, Rep]: Stackable[ServiceFactory[Req, Rep]] =
new BalancerStackModule[Req, Rep] {
val description = "Balance requests across multiple connections on a single endpoint, used for pipelining protocols"
val parameters = Seq(
implicitly[Stack.Param[ErrorLabel]],
implicitly[Stack.Param[Dest]],
implicitly[Stack.Param[param.Stats]],
implicitly[Stack.Param[param.Logger]],
implicitly[Stack.Param[param.Monitor]],
implicitly[Stack.Param[param.Reporter]],
implicitly[Stack.Param[Param]])
override protected def processAddrs(
params: Stack.Params,
addrs: Set[SocketAddress]
): Set[SocketAddress] = {
val n = params[Param].numConnections
addrs.flatMap(SocketAddresses.replicate(n))
}
}
}