日期:2014-05-18  浏览次数:20520 次

感觉开源的Enyim.Caching.Memcached的实现是有问题的... 大家进来分析一下...
public PooledSocket Acquire()
{
if (log.IsDebugEnabled)
log.Debug("Acquiring stream from pool.");

if (!this.IsAlive)
{
if (log.IsDebugEnabled)
log.Debug("Pool is dead, returning null.");

return null;
}

// every release signals the event, so even if the pool becomes full in the meantime
// the WaitOne will succeed, and more items will be in the pool than allowed,
// so reset the event when an item is inserted
this.itemReleasedEvent.Reset();--------------这边reset有意义吗?有兄弟知道吗?

PooledSocket retval = null;

// do we have free items?
if (this.freeItems.Dequeue(out retval))
{
try
{
retval.Reset();

if (log.IsDebugEnabled)
log.Debug("Socket was reset. " + retval.InstanceId);

Interlocked.Increment(ref this.workingCount);

return retval;
}
catch (Exception e)
{
log.Error("Failed to reset an acquired socket.", e);

this.MarkAsDead();

return null;
}
}
else
{
// free item pool is empty
if (log.IsDebugEnabled)
log.Debug("Could not get a socket from the pool.");

// we are not allowed to create more, so wait for an item to be released back into the pool
if (this.workingCount >= this.maxItems)
{
if (log.IsDebugEnabled)
log.Debug("Pool is full, wait for a release.");

// wait on the event
if (!itemReleasedEvent.WaitOne(this.connectionTimeout,true))
{
if (log.IsDebugEnabled)
log.Debug("Pool is still full, timeouting.");

// everyone is working
throw new TimeoutException();
}----------------------当获取信号量的时候...代码会接着往下走,也就是说,并不是从已有的队列中去取一个socket出来..而又去createsocket一个出来...不知道是写的太烂,还是我没理解清楚?
}

if (log.IsDebugEnabled)
log.Debug("Creating a new item.");

try
{
// okay, create the new item
retval = this.CreateSocket();

Interlocked.Increment(ref this.workingCount);
}
catch (Exception e)
{
log.Error("Failed to create socket.", e);
this.MarkAsDead();

return null;
}
}

if (log.IsDebugEnabled)
log.Debug("Done.");

return retval;
}

------解决方案--------------------
帮你顶上去
------解决方案--------------------
不太清楚,顶一下了.
------解决方案--------------------
不太了解,帮你顶一下