Yes, MaxIdleTime is based on mongo activity. Even if TCP keep-alive is set, no mongo activity would cause the connection to be torn down.
To prove that MaxIdleTime is working you can do the following... This is for the 2.0.x versions of the driver and uses an experimental API that will be changed in 2.1. The below sets MaxIdleTime to 5 seconds and then runs a query every 7 seconds. In the console, you'll notice that the connections are opened and closed every ~7 seconds. If your problems seem to occur with long lived connections, you might also want to play with MaxLifeTime.
using System;
using System.Threading.Tasks;
using MongoDB.Bson;
using MongoDB.Driver;
using MongoDB.Driver.Core.Events.Diagnostics;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
MainAsync().GetAwaiter().GetResult();
}
static async Task MainAsync()
{
var settings = new MongoClientSettings
{
MaxConnectionIdleTime = TimeSpan.FromSeconds(5),
ClusterConfigurator = cb =>
{
var writer = Console.Out;
cb.AddListener(new LogListener(writer, LogLevel.Info));
}
};
var client = new MongoClient(settings);
var db = client.GetDatabase("test");
var col = db.GetCollection<BsonDocument>("foo");
while (true)
{
var results = await col.Find("{}").ToListAsync();
// throw them away...
await Task.Delay(TimeSpan.FromSeconds(7));