This repository has been archived by the owner on Sep 18, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 142
/
FinagleTransport.scala
123 lines (103 loc) · 4.51 KB
/
FinagleTransport.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/*
Copyright 2012 Twitter, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package com.twitter.parrot.server
import collection.mutable
import com.twitter.finagle.builder.ClientBuilder
import com.twitter.finagle.http.Http
import com.twitter.finagle.zipkin.thrift.ZipkinTracer
import com.twitter.finagle.stats.OstrichStatsReceiver
import com.twitter.finagle.{ ServiceFactory, Service }
import com.twitter.parrot.config.ParrotServerConfig
import com.twitter.util.{ Promise, Duration, Future }
import java.nio.ByteOrder.BIG_ENDIAN
import java.util.concurrent.TimeUnit
import org.jboss.netty.buffer.ChannelBuffers
import org.jboss.netty.handler.codec.http.HttpHeaders.Names.CONTENT_LENGTH
import org.jboss.netty.handler.codec.http._
import org.jboss.netty.util.CharsetUtil.UTF_8
import com.twitter.util._
object FinagleTransportFactory extends ParrotTransportFactory[ParrotRequest, HttpResponse] {
def apply(config: ParrotServerConfig[ParrotRequest, HttpResponse]) = {
val statsReceiver = new OstrichStatsReceiver
val builder = ClientBuilder()
.codec(Http())
.daemon(true)
.hostConnectionCoresize(config.hostConnectionCoresize)
.hostConnectionIdleTime(Duration(config.hostConnectionIdleTimeInMs, TimeUnit.MILLISECONDS))
.hostConnectionLimit(config.hostConnectionLimit)
.hostConnectionMaxIdleTime(Duration(config.hostConnectionMaxIdleTimeInMs, TimeUnit.MILLISECONDS))
.hostConnectionMaxLifeTime(Duration(config.hostConnectionMaxLifeTimeInMs, TimeUnit.MILLISECONDS))
.requestTimeout(Duration(config.requestTimeoutInMs, TimeUnit.MILLISECONDS))
.tcpConnectTimeout(Duration(config.tcpConnectTimeoutInMs, TimeUnit.MILLISECONDS))
.keepAlive(true)
.reportTo(statsReceiver)
.tracer(ZipkinTracer.mk(statsReceiver))
val builder2 = {
if (config.transportScheme == config.TransportScheme.HTTPS)
builder.tlsWithoutValidation()
else builder
}
val builder3 = config.victim.value match {
case config.HostPortListVictim(victims) => builder2.hosts(victims)
case config.ServerSetVictim(cluster) => builder2.cluster(cluster)
}
val service =
if (config.reuseConnections)
FinagleService(new RefcountedService(builder3.build()))
else
FinagleServiceFactory(builder3.buildFactory())
new FinagleTransport(service, config)
}
}
class FinagleTransport(service: FinagleServiceAbstraction, config: ParrotServerConfig[ParrotRequest, HttpResponse])
extends ParrotTransport[ParrotRequest, HttpResponse] {
var allRequests = 0
override def stats(response: HttpResponse) = Seq(response.getStatus.getCode.toString)
override protected[server] def sendRequest(request: ParrotRequest): Future[HttpResponse] = {
val requestMethod = request.method match {
case "POST" => HttpMethod.POST
case "PUT" => HttpMethod.PUT
case _ => HttpMethod.GET
}
val httpRequest =
new DefaultHttpRequest(HttpVersion.HTTP_1_1, requestMethod, request.uri.toString)
request.headers foreach {
case (key, value) =>
httpRequest.setHeader(key, value)
}
httpRequest.setHeader("Cookie", request.cookies map {
case (name, value) => name + "=" + value
} mkString (";"))
httpRequest.setHeader("User-Agent", "com.twitter.parrot")
if (config.includeParrotHeader) {
httpRequest.setHeader("X-Parrot", "true")
}
if (config.includeRandomXForwardedForHeader) {
httpRequest.setHeader("X-Forwarded-For", randomIp)
}
if ((request.method == "POST" || request.method == "PUT") && request.body.length > 0) {
val buffer = ChannelBuffers.copiedBuffer(BIG_ENDIAN, request.body, UTF_8)
httpRequest.setHeader(CONTENT_LENGTH, buffer.readableBytes)
httpRequest.setContent(buffer)
}
allRequests += 1
log.debug(
"""
===================== HttpRequest ======================
%s
========================================================"""
.format(httpRequest.toString))
service.send(httpRequest, request)
}
override def close(deadline: Time): Future[Unit] = service.close(deadline)
}