in finagle-thrift/src/main/scala/com/twitter/finagle/Thrift.scala [331:459]
def framePartitionedRequest(
rawRequest: ThriftClientRequest,
original: ThriftClientRequest
): ThriftClientRequest = rawRequest
def isOneway(original: ThriftClientRequest): Boolean = original.oneway
def fromResponseToBytes(rep: Array[Byte]): Array[Byte] = rep
val emptyResponse: Array[Byte] = Array.emptyByteArray
}
// we insert validationReporting filter to thrift client after the statsFilter
// because we will be able to see all metrics the statsFilter sees
StackClient.newStack
.replace(StackClient.Role.prepConn, preparer)
.insertAfter(BindingFactory.role, ThriftPartitioningService.module(ThriftMarshallable))
.insertAfter(
StatsFilter.role,
ValidationReportingFilter.module[ThriftClientRequest, Array[Byte]])
}
private def params: Stack.Params = StackClient.defaultParams +
ProtocolLibrary(protocolLibraryName)
}
/**
* A ThriftMux `com.twitter.finagle.Client`.
*
* @see [[https://twitter.github.io/finagle/guide/Configuration.html#clients-and-servers Configuration]] documentation
* @see [[https://twitter.github.io/finagle/guide/Protocols.html#thrift Thrift]] documentation
* @see [[https://twitter.github.io/finagle/guide/Protocols.html#mux Mux]] documentation
*/
case class Client(
stack: Stack[ServiceFactory[ThriftClientRequest, Array[Byte]]] = Client.stack,
params: Stack.Params = Client.params)
extends StdStackClient[ThriftClientRequest, Array[Byte], Client]
with WithSessionPool[Client]
with WithDefaultLoadBalancer[Client]
with WithThriftPartitioningStrategy[Client]
with ThriftRichClient {
protected def copy1(
stack: Stack[ServiceFactory[ThriftClientRequest, Array[Byte]]] = this.stack,
params: Stack.Params = this.params
): Client = copy(stack, params)
protected val clientParam: RichClientParam = RichClientParam(
protocolFactory = params[param.ProtocolFactory].protocolFactory,
maxThriftBufferSize = params[Thrift.param.MaxReusableBufferSize].maxReusableBufferSize,
thriftReusableBufferFactory =
params[Thrift.param.TReusableBufferFactory].tReusableBufferFactory,
clientStats = params[Stats].statsReceiver,
responseClassifier = params[com.twitter.finagle.param.ResponseClassifier].responseClassifier,
perEndpointStats = params[Thrift.param.PerEndpointStats].enabled
)
protected lazy val Label(defaultClientName) = params[Label]
protected type In = ThriftClientRequest
protected type Out = Array[Byte]
protected type Context = TransportContext
protected def newTransporter(addr: SocketAddress): Transporter[In, Out, Context] =
Netty4Transport.Client(params)(addr)
protected def newDispatcher(
transport: Transport[ThriftClientRequest, Array[Byte]] { type Context <: Client.this.Context }
): Service[ThriftClientRequest, Array[Byte]] =
new ThriftSerialClientDispatcher(
transport,
params[Stats].statsReceiver.scope(ClientDispatcher.StatsScope)
)
def withProtocolFactory(protocolFactory: TProtocolFactory): Client =
configured(param.ProtocolFactory(protocolFactory))
def withClientId(clientId: FinagleClientId): Client =
configured(Thrift.param.ClientId(Some(clientId)))
/**
* Use a buffered transport instead of the default framed transport.
* In almost all cases, the default framed transport should be used.
*/
def withBufferedTransport: Client =
configured(Thrift.param.Framed(false))
def withAttemptTTwitterUpgrade: Client =
configured(param.AttemptTTwitterUpgrade(true))
def withNoAttemptTTwitterUpgrade: Client =
configured(param.AttemptTTwitterUpgrade(false))
/**
* Produce a [[com.twitter.finagle.Thrift.Client]] with the specified max
* size of the reusable buffer for thrift responses. If this size
* is exceeded, the buffer is not reused and a new buffer is
* allocated for the next thrift response.
* The default max size is 16Kb.
*
* @note MaxReusableBufferSize will be ignored if TReusableBufferFactory is set.
*
* @param size Max size of the reusable buffer for thrift responses in bytes.
*/
def withMaxReusableBufferSize(size: Int): Client =
configured(param.MaxReusableBufferSize(size))
/**
* Produce a [[com.twitter.finagle.Thrift.Client]] with a factory creates new
* TReusableBuffer, the TReusableBuffer can be shared with other client instance.
* If set, the MaxReusableBufferSize will be ignored.
*/
def withTReusableBufferFactory(tReusableBufferFactory: () => TReusableBuffer): Client =
configured(param.TReusableBufferFactory(tReusableBufferFactory))
/**
* Produce a [[com.twitter.finagle.Thrift.Client]] with per-endpoint stats filters
*/
def withPerEndpointStats: Client =
configured(param.PerEndpointStats(true))
def clientId: Option[FinagleClientId] = params[Thrift.param.ClientId].clientId
private[this] def withDeserializingClassifier: Client = {
// Note: what type of deserializer used is important if none is specified
// so that we keep the prior behavior of Thrift exceptions
// being counted as a success. Otherwise, even using the default
// ResponseClassifier would then see that response as a `Throw` and thus
// a failure. So, when none is specified, a "deserializing-only"
// classifier is used to make when deserialization happens in the stack
// uniform whether or not a `ResponseClassifier` is wired up.
val classifier = if (params.contains[com.twitter.finagle.param.ResponseClassifier]) {