Skip to content

Commit d8c118d

Browse files
committed
remove DispatchingAuthenticator since it wasn't thread-safe and was getting mixed up under high concurrency rburgst/okhttp-digest#33
1 parent 1616253 commit d8c118d

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed

src/main/java/com/marklogic/client/impl/OkHttpServices.java

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,6 @@
103103
import okio.Okio;
104104
import com.burgstaller.okhttp.AuthenticationCacheInterceptor;
105105
import com.burgstaller.okhttp.CachingAuthenticatorDecorator;
106-
import com.burgstaller.okhttp.DispatchingAuthenticator;
107106
import com.burgstaller.okhttp.basic.BasicAuthenticator;
108107
import com.burgstaller.okhttp.digest.CachingAuthenticator;
109108
import com.burgstaller.okhttp.digest.Credentials;
@@ -292,24 +291,22 @@ private void connect(String host, int port, String database, String user, String
292291

293292
Credentials credentials = new Credentials(user, password);
294293
final Map<String,CachingAuthenticator> authCache = new ConcurrentHashMap<String,CachingAuthenticator>();
295-
final BasicAuthenticator basicAuthenticator = new BasicAuthenticator(credentials);
296-
final DigestAuthenticator digestAuthenticator = new DigestAuthenticator(credentials);
297294

298295
if ( authenType == null && sslContext != null ) {
299296
authenType = Authentication.BASIC;
300297
}
301298

302-
DispatchingAuthenticator.Builder authenticator = new DispatchingAuthenticator.Builder();
299+
CachingAuthenticator authenticator = null;
303300
if (authenType == null) {
304301
checkFirstRequest = false;
305302
} else {
306303
if (user == null) throw new IllegalArgumentException("No user provided");
307304
if (password == null) throw new IllegalArgumentException("No password provided");
308305
if (authenType == Authentication.BASIC) {
309-
authenticator = authenticator.with("basic", basicAuthenticator);
306+
authenticator = new BasicAuthenticator(credentials);
310307
checkFirstRequest = false;
311308
} else if (authenType == Authentication.DIGEST) {
312-
authenticator = authenticator.with("digest", digestAuthenticator);
309+
authenticator = new DigestAuthenticator(credentials);
313310
checkFirstRequest = true;
314311
} else {
315312
throw new MarkLogicInternalException(
@@ -318,14 +315,20 @@ private void connect(String host, int port, String database, String user, String
318315
}
319316

320317
OkHttpClient.Builder clientBldr = new OkHttpClient.Builder()
321-
.authenticator(new CachingAuthenticatorDecorator(authenticator.build(), authCache))
322-
.addInterceptor(new AuthenticationCacheInterceptor(authCache))
323318
.followRedirects(false)
324319
.followSslRedirects(false)
325320
// all clients share a single connection pool
326321
.connectionPool(connectionPool)
327322
// cookies are ignored (except when a Transaction is being used)
328-
.cookieJar(CookieJar.NO_COOKIES);
323+
.cookieJar(CookieJar.NO_COOKIES)
324+
// no timeouts since some of our clients' reads and writes can be massive
325+
.readTimeout(0, TimeUnit.SECONDS)
326+
.writeTimeout(0, TimeUnit.SECONDS);
327+
328+
if ( authenticator != null ) {
329+
clientBldr = clientBldr.authenticator(new CachingAuthenticatorDecorator(authenticator, authCache));
330+
clientBldr = clientBldr.addInterceptor(new AuthenticationCacheInterceptor(authCache));
331+
}
329332

330333
if ( verifier != null ) {
331334
clientBldr = clientBldr.hostnameVerifier(verifier);

0 commit comments

Comments
 (0)