Я пытаюсь реализовать пул TCP-соединений и вернуть соединение обратно в пул, используя IDisposable.Мне интересно, если моя реализация верна, похоже, она работает, но я думаю, потому что базовый класс также реализует IDisposable и finalize, мой код может быть дырявым.
public class BaseClass : IDisposable
{
internal bool IsDisposed { get; set; }
private object someResource;
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
~BaseClass()
{
Dispose(false);
}
protected virtual void Dispose(bool disposing)
{
if (someResource != null)
{
// some clearn up
return;
}
if (disposing)
{
//dispose un managed resources
}
}
}
public class ChildClass : BaseClass
{
// adds some functionality
}
public class MyClass : ChildClass, IDisposable
{
MyPoolManager manager = null;
public MyClass(MyPoolManager manager)
{
this.manager = manager;
}
public new void Dispose()
{
manager.ReturnPooledConnection(this);
}
}
public class MyPoolManager
{
private static MyPoolManager instance = new MyPoolManager();
private static object objLock = new object();
private static Queue<MyClass> que = null;
private string name;
static MyPoolManager()
{
que = new Queue<MyClass>();
// enqueue some instances of MyClass here
MyClass client = new MyClass(instance);
que.Enqueue(client);
}
private MyPoolManager() { }
public MyPoolManager(string name)
{
this.name = name;
}
public MyClass GetPooledConnection()
{
lock (objLock)
{
while (que.Count == 0)
{
if (!Monitor.Wait(objLock, 1000))
throw new TimeoutException("Connection timeout");
}
return que.Dequeue();
}
}
public void ReturnPooledConnection(MyClass client)
{
lock (objLock)
{
que.Enqueue(client);
Monitor.Pulse(objLock);
}
}
}
И вы бы использовалив вашей программе это так:
MyPoolManager pool = new MyPoolManager();
using (var conn = pool.GetPooledConnection())
{
// use the conn here
}
// when you reach here the conn should have returned back to the pool