test changes on oshttpserver responses send

This commit is contained in:
UbitUmarov
2021-09-02 04:49:22 +01:00
parent af4dacee53
commit f5e0459e08
6 changed files with 113 additions and 129 deletions

View File

@@ -54,9 +54,7 @@ namespace OSHttpServer
private static int m_ActiveSendingCount;
private static double m_lastTimeOutCheckTime = 0;
private static double m_lastSendCheckTime = 0;
const int m_maxBandWidth = 10485760; //80Mbps
const int m_maxConcurrenSend = 32;
static ContextTimeoutManager()
@@ -86,10 +84,11 @@ namespace OSHttpServer
public static void Stop()
{
m_shuttingDown = true;
m_processWaitEven.Set();
//m_internalThread.Join();
//ProcessShutDown();
if (m_processWaitEven != null)
{
m_processWaitEven.Set();
m_shuttingDown = true;
}
}
private static void ThreadRunProcess()
@@ -98,13 +97,16 @@ namespace OSHttpServer
{
m_processWaitEven.WaitOne(500);
if(m_shuttingDown)
return;
if (m_shuttingDown)
break;
double now = GetTimeStamp();
if(m_contexts.Count > 0)
{
ProcessSendQueues(now);
ProcessSendQueues();
if (m_shuttingDown)
break;
if (now - m_lastTimeOutCheckTime > 1.0)
{
@@ -115,26 +117,30 @@ namespace OSHttpServer
else
m_lastTimeOutCheckTime = now;
}
ProcessShutDown();
}
public static void ProcessShutDown()
{
try
{
SocketError disconnectError = SocketError.HostDown;
for (int i = 0; i < m_contexts.Count; i++)
if(m_processWaitEven != null)
{
if (m_contexts.TryDequeue(out HttpClientContext context))
SocketError disconnectError = SocketError.HostDown;
for (int i = 0; i < m_contexts.Count; i++)
{
try
if (m_contexts.TryDequeue(out HttpClientContext context))
{
context.Disconnect(disconnectError);
try
{
context.Disconnect(disconnectError);
}
catch { }
}
catch { }
}
m_processWaitEven.Dispose();
m_processWaitEven = null;
}
m_processWaitEven.Dispose();
m_processWaitEven = null;
}
catch
{
@@ -142,32 +148,21 @@ namespace OSHttpServer
}
}
public static void ProcessSendQueues(double now)
public static void ProcessSendQueues()
{
int inqueues = m_highPrio.Count + m_midPrio.Count + m_lowPrio.Count;
if(inqueues == 0)
return;
double dt = now - m_lastSendCheckTime;
m_lastSendCheckTime = now;
const int curbytesLimit = 128 * 1024;
int totalSending = m_ActiveSendingCount;
int curConcurrentLimit = m_maxConcurrenSend - totalSending;
int curConcurrentLimit = m_maxConcurrenSend - m_ActiveSendingCount;
if(curConcurrentLimit <= 0)
return;
if(curConcurrentLimit > inqueues)
curConcurrentLimit = inqueues;
if (dt > 0.5)
dt = 0.5;
dt /= curConcurrentLimit;
int curbytesLimit = (int)(m_maxBandWidth * dt);
if(curbytesLimit < 1024)
curbytesLimit = 1024;
HttpClientContext ctx;
int sent;
while (curConcurrentLimit > 0)
@@ -175,49 +170,39 @@ namespace OSHttpServer
sent = 0;
while (m_highPrio.TryDequeue(out ctx))
{
if(TrySend(ctx, curbytesLimit))
m_highPrio.Enqueue(ctx);
if (m_shuttingDown)
return;
--curConcurrentLimit;
if (++sent == 4)
break;
if (ctx.TrySendResponse(curbytesLimit))
{
--curConcurrentLimit;
if (++sent == 3)
break;
}
}
sent = 0;
while(m_midPrio.TryDequeue(out ctx))
{
if(TrySend(ctx, curbytesLimit))
m_midPrio.Enqueue(ctx);
if (m_shuttingDown)
return;
--curConcurrentLimit;
if (++sent >= 2)
break;
if (ctx.TrySendResponse(curbytesLimit))
{
--curConcurrentLimit;
if (++sent >= 2)
break;
}
}
if (m_lowPrio.TryDequeue(out ctx))
{
--curConcurrentLimit;
if(TrySend(ctx, curbytesLimit))
m_lowPrio.Enqueue(ctx);
if (m_shuttingDown)
return;
if (ctx.TrySendResponse(curbytesLimit))
--curConcurrentLimit;
}
if (m_shuttingDown)
return;
}
}
private static bool TrySend(HttpClientContext ctx, int bytesLimit)
{
if(!ctx.CanSend())
return false;
return ctx.TrySendResponse(bytesLimit);
}
/// <summary>
/// Causes the watcher to immediately check the connections.
/// </summary>