This repository has been archived by the owner on Aug 11, 2020. It is now read-only.
/
Hc4ProviderImpl.java
297 lines (258 loc) · 11.3 KB
/
Hc4ProviderImpl.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/*
* Sonatype Nexus (TM) Open Source Version
* Copyright (c) 2007-2012 Sonatype, Inc.
* All rights reserved. Includes the third-party code listed at http://links.sonatype.com/products/nexus/oss/attributions.
*
* This program and the accompanying materials are made available under the terms of the Eclipse Public License Version 1.0,
* which accompanies this distribution and is available at http://www.eclipse.org/legal/epl-v10.html.
*
* Sonatype Nexus (TM) Professional Version is available from Sonatype, Inc. "Sonatype" and "Sonatype Nexus" are trademarks
* of Sonatype, Inc. Apache Maven is a trademark of the Apache Software Foundation. M2eclipse is a trademark of the
* Eclipse Foundation. All other trademarks are the property of their respective owners.
*/
package org.sonatype.nexus.apachehttpclient;
import java.util.List;
import java.util.concurrent.TimeUnit;
import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;
import com.yammer.metrics.httpclient.InstrumentedClientConnManager;
import org.apache.http.client.params.ClientPNames;
import org.apache.http.conn.ClientConnectionOperator;
import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.impl.NoConnectionReuseStrategy;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.PoolingClientConnectionManager;
import org.apache.http.params.HttpParams;
import org.sonatype.nexus.configuration.application.ApplicationConfiguration;
import org.sonatype.nexus.configuration.application.GlobalRemoteConnectionSettings;
import org.sonatype.nexus.proxy.events.NexusStoppedEvent;
import org.sonatype.nexus.proxy.storage.remote.RemoteStorageContext;
import org.sonatype.nexus.proxy.utils.UserAgentBuilder;
import org.sonatype.nexus.util.SystemPropertiesHelper;
import org.sonatype.sisu.goodies.eventbus.EventBus;
import com.google.common.base.Preconditions;
import com.google.common.eventbus.Subscribe;
/**
* Default implementation of {@link Hc4Provider}.
*
* @author cstamas
* @since 2.2
*/
@Singleton
@Named
public class Hc4ProviderImpl
extends Hc4ProviderBase
implements Hc4Provider
{
/**
* Key for customizing connection pool maximum size. Value should be integer equal to 0 or greater. Pool size of 0
* will actually prevent use of pool. Any positive number means the actual size of the pool to be created. This is a
* hard limit, connection pool will never contain more than this count of open sockets.
*/
private static final String CONNECTION_POOL_MAX_SIZE_KEY = "nexus.apacheHttpClient4x.connectionPoolMaxSize";
/**
* Default pool max size: 200.
*/
private static final int CONNECTION_POOL_MAX_SIZE_DEFAULT = 200;
/**
* Key for customizing connection pool size per route (usually per-repository, but not quite in case of Mirrors).
* Value should be integer equal to 0 or greater. Pool size of 0 will actually prevent use of pool. Any positive
* number means the actual size of the pool to be created.
*/
private static final String CONNECTION_POOL_SIZE_KEY = "nexus.apacheHttpClient4x.connectionPoolSize";
/**
* Default pool size: 20.
*/
private static final int CONNECTION_POOL_SIZE_DEFAULT = 20;
/**
* Key for customizing connection pool idle time. In other words, how long open connections (sockets) are kept in
* pool idle (unused) before they get evicted and closed. Value is milliseconds.
*/
private static final String CONNECTION_POOL_IDLE_TIME_KEY = "nexus.apacheHttpClient4x.connectionPoolIdleTime";
/**
* Default pool idle time: 30 seconds.
*/
private static final long CONNECTION_POOL_IDLE_TIME_DEFAULT = TimeUnit.SECONDS.toMillis( 30 );
/**
* Key for customizing connection pool timeout. In other words, how long should a HTTP request execution be blocked
* when pool is depleted, for a connection. Value is milliseconds.
*/
private static final String CONNECTION_POOL_TIMEOUT_KEY = "nexus.apacheHttpClient4x.connectionPoolTimeout";
/**
* Default pool timeout: 30 seconds.
*/
private static final long CONNECTION_POOL_TIMEOUT_DEFAULT = TimeUnit.SECONDS.toMillis( 30 );
// ==
/**
* Application configuration holding the {@link GlobalRemoteConnectionSettings}.
*/
private final ApplicationConfiguration applicationConfiguration;
/**
* The low level core event bus.
*/
private final EventBus eventBus;
/**
* Shared client connection manager.
*/
private final PoolingClientConnectionManager sharedConnectionManager;
/**
* Thread evicting idle open connections from {@link #sharedConnectionManager}.
*/
private final EvictingThread evictingThread;
/**
* Used to install created {@link PoolingClientConnectionManager} into jmx.
*/
private final PoolingClientConnectionManagerMBeanInstaller jmxInstaller;
/**
* Constructor.
*
* @param applicationConfiguration the Nexus {@link ApplicationConfiguration}, must not be {@code null}.
* @param userAgentBuilder UA builder component, must not be {@code null}.
* @param eventBus the event bus, must not be {@code null}.
* @param jmxInstaller installer to expose pool information over JMX, must not be {@code null}.
* @param selectors list of {@link ClientConnectionOperatorSelector}, might be {@code null}.
*/
@Inject
public Hc4ProviderImpl( final ApplicationConfiguration applicationConfiguration,
final UserAgentBuilder userAgentBuilder, final EventBus eventBus,
final PoolingClientConnectionManagerMBeanInstaller jmxInstaller,
final List<ClientConnectionOperatorSelector> selectors )
{
super( userAgentBuilder );
this.applicationConfiguration = Preconditions.checkNotNull( applicationConfiguration );
this.jmxInstaller = Preconditions.checkNotNull( jmxInstaller );
this.sharedConnectionManager = createClientConnectionManager( selectors );
this.evictingThread = new EvictingThread( sharedConnectionManager, getConnectionPoolIdleTime() );
this.evictingThread.start();
this.eventBus = Preconditions.checkNotNull( eventBus );
this.eventBus.register( this );
this.jmxInstaller.register( sharedConnectionManager );
getLogger().info(
"{} started (connectionPoolMaxSize {}, connectionPoolSize {}, connectionPoolIdleTime {} ms, connectionPoolTimeout {} ms, keepAliveMaxDuration {} ms)",
getClass().getSimpleName(), getConnectionPoolMaxSize(), getConnectionPoolSize(),
getConnectionPoolIdleTime(), getConnectionPoolTimeout(), getKeepAliveMaxDuration() );
}
// configuration
/**
* Returns the pool max size.
*
* @return pool max size
*/
protected int getConnectionPoolMaxSize()
{
return SystemPropertiesHelper.getInteger( CONNECTION_POOL_MAX_SIZE_KEY, CONNECTION_POOL_MAX_SIZE_DEFAULT );
}
/**
* Returns the pool size per route.
*
* @return pool per route size
*/
protected int getConnectionPoolSize()
{
return SystemPropertiesHelper.getInteger( CONNECTION_POOL_SIZE_KEY, CONNECTION_POOL_SIZE_DEFAULT );
}
/**
* Returns the connection pool idle (idle as unused but pooled) time in milliseconds.
*
* @return idle time in milliseconds.
*/
protected long getConnectionPoolIdleTime()
{
return SystemPropertiesHelper.getLong( CONNECTION_POOL_IDLE_TIME_KEY, CONNECTION_POOL_IDLE_TIME_DEFAULT );
}
/**
* Returns the pool timeout in milliseconds.
*
* @return pool timeout in milliseconds.
*/
protected long getConnectionPoolTimeout()
{
return SystemPropertiesHelper.getLong( CONNECTION_POOL_TIMEOUT_KEY, CONNECTION_POOL_TIMEOUT_DEFAULT );
}
// ==
/**
* Performs a clean shutdown on this component, it kills the evicting thread and shuts down the shared connection
* manager. Multiple invocation of this method is safe, it will not do anything.
*/
public synchronized void shutdown()
{
evictingThread.interrupt();
jmxInstaller.unregister();
sharedConnectionManager.shutdown();
eventBus.unregister( this );
getLogger().info( "{} stopped.", getClass().getSimpleName() );
}
@Subscribe
public void onEvent( final NexusStoppedEvent evt )
{
shutdown();
}
// ==
/**
* Safety net to prevent thread leaks (in non-production environment, mainly for ITs or UTs).
*/
@Override
protected void finalize()
throws Throwable
{
try
{
shutdown();
}
finally
{
super.finalize();
}
}
// == Hc4Provider API
@Override
public DefaultHttpClient createHttpClient()
{
final DefaultHttpClient result = createHttpClient( applicationConfiguration.getGlobalRemoteStorageContext() );
// connection manager will cap the max count of connections, but with this below
// we get rid of pooling. Pooling is used in Proxy repositories only, as all other
// components using the "shared" httpClient should not produce hiw rate of requests
// anyway, as they usually happen per user interactions (GPG gets keys are staging repo is closed, if not cached
// yet, LVO gets info when UI's main window is loaded into user's browser, etc
result.setReuseStrategy( new NoConnectionReuseStrategy() );
return result;
}
@Override
public DefaultHttpClient createHttpClient( final RemoteStorageContext context )
{
return createHttpClient( context, sharedConnectionManager );
}
// ==
@Override
protected HttpParams createHttpParams( final RemoteStorageContext context )
{
final HttpParams params = super.createHttpParams( context );
params.setLongParameter( ClientPNames.CONN_MANAGER_TIMEOUT, getConnectionPoolTimeout() );
return params;
}
protected PoolingClientConnectionManager createClientConnectionManager(
final List<ClientConnectionOperatorSelector> selectors )
throws IllegalStateException
{
final SchemeRegistry schemeRegistry = new SchemeRegistry();
schemeRegistry.register( new Scheme( "http", 80, PlainSocketFactory.getSocketFactory() ) );
schemeRegistry.register( new Scheme( "https", 443, SSLSocketFactory.getSocketFactory() ) );
final PoolingClientConnectionManager connManager = new InstrumentedClientConnManager( schemeRegistry )
{
@Override
protected ClientConnectionOperator createConnectionOperator( final SchemeRegistry defaultSchemeRegistry )
{
return new Hc4ClientConnectionOperator( defaultSchemeRegistry, selectors );
}
};
final int maxConnectionCount = getConnectionPoolMaxSize();
final int perRouteConnectionCount = Math.min( getConnectionPoolSize(), maxConnectionCount );
connManager.setMaxTotal( maxConnectionCount );
connManager.setDefaultMaxPerRoute( perRouteConnectionCount );
return connManager;
}
}