reduce default http keepalive to just 30s because to viewers connections flood.

master
UbitUmarov 2020-07-24 19:19:16 +01:00
parent d9a680703f
commit e08ca7402c
2 changed files with 16 additions and 24 deletions

View File

@ -276,22 +276,24 @@ namespace OSHttpServer
if (context.TriggerKeepalive) if (context.TriggerKeepalive)
{ {
context.TriggerKeepalive = false; context.TriggerKeepalive = false;
context.MonitorKeepaliveStartMS = nowMS + 1; context.MonitorKeepaliveStartMS = nowMS + 500;
return false; return false;
} }
if (context.MonitorKeepaliveStartMS != 0) if (context.MonitorKeepaliveStartMS != 0)
{ {
if (context.IsClosing)
{
disconnectError = SocketError.Success;
return true;
}
if (EnvironmentTickCountAdd(context.TimeoutKeepAlive, context.MonitorKeepaliveStartMS) < nowMS) if (EnvironmentTickCountAdd(context.TimeoutKeepAlive, context.MonitorKeepaliveStartMS) < nowMS)
{ {
if(context.IsClosing) disconnectError = SocketError.TimedOut;
disconnectError = SocketError.Success;
else
disconnectError = SocketError.TimedOut;
context.MonitorKeepaliveStartMS = 0; context.MonitorKeepaliveStartMS = 0;
return true; return true;
} }
return false;
} }
if (EnvironmentTickCountAdd(context.TimeoutMaxIdle, context.LastActivityTimeMS) < nowMS) if (EnvironmentTickCountAdd(context.TimeoutMaxIdle, context.LastActivityTimeMS) < nowMS)

View File

@ -46,7 +46,7 @@ namespace OSHttpServer
public int TimeoutRequestReceived = 30000; // 30 seconds public int TimeoutRequestReceived = 30000; // 30 seconds
public int TimeoutMaxIdle = 180000; // 3 minutes public int TimeoutMaxIdle = 180000; // 3 minutes
public int m_TimeoutKeepAlive = 60000; public int m_TimeoutKeepAlive = 30000;
public bool FirstRequestLineReceived; public bool FirstRequestLineReceived;
public bool FullRequestReceived; public bool FullRequestReceived;
@ -444,7 +444,7 @@ namespace OSHttpServer
if (m_maxRequests == 0) if (m_maxRequests == 0)
return; return;
if(--m_maxRequests == 0) if (--m_maxRequests == 0)
m_currentRequest.Connection = ConnectionType.Close; m_currentRequest.Connection = ConnectionType.Close;
if(m_currentRequest.Uri == null) if(m_currentRequest.Uri == null)
@ -527,8 +527,7 @@ namespace OSHttpServer
if(contextID < 0) if(contextID < 0)
return; return;
bool doclose = ctype == ConnectionType.Close; if (ctype == ConnectionType.Close)
if (doclose)
{ {
m_isClosing = true; m_isClosing = true;
m_requests.Clear(); m_requests.Clear();
@ -538,30 +537,21 @@ namespace OSHttpServer
else else
{ {
LastActivityTimeMS = ContextTimeoutManager.EnvironmentTickCount(); LastActivityTimeMS = ContextTimeoutManager.EnvironmentTickCount();
if (Stream != null && Stream.CanWrite)
{
ContextTimeoutManager.ContextEnterActiveSend();
try
{
await Stream.FlushAsync().ConfigureAwait(false);
}
catch
{
};
ContextTimeoutManager.ContextLeaveActiveSend();
}
if (Stream == null || !Stream.CanWrite) if (Stream == null || !Stream.CanWrite)
return; return;
TriggerKeepalive = true;
HttpRequest nextRequest = null; HttpRequest nextRequest = null;
lock (m_requestsLock) lock (m_requestsLock)
{ {
if (m_requests != null && m_requests.Count > 0) if (m_requests != null && m_requests.Count > 0)
nextRequest = m_requests.Dequeue(); nextRequest = m_requests.Dequeue();
if (nextRequest != null && RequestReceived != null) if (nextRequest != null && RequestReceived != null)
{
m_waitingResponse = true; m_waitingResponse = true;
TriggerKeepalive = false;
}
else
TriggerKeepalive = true;
} }
if (nextRequest != null) if (nextRequest != null)
RequestReceived?.Invoke(this, new RequestEventArgs(nextRequest)); RequestReceived?.Invoke(this, new RequestEventArgs(nextRequest));