Skip to content

Commit 1ee1bb2

Browse files
committed
add property http.client.useThreadLocal
1 parent b40c85b commit 1ee1bb2

File tree

3 files changed

+22
-10
lines changed

3 files changed

+22
-10
lines changed

server/src/main/java/password/pwm/AppProperty.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ public enum AppProperty
133133
HTTP_CLIENT_IMPLEMENTATION ( "http.client.implementation" ),
134134
HTTP_CLIENT_ENABLE_HOSTNAME_VERIFICATION ( "http.client.enableHostnameVerification" ),
135135
HTTP_CLIENT_PROMISCUOUS_WORDLIST_ENABLE ( "http.client.promiscuous.wordlist.enable" ),
136+
HTTP_CLIENT_USE_THREAD_LOCAL ( "http.client.useThreadLocal" ),
136137
HTTP_ENABLE_GZIP ( "http.gzip.enable" ),
137138
HTTP_ERRORS_ALLOW_HTML ( "http.errors.allowHtml" ),
138139
HTTP_HEADER_SERVER ( "http.header.server" ),

server/src/main/java/password/pwm/svc/httpclient/HttpClientService.java

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public class HttpClientService extends AbstractPwmService implements PwmService
4848

4949
private Class<PwmHttpClientProvider> httpClientClass;
5050
private PwmApplication pwmApplication;
51-
51+
private boolean useThreadLocals = false;
5252
private final Map<PwmHttpClientConfiguration, ThreadLocal<PwmHttpClientProvider>> clients = new ConcurrentHashMap<>( );
5353
private final Map<PwmHttpClientProvider, Object> issuedClients = Collections.synchronizedMap( new WeakHashMap<>( ) );
5454

@@ -72,6 +72,8 @@ public void init( final PwmApplication pwmApplication )
7272
throws PwmException
7373
{
7474
this.pwmApplication = pwmApplication;
75+
this.useThreadLocals = pwmApplication.getConfig().readBooleanAppProperty( AppProperty.HTTP_CLIENT_USE_THREAD_LOCAL );
76+
7577
final String implClassName = pwmApplication.getConfig().readAppProperty( AppProperty.HTTP_CLIENT_IMPLEMENTATION );
7678
try
7779
{
@@ -115,23 +117,31 @@ public PwmHttpClient getPwmHttpClient( final PwmHttpClientConfiguration pwmHttpC
115117
{
116118
Objects.requireNonNull( pwmHttpClientConfiguration );
117119

118-
final ThreadLocal<PwmHttpClientProvider> threadLocal = clients.computeIfAbsent(
119-
pwmHttpClientConfiguration,
120-
clientConfig -> new ThreadLocal<>() );
121-
122-
final PwmHttpClient existingClient = threadLocal.get();
123-
if ( existingClient != null && existingClient.isOpen() )
120+
if ( useThreadLocals )
124121
{
125-
stats.increment( StatsKey.reusedClients );
126-
return existingClient;
122+
final ThreadLocal<PwmHttpClientProvider> threadLocal = clients.computeIfAbsent(
123+
pwmHttpClientConfiguration,
124+
clientConfig -> new ThreadLocal<>() );
125+
126+
final PwmHttpClient existingClient = threadLocal.get();
127+
if ( existingClient != null && existingClient.isOpen() )
128+
{
129+
stats.increment( StatsKey.reusedClients );
130+
return existingClient;
131+
}
127132
}
128133

129134
try
130135
{
131136
final PwmHttpClientProvider newClient = httpClientClass.getDeclaredConstructor().newInstance();
132137
newClient.init( pwmApplication, this, pwmHttpClientConfiguration );
133138
issuedClients.put( newClient, null );
134-
threadLocal.set( newClient );
139+
140+
if ( useThreadLocals )
141+
{
142+
clients.get( pwmHttpClientConfiguration ).set( newClient );
143+
}
144+
135145
stats.increment( StatsKey.createdClients );
136146
return newClient;
137147
}

server/src/main/resources/password/pwm/AppProperty.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ http.client.response.maxSize=20000000
133133
http.client.enableHostnameVerification=true
134134
http.client.promiscuous.wordlist.enable=true
135135
http.client.implementation=password.pwm.svc.httpclient.ApachePwmHttpClient
136+
http.client.useThreadLocal=false
136137
http.header.server=@PwmAppName@
137138
http.header.sendContentLanguage=true
138139
http.header.sendXAmb=true

0 commit comments

Comments
 (0)