Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
tmds committed May 17, 2017
1 parent bbf65a0 commit 553aed1
Showing 1 changed file with 15 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public bool TryRegister(SafeCloseSocket socket, Interop.Sys.SocketEvents current
//
private static readonly IntPtr MaxHandles = IntPtr.Size == 4 ? (IntPtr)int.MaxValue : (IntPtr)long.MaxValue;
#endif
private static readonly IntPtr MinHandlesAdditionalEngine = EngineCount == 1 ? MaxHandles : (IntPtr)32;
private static readonly IntPtr MinHandlesForAdditionalEngine = EngineCount == 1 ? MaxHandles : (IntPtr)32;

//
// Sentinel handle value to identify events from the "shutdown pipe," used to signal an event loop to stop
Expand Down Expand Up @@ -134,6 +134,16 @@ public bool TryRegister(SafeCloseSocket socket, Interop.Sys.SocketEvents current
//
private bool IsFull { get { return _nextHandle == MaxHandles; } }

// True if we've don't have sufficient active sockets to allow allocating a new engine.
private bool HasLowNumberOfSockets
{
get
{
return IntPtr.Size == 4 ? _outstandingHandles.ToInt32() < MinHandlesForAdditionalEngine.ToInt32() :
_outstandingHandles.ToInt64() < MinHandlesForAdditionalEngine.ToInt64();
}
}

//
// Allocates a new {SocketAsyncEngine, handle} pair.
//
Expand All @@ -144,12 +154,11 @@ private static void AllocateToken(SocketAsyncContext context, out SocketAsyncEng
engine = s_currentEngines[s_allocateFromEngine];
if (engine == null)
{
// Minimize the number of engines on applications which have a low number of concurrent sockets.
// We minimize the number of engines on applications that have a low number of concurrent sockets.
for (int i = 0; i < s_allocateFromEngine; i++)
{
var previousEngine = s_currentEngines[i];
if (previousEngine == null ||
previousEngine._outstandingHandles.ToInt64() < MinHandlesAdditionalEngine.ToInt64())
if (previousEngine == null || previousEngine.HasLowNumberOfSockets)
{
s_allocateFromEngine = i;
engine = previousEngine;
Expand All @@ -170,8 +179,8 @@ private static void AllocateToken(SocketAsyncContext context, out SocketAsyncEng
s_currentEngines[s_allocateFromEngine] = null;
}

// Round-robin to the next engine.
if (engine._outstandingHandles.ToInt64() >= MinHandlesAdditionalEngine.ToInt64())
// Round-robin to the next engine once we have sufficient sockets on this one.
if (!engine.HasLowNumberOfSockets)
{
s_allocateFromEngine = (s_allocateFromEngine + 1) % EngineCount;
}
Expand Down

0 comments on commit 553aed1

Please sign in to comment.