@@ -115,7 +115,7 @@ public ApiClient() {
115115 }
116116
117117 public ApiClient (String basePath , Realm realm , String appKey , String clientId , String clientSecret , String appId , String appCert , int maxIdleConnections , int connectKeepAliveMilliSeconds ,
118- int connectTimeoutMilliSeconds , EMProxy proxy )
118+ int connectTimeoutMilliSeconds , EMProxy proxy , int dispatcherMaxRequests , int dispatcherMaxRequestsPerHost )
119119 throws ApiException {
120120 this .basePath = basePath ;
121121 this .realm = realm ;
@@ -134,7 +134,8 @@ public ApiClient(String basePath, Realm realm, String appKey, String clientId, S
134134 );
135135
136136 initHttpClient (Collections .<Interceptor >emptyList (), maxIdleConnections ,
137- connectKeepAliveMilliSeconds , connectTimeoutMilliSeconds , proxy );
137+ connectKeepAliveMilliSeconds , connectTimeoutMilliSeconds , proxy ,
138+ dispatcherMaxRequests , dispatcherMaxRequestsPerHost );
138139 init ();
139140
140141 // Setup authentications (key: authentication name, value: authentication).
@@ -159,7 +160,8 @@ public ApiClient(OkHttpClient client) throws ApiException {
159160 }
160161
161162 private void initHttpClient (List <Interceptor > interceptors , int maxIdleConnections ,
162- int connectKeepAliveMilliSeconds , int connectTimeoutMilliSeconds , EMProxy emProxy ) {
163+ int connectKeepAliveMilliSeconds , int connectTimeoutMilliSeconds , EMProxy emProxy ,
164+ int dispatcherMaxRequests , int dispatcherMaxRequestsPerHost ) {
163165 OkHttpClient .Builder builder = new OkHttpClient .Builder ();
164166 builder .addNetworkInterceptor (getProgressInterceptor ());
165167 for (Interceptor interceptor : interceptors ) {
@@ -192,6 +194,11 @@ private void initHttpClient(List<Interceptor> interceptors, int maxIdleConnectio
192194 }
193195 }
194196
197+ Dispatcher customDispatcher = new Dispatcher ();
198+ customDispatcher .setMaxRequests (dispatcherMaxRequests );
199+ customDispatcher .setMaxRequestsPerHost (dispatcherMaxRequestsPerHost );
200+
201+ builder .dispatcher (customDispatcher );
195202 httpClient = builder .build ();
196203 }
197204
@@ -353,6 +360,10 @@ public static class Builder {
353360
354361 private int connectTimeoutMilliSeconds = 10000 ;
355362
363+ private int dispatcherMaxRequests = 200 ;
364+
365+ private int dispatcherMaxRequestsPerHost = 50 ;
366+
356367 private EMProxy proxy ;
357368
358369 public Builder setRealm (Realm realm ) {
@@ -442,6 +453,22 @@ public Builder setConnectTimeoutMilliSeconds(int connectTimeoutMilliSeconds) {
442453 return this ;
443454 }
444455
456+ public Builder setDispatcherMaxRequests (int dispatcherMaxRequests ) {
457+ if (dispatcherMaxRequests < 0 ) {
458+ throw new IllegalArgumentException ("dispatcherMaxRequests must not be negative" );
459+ }
460+ this .dispatcherMaxRequests = dispatcherMaxRequests ;
461+ return this ;
462+ }
463+
464+ public Builder setDispatcherMaxRequestsPerHost (int dispatcherMaxRequestsPerHost ) {
465+ if (dispatcherMaxRequestsPerHost < 0 ) {
466+ throw new IllegalArgumentException ("dispatcherMaxRequestsPerHost must not be negative" );
467+ }
468+ this .dispatcherMaxRequestsPerHost = dispatcherMaxRequestsPerHost ;
469+ return this ;
470+ }
471+
445472 public Builder setProxy (EMProxy proxy ) {
446473 this .proxy = proxy ;
447474 return this ;
@@ -480,7 +507,7 @@ public ApiClient build() throws ApiException {
480507 return new ApiClient (this .basePath , this .realm , this .appKey , this .clientId ,
481508 this .clientSecret , null , null , this .maxIdleConnections ,
482509 this .connectKeepAliveMilliSeconds , this .connectTimeoutMilliSeconds ,
483- this .proxy );
510+ this .proxy , this . dispatcherMaxRequests , this . dispatcherMaxRequestsPerHost );
484511 } else if (this .realm .equals (Realm .AGORA_REALM )) {
485512 if (this .appId == null ) {
486513 throw new IllegalArgumentException ("appId not set" );
@@ -494,7 +521,7 @@ public ApiClient build() throws ApiException {
494521
495522 return new ApiClient (this .basePath , this .realm , this .appKey , null , null , this .appId ,
496523 this .appCert , this .maxIdleConnections , this .connectKeepAliveMilliSeconds ,
497- this .connectTimeoutMilliSeconds , this .proxy );
524+ this .connectTimeoutMilliSeconds , this .proxy , this . dispatcherMaxRequests , this . dispatcherMaxRequestsPerHost );
498525 } else {
499526 throw new IllegalArgumentException (
500527 String .format ("invalid realm type %s" , this .realm ));
0 commit comments