/
InstrumentedHttpClientConnectionManager.java
200 lines (170 loc) · 8.74 KB
/
InstrumentedHttpClientConnectionManager.java
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package com.codahale.metrics.httpclient;
import com.codahale.metrics.MetricRegistry;
import org.apache.http.config.Registry;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.conn.DnsResolver;
import org.apache.http.conn.HttpClientConnectionManager;
import org.apache.http.conn.HttpClientConnectionOperator;
import org.apache.http.conn.HttpConnectionFactory;
import org.apache.http.conn.ManagedHttpClientConnection;
import org.apache.http.conn.SchemePortResolver;
import org.apache.http.conn.routing.HttpRoute;
import org.apache.http.conn.socket.ConnectionSocketFactory;
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.impl.conn.DefaultHttpClientConnectionOperator;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.impl.conn.SystemDefaultDnsResolver;
import java.util.concurrent.TimeUnit;
import static com.codahale.metrics.MetricRegistry.name;
/**
* A {@link HttpClientConnectionManager} which monitors the number of open connections.
*/
public class InstrumentedHttpClientConnectionManager extends PoolingHttpClientConnectionManager {
protected static Registry<ConnectionSocketFactory> getDefaultRegistry() {
return RegistryBuilder.<ConnectionSocketFactory>create()
.register("http", PlainConnectionSocketFactory.getSocketFactory())
.register("https", SSLConnectionSocketFactory.getSocketFactory())
.build();
}
private final MetricRegistry metricsRegistry;
private final String name;
/**
* @deprecated Use {@link #builder(MetricRegistry)} instead.
*/
@Deprecated
public InstrumentedHttpClientConnectionManager(MetricRegistry metricRegistry) {
this(metricRegistry, getDefaultRegistry());
}
/**
* @deprecated Use {@link #builder(MetricRegistry)} instead.
*/
@Deprecated
public InstrumentedHttpClientConnectionManager(MetricRegistry metricsRegistry,
Registry<ConnectionSocketFactory> socketFactoryRegistry) {
this(metricsRegistry, socketFactoryRegistry, -1, TimeUnit.MILLISECONDS);
}
/**
* @deprecated Use {@link #builder(MetricRegistry)} instead.
*/
@Deprecated
public InstrumentedHttpClientConnectionManager(MetricRegistry metricsRegistry,
Registry<ConnectionSocketFactory> socketFactoryRegistry,
long connTTL,
TimeUnit connTTLTimeUnit) {
this(metricsRegistry, socketFactoryRegistry, null, null, SystemDefaultDnsResolver.INSTANCE, connTTL, connTTLTimeUnit, null);
}
/**
* @deprecated Use {@link #builder(MetricRegistry)} instead.
*/
@Deprecated
public InstrumentedHttpClientConnectionManager(MetricRegistry metricsRegistry,
Registry<ConnectionSocketFactory> socketFactoryRegistry,
HttpConnectionFactory<HttpRoute, ManagedHttpClientConnection>
connFactory,
SchemePortResolver schemePortResolver,
DnsResolver dnsResolver,
long connTTL,
TimeUnit connTTLTimeUnit,
String name) {
this(metricsRegistry,
new DefaultHttpClientConnectionOperator(socketFactoryRegistry, schemePortResolver, dnsResolver),
connFactory,
connTTL,
connTTLTimeUnit,
name);
}
/**
* @deprecated Use {@link #builder(MetricRegistry)} instead.
*/
@Deprecated
public InstrumentedHttpClientConnectionManager(MetricRegistry metricsRegistry,
HttpClientConnectionOperator httpClientConnectionOperator,
HttpConnectionFactory<HttpRoute, ManagedHttpClientConnection>
connFactory,
long connTTL,
TimeUnit connTTLTimeUnit,
String name) {
super(httpClientConnectionOperator, connFactory, connTTL, connTTLTimeUnit);
this.metricsRegistry = metricsRegistry;
this.name = name;
// this acquires a lock on the connection pool; remove if contention sucks
metricsRegistry.registerGauge(name(HttpClientConnectionManager.class, name, "available-connections"),
() -> getTotalStats().getAvailable());
// this acquires a lock on the connection pool; remove if contention sucks
metricsRegistry.registerGauge(name(HttpClientConnectionManager.class, name, "leased-connections"),
() -> getTotalStats().getLeased());
// this acquires a lock on the connection pool; remove if contention sucks
metricsRegistry.registerGauge(name(HttpClientConnectionManager.class, name, "max-connections"),
() -> getTotalStats().getMax());
// this acquires a lock on the connection pool; remove if contention sucks
metricsRegistry.registerGauge(name(HttpClientConnectionManager.class, name, "pending-connections"),
() -> getTotalStats().getPending());
}
@Override
public void shutdown() {
super.shutdown();
metricsRegistry.remove(name(HttpClientConnectionManager.class, name, "available-connections"));
metricsRegistry.remove(name(HttpClientConnectionManager.class, name, "leased-connections"));
metricsRegistry.remove(name(HttpClientConnectionManager.class, name, "max-connections"));
metricsRegistry.remove(name(HttpClientConnectionManager.class, name, "pending-connections"));
}
public static Builder builder(MetricRegistry metricsRegistry) {
return new Builder().metricsRegistry(metricsRegistry);
}
public static class Builder {
private MetricRegistry metricsRegistry;
private HttpClientConnectionOperator httpClientConnectionOperator;
private Registry<ConnectionSocketFactory> socketFactoryRegistry = getDefaultRegistry();
private HttpConnectionFactory<HttpRoute, ManagedHttpClientConnection> connFactory;
private SchemePortResolver schemePortResolver;
private DnsResolver dnsResolver = SystemDefaultDnsResolver.INSTANCE;
private long connTTL = -1;
private TimeUnit connTTLTimeUnit = TimeUnit.MILLISECONDS;
private String name;
Builder() {
}
public Builder metricsRegistry(MetricRegistry metricsRegistry) {
this.metricsRegistry = metricsRegistry;
return this;
}
public Builder socketFactoryRegistry(Registry<ConnectionSocketFactory> socketFactoryRegistry) {
this.socketFactoryRegistry = socketFactoryRegistry;
return this;
}
public Builder connFactory(HttpConnectionFactory<HttpRoute, ManagedHttpClientConnection> connFactory) {
this.connFactory = connFactory;
return this;
}
public Builder schemePortResolver(SchemePortResolver schemePortResolver) {
this.schemePortResolver = schemePortResolver;
return this;
}
public Builder dnsResolver(DnsResolver dnsResolver) {
this.dnsResolver = dnsResolver;
return this;
}
public Builder connTTL(long connTTL) {
this.connTTL = connTTL;
return this;
}
public Builder connTTLTimeUnit(TimeUnit connTTLTimeUnit) {
this.connTTLTimeUnit = connTTLTimeUnit;
return this;
}
public Builder httpClientConnectionOperator(HttpClientConnectionOperator httpClientConnectionOperator) {
this.httpClientConnectionOperator = httpClientConnectionOperator;
return this;
}
public Builder name(final String name) {
this.name = name;
return this;
}
public InstrumentedHttpClientConnectionManager build() {
if (httpClientConnectionOperator == null) {
httpClientConnectionOperator = new DefaultHttpClientConnectionOperator(socketFactoryRegistry, schemePortResolver, dnsResolver);
}
return new InstrumentedHttpClientConnectionManager(metricsRegistry, httpClientConnectionOperator, connFactory, connTTL, connTTLTimeUnit, name);
}
}
}