# Port Exhaustion

**URL:** <https://discuss.elastic.co/t/port-exhaustion/85186>\
**Category:** Elasticsearch\
**Created:** [May 10, 2017, 4:13am UTC](https://discuss.elastic.co/t/port-exhaustion/85186 "2017-05-10T04:13:11Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![amccool](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/amccool/32/29247_2.png) [@amccool](https://discuss.elastic.co/u/amccool)\
**Post date:** [May 10, 2017, 4:13am UTC](https://discuss.elastic.co/t/port-exhaustion/85186/1 "2017-05-10T04:13:11Z")

</div>

using NEST or ElasticsearchClient (1.9,2) outputting trace diagnostics from .net services I'm running to into port exhaustion problems.  
netstat -ano | findstr 9200 | measure-object -line  
yields well over 5000 established ports to the server. This number can reach over 10,000

I'm using both IndexAsync and BulkAsync - I dont care what the response is, only that the request was successfully submitted.

how can I better control this?

---

<div class="post-metadata">

**Author:** ![forloop](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/forloop/32/9021_2.png) [@forloop](https://discuss.elastic.co/u/forloop)\
**Post date:** [May 10, 2017, 7:21am UTC](https://discuss.elastic.co/t/port-exhaustion/85186/2 "2017-05-10T07:21:14Z")

</div>

Hey @amccool What version of Elasticsearch are you running against?

You can use [`ConnectionLimit`](https://www.elastic.co/guide/en/elasticsearch/client/net-api/current/configuration-options.html#_options_on_connectionconfiguration) in NEST 2.x and 5.x to control the number of open TCP connections to an endpoint, but this is hardcoded in NEST 1.x to be 10,000:

> <https://github.com/elastic/elasticsearch-net/blob/1.x/src/Elasticsearch.Net/Connection/HttpConnection.cs#L150>

You can however provide your own implementation of `IConnection` that derives from `HttpConnection` and override the `AlterServicePoint` method to set `ConnectionLimit` to a more reasonable number e.g. 80 as per 2.x and 5.x.

Then pass it to the constructor for `ElasticClient` to use it

```
var uri = new Uri("http://localhost:9200");
var settings = new ConnectionSettings(uri, defaultIndex: "default-index");
var client = new ElasticClient(settings, new CustomHttpConnectionWithLowerConnectionLimit(settings));
```

---

<div class="post-metadata">

**Author:** ![amccool](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/amccool/32/29247_2.png) [@amccool](https://discuss.elastic.co/u/amccool)\
**Post date:** [May 11, 2017, 6:22pm UTC](https://discuss.elastic.co/t/port-exhaustion/85186/3 "2017-05-11T18:22:41Z")

</div>

The clients are executing against a single node of ES running 5.3.1.  
Thanks for the IConnection pointer.

---

<div class="post-metadata">

**Author:** ![forloop](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/forloop/32/9021_2.png) [@forloop](https://discuss.elastic.co/u/forloop)\
**Post date:** [May 12, 2017, 1:31pm UTC](https://discuss.elastic.co/t/port-exhaustion/85186/4 "2017-05-12T13:31:36Z")

</div>

No worries, let me know if it helps with port exhaustion 👍

---

<div class="post-metadata">

**Author:** ![amccool](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/amccool/32/29247_2.png) [@amccool](https://discuss.elastic.co/u/amccool)\
**Post date:** [May 12, 2017, 5:31pm UTC](https://discuss.elastic.co/t/port-exhaustion/85186/5 "2017-05-12T17:31:54Z")

</div>

fighting with my docker images. Cant wait to try the IConnection.

Quick question - by limiting the available ports, should I see port connection re-use? And does that backup into some sort of queue?

---

<div class="post-metadata">

**Author:** ![forloop](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/forloop/32/9021_2.png) [@forloop](https://discuss.elastic.co/u/forloop)\
**Post date:** [May 14, 2017, 11:14pm UTC](https://discuss.elastic.co/t/port-exhaustion/85186/6 "2017-05-14T23:14:54Z")

</div>

TCP connection pooling is managed by the .NET framework, so connections _should_ be being reused already. In full .NET framework, this is controlled by [ServicePointManager](https://blogs.msdn.microsoft.com/adarshk/2005/01/02/understanding-system-net-connection-management-and-servicepointmanager/), and the framework will queue up a request on a connection until one becomes available.

---

<div class="post-metadata">

**Author:** ![amccool](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/amccool/32/29247_2.png) [@amccool](https://discuss.elastic.co/u/amccool)\
**Post date:** [May 15, 2017, 4:07pm UTC](https://discuss.elastic.co/t/port-exhaustion/85186/7 "2017-05-15T16:07:55Z")

</div>

> [@forloop](#):
>
> new CustomHttpConnectionWithLowerConnectionLimit(settings));

Setting the limit to 1000 - I am getting 429 (too many requests) back from the server (docker). I "think" this has more to do with the server than my code. Still looking.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [June 12, 2017, 4:09pm UTC](https://discuss.elastic.co/t/port-exhaustion/85186/8 "2017-06-12T16:09:56Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
