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>

View File

@@ -528,7 +528,7 @@ namespace OSHttpServer
LastActivityTimeMS = ContextTimeoutManager.EnvironmentTickCount();
m_currentResponse?.SendNextAsync(bytesLimit);
return false;
return true;
}
public void ContinueSendResponse(bool notThrottled)

View File

@@ -352,11 +352,6 @@ namespace OSHttpServer
{
if(!await m_context.SendAsync(m_headerBytes, 0, m_headerBytes.Length).ConfigureAwait(false))
{
if (m_context.CanSend())
{
m_context.ContinueSendResponse(true);
return;
}
if (m_body != null)
m_body.Dispose();
RawBuffer = null;
@@ -367,50 +362,49 @@ namespace OSHttpServer
m_headerBytes = null;
if(bytesLimit <= 0)
{
m_context.ContinueSendResponse(true);
m_context.ContinueSendResponse(false);
return;
}
}
if (RawBuffer != null)
bool sendRes;
if (RawBuffer != null && RawBufferLen > 0)
{
if (RawBufferLen > 0)
if (BandWitdhEvent != null)
{
if(BandWitdhEvent!=null)
bytesLimit = CheckBandwidth(RawBufferLen, bytesLimit);
bool sendRes;
if(RawBufferLen > bytesLimit)
bytesLimit = CheckBandwidth(RawBufferLen, bytesLimit);
if (bytesLimit <= 0)
{
sendRes = (await m_context.SendAsync(RawBuffer, RawBufferStart, bytesLimit).ConfigureAwait(false));
if (sendRes)
{
RawBufferLen -= bytesLimit;
RawBufferStart += bytesLimit;
}
}
else
{
sendRes = await m_context.SendAsync(RawBuffer, RawBufferStart, RawBufferLen).ConfigureAwait(false);
if(sendRes)
RawBufferLen = 0;
}
if (!sendRes)
{
if (m_context.CanSend())
{
m_context.ContinueSendResponse(true);
return;
}
RawBuffer = null;
if(m_body != null)
Body.Dispose();
Sent = true;
m_context.ContinueSendResponse(false);
return;
}
}
if(RawBufferLen > bytesLimit)
{
sendRes = (await m_context.SendAsync(RawBuffer, RawBufferStart, bytesLimit).ConfigureAwait(false));
if (sendRes)
{
RawBufferLen -= bytesLimit;
RawBufferStart += bytesLimit;
}
}
else
{
sendRes = await m_context.SendAsync(RawBuffer, RawBufferStart, RawBufferLen).ConfigureAwait(false);
if(sendRes)
RawBufferLen = 0;
}
if (!sendRes)
{
RawBuffer = null;
if(m_body != null)
Body.Dispose();
Sent = true;
return;
}
if (RawBufferLen <= 0)
RawBuffer = null;
else
@@ -431,7 +425,16 @@ namespace OSHttpServer
if(RawBufferLen > 0)
{
bool sendRes;
if (BandWitdhEvent != null)
{
bytesLimit = CheckBandwidth(RawBufferLen, bytesLimit);
if (bytesLimit <= 0)
{
m_context.ContinueSendResponse(false);
return;
}
}
if (RawBufferLen > bytesLimit)
{
sendRes = await m_context.SendAsync(RawBuffer, RawBufferStart, bytesLimit).ConfigureAwait(false);
@@ -450,11 +453,6 @@ namespace OSHttpServer
if (!sendRes)
{
if (m_context.CanSend())
{
m_context.ContinueSendResponse(true);
return;
}
RawBuffer = null;
Sent = true;
return;
@@ -475,14 +473,17 @@ namespace OSHttpServer
private int CheckBandwidth(int request, int bytesLimit)
{
if(request > bytesLimit)
if (request > bytesLimit)
request = bytesLimit;
var args = new BandWitdhEventArgs(request);
BandWitdhEvent?.Invoke(this, args);
if(args.Result > 8196)
try
{
BandWitdhEvent?.Invoke(this, args);
return args.Result;
}
catch { }
return 8196;
return request;
}
public void Clear()