Kong / unirest-java

Unirest in Java: Simplified, lightweight HTTP client library.
http://kong.github.io/unirest-java/
MIT License
2.58k stars 591 forks source link

Concurrency in Unirest #452

Closed router19 closed 1 year ago

router19 commented 1 year ago

I have a webapp, which inturns makes calls to few upstream services. The webapp can be called in parallel by multiple client applications. As there will be multiple parallel calls recieved by the webapp, I planned to use separate UnirestInstance for each incoming requests, and use these UnirestInstance to make calls to upstream services , so that the incoming calls should not get blocked.

But with creating multiple unirest instance, I found that the connection establishment with the upstream services are very time consuming. Also, provided that the domain name of the upstream services is same, I want to connectionpool with a longer TTL.

But as I was creating multiple UnirestInstance, I do not see previous connections being reused.

Could you please guide me on how can I reuse the previous established connections/ connection pool in Unirest, such that I can handle multiple parallel calls received by my webapp

ryber commented 1 year ago

hi @router19, for what you want, don't use separate instances, use the same instance. One instance has a connection pool (via PoolingHttpClientConnectionManager) and is (mostly) non-blocking and can be used by many threads at once to access different requests over the same connection.