@@ -48,7 +48,7 @@ public class HttpClientService extends AbstractPwmService implements PwmService
48
48
49
49
private Class <PwmHttpClientProvider > httpClientClass ;
50
50
private PwmApplication pwmApplication ;
51
-
51
+ private boolean useThreadLocals = false ;
52
52
private final Map <PwmHttpClientConfiguration , ThreadLocal <PwmHttpClientProvider >> clients = new ConcurrentHashMap <>( );
53
53
private final Map <PwmHttpClientProvider , Object > issuedClients = Collections .synchronizedMap ( new WeakHashMap <>( ) );
54
54
@@ -72,6 +72,8 @@ public void init( final PwmApplication pwmApplication )
72
72
throws PwmException
73
73
{
74
74
this .pwmApplication = pwmApplication ;
75
+ this .useThreadLocals = pwmApplication .getConfig ().readBooleanAppProperty ( AppProperty .HTTP_CLIENT_USE_THREAD_LOCAL );
76
+
75
77
final String implClassName = pwmApplication .getConfig ().readAppProperty ( AppProperty .HTTP_CLIENT_IMPLEMENTATION );
76
78
try
77
79
{
@@ -115,23 +117,31 @@ public PwmHttpClient getPwmHttpClient( final PwmHttpClientConfiguration pwmHttpC
115
117
{
116
118
Objects .requireNonNull ( pwmHttpClientConfiguration );
117
119
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 )
124
121
{
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
+ }
127
132
}
128
133
129
134
try
130
135
{
131
136
final PwmHttpClientProvider newClient = httpClientClass .getDeclaredConstructor ().newInstance ();
132
137
newClient .init ( pwmApplication , this , pwmHttpClientConfiguration );
133
138
issuedClients .put ( newClient , null );
134
- threadLocal .set ( newClient );
139
+
140
+ if ( useThreadLocals )
141
+ {
142
+ clients .get ( pwmHttpClientConfiguration ).set ( newClient );
143
+ }
144
+
135
145
stats .increment ( StatsKey .createdClients );
136
146
return newClient ;
137
147
}
0 commit comments