title: Connection Pooling with NEST ElasticSearch Library
body: <p>I'm currently using the NEST ElasticSearch C# Library for interacting with ElasticSearch. My project is an MVC 4 WebAPI project that basically builds a RESTful webservice for accessing directory assistance information.</p>
  
  <p>We've only just started working with NEST, and have been stumbling over the lack of documentation. What's there is useful, but it's got some very large holes. Currently, everything we need works, however, we're running into an issue with connections sometimes taking up to a full second. What we'd like to do is use some sort of connection pooling, similar to how you'd interact with SQL Server.</p>
  
  <p>Here is the documentation on how to connect using nest: <a href="http://mpdreamz.github.com/NEST/concepts/connecting.html" rel="nofollow">http://mpdreamz.github.com/NEST/concepts/connecting.html</a></p>
  
  <p>Here is the relevant code snippet from our project:</p>
  
  <pre><code>public class EOCategoryProvider : IProvider
  {
      public DNList ExecuteQuery(Query query)
      {
          //Configure the elastic client and it's settings
          ConnectionSettings elasticSettings = new ConnectionSettings(Config.server, Config.port).SetDefaultIndex(Config.index);
          ElasticClient client = new ElasticClient(elasticSettings);
  
          //Connect to Elastic
          ConnectionStatus connectionStatus;
          if (client.TryConnect(out connectionStatus))
          {
              // Elastic Search Code here ...
          } // end if
      } // end ExecuteQuery
  } // end EOCategoryProvider
  </code></pre>
  
  <p>From looking at the documentation, I can't see any provisions for a connection pool. I've been thinking about implementing my own (having, say 3 or 4 ElasticClient objects stored, and selecting them round-robin style), but I was wondering if anyone had a better solution. If not, does anyone have advice on the best way to implement a connection pool by hand? Any articles to point to?</p>
  
  <p>Thanks for anything you guys come up with.</p>
  
  <p><strong>Update</strong>: This seems to have been related to calling TryConnect on every request, and the particular network setup. The problem completely disappeared when using a machine on the same network as the Elastic box; My development machine (which averages 350ms to the Elastic box) seemed to fail to make http connections sometimes, which caused the long times in TryConnect.</p>