Reputation: 1564
I have a C# application that performs multi-threaded inserts via the C# ThreadPool into MongoDB. However, I have been getting a TimeoutException: Timeout waiting for a MongoConnection
. I'm using the MongoServer.RequestStart method, which should be releasing the connection back into the MongoClient connection pool.
Also, the threadpool has a minimum of 4 threads and maximum of 8, while the Mongo connection pool has a default of 100 connections, so I shouldn't be running out of connections.
So why am I getting this error?
Here's the method which is passed into the threadpool. _client
is a MongoClient instance variable.
public void BatchInsert(string collectionName, BinaryPacketDocument[] documents, int batchSize) {
MongoServer server = _client.GetServer();
MongoDatabase database = server.GetDatabase(_databaseName);
using (server.RequestStart(database)) {
MongoCollection collection = database.GetCollection(collectionName);
collection.InsertBatch(documents);
StatisticsManager.GetCounter("logs").Add(batchSize);
}
}
And here's how I pass it into the threadpool.
private void SendWorkToThreadPool(string collectionName, BinaryPacketDocument[] documents, int batchSize) {
if (documents.Length != 0) {
ThreadPool.QueueUserWorkItem(state => _inserter.BatchInsert(collectionName, documents, batchSize));
}
}
Upvotes: 3
Views: 1358
Reputation: 1564
I realized that my thread pool was not actually being limited to 8 threads. If you pass in 0 as one of the arguments to ThreadPool.SetMax/Min threads, it will fail to set the max (but not explicitly).
Upvotes: 1