From 29f59fe4079867365e8a4f8083a0c011b691080a Mon Sep 17 00:00:00 2001 From: UbitUmarov Date: Mon, 6 Apr 2020 16:53:28 +0100 Subject: [PATCH] reduce httppipeline depth --- .../Servers/HttpServer/OSHttpRequest.cs | 7 +++- .../OSHttpServer/HttpClientContext.cs | 41 +++++++++++-------- .../HttpServer/OSHttpServer/HttpResponse.cs | 6 +-- .../OSHttpServer/IHttpClientContext.cs | 2 +- 4 files changed, 32 insertions(+), 24 deletions(-) diff --git a/OpenSim/Framework/Servers/HttpServer/OSHttpRequest.cs b/OpenSim/Framework/Servers/HttpServer/OSHttpRequest.cs index 2ffb7a96af..a10506f6be 100644 --- a/OpenSim/Framework/Servers/HttpServer/OSHttpRequest.cs +++ b/OpenSim/Framework/Servers/HttpServer/OSHttpRequest.cs @@ -79,8 +79,11 @@ namespace OpenSim.Framework.Servers.HttpServer { RequestCookies cookies = _request.Cookies; HttpCookieCollection httpCookies = new HttpCookieCollection(); - foreach (RequestCookie cookie in cookies) - httpCookies.Add(new HttpCookie(cookie.Name, cookie.Value)); + if(cookies != null) + { + foreach (RequestCookie cookie in cookies) + httpCookies.Add(new HttpCookie(cookie.Name, cookie.Value)); + } return httpCookies; } } diff --git a/OpenSim/Framework/Servers/HttpServer/OSHttpServer/HttpClientContext.cs b/OpenSim/Framework/Servers/HttpServer/OSHttpServer/HttpClientContext.cs index 053764d550..231e352b2b 100644 --- a/OpenSim/Framework/Servers/HttpServer/OSHttpServer/HttpClientContext.cs +++ b/OpenSim/Framework/Servers/HttpServer/OSHttpServer/HttpClientContext.cs @@ -20,7 +20,7 @@ namespace OSHttpServer /// public class HttpClientContext : IHttpClientContext, IDisposable { - const int MAXREQUESTS = 20; + const int MAXPIPEREQUESTS = 5; const int MAXKEEPALIVE = 60000; static private int basecontextID; @@ -47,7 +47,7 @@ namespace OSHttpServer public int m_TimeoutKeepAlive = MAXKEEPALIVE; // 400 seconds before keepalive timeout // public int TimeoutKeepAlive = 120000; // 400 seconds before keepalive timeout - public int m_maxRequests = MAXREQUESTS; + public int m_maxPipeRequests = MAXPIPEREQUESTS; public bool FirstRequestLineReceived; public bool FullRequestReceived; @@ -68,12 +68,15 @@ namespace OSHttpServer } } - public int MAXRequests + public int MaxPipeRequests { - get { return m_maxRequests; } + get { return m_maxPipeRequests; } set { - m_maxRequests = value > MAXREQUESTS ? MAXREQUESTS : value; + if(value <= 1) + m_maxPipeRequests = 1; + else + m_maxPipeRequests = value > MAXPIPEREQUESTS ? MAXPIPEREQUESTS : value; } } @@ -354,6 +357,9 @@ namespace OSHttpServer return; } + if(m_maxPipeRequests <= 0) + return; + m_ReceiveBytesLeft += bytesRead; if (m_ReceiveBytesLeft > m_ReceiveBuffer.Length) { @@ -531,28 +537,29 @@ namespace OSHttpServer { TriggerKeepalive = false; MonitorKeepaliveMS = 0; + FullRequestReceived = true; + + if (m_maxPipeRequests == 0) + return; + + if(--m_maxPipeRequests == 0) + m_currentRequest.Connection = ConnectionType.Close; // load cookies if they exist - - RequestCookies cookies = m_currentRequest.Headers["cookie"] != null - ? new RequestCookies(m_currentRequest.Headers["cookie"]) : new RequestCookies(String.Empty); - m_currentRequest.SetCookies(cookies); + if(m_currentRequest.Headers["cookie"] != null) + m_currentRequest.SetCookies(new RequestCookies(m_currentRequest.Headers["cookie"])); m_currentRequest.Body.Seek(0, SeekOrigin.Begin); - FullRequestReceived = true; - int nreqs; lock (requestsInServiceIDs) { nreqs = requestsInServiceIDs.Count; requestsInServiceIDs.Add(m_currentRequest.ID); - if (m_maxRequests > 0) - m_maxRequests--; } // for now pipeline requests need to be serialized by opensim - RequestReceived(this, new RequestEventArgs(m_currentRequest)); + RequestReceived?.Invoke(this, new RequestEventArgs(m_currentRequest)); m_currentRequest = new HttpRequest(this); @@ -610,10 +617,8 @@ namespace OSHttpServer lock (requestsInServiceIDs) { requestsInServiceIDs.Remove(requestID); -// doclose = doclose && requestsInServiceIDs.Count == 0; if (requestsInServiceIDs.Count > 1) { - } } @@ -750,11 +755,11 @@ namespace OSHttpServer /// /// Event can be used to clean up a context, or to reuse it. /// - public event EventHandler Disconnected = delegate { }; + public event EventHandler Disconnected; /// /// A request have been received in the context. /// - public event EventHandler RequestReceived = delegate { }; + public event EventHandler RequestReceived; public HTTPNetworkContext GiveMeTheNetworkStreamIKnowWhatImDoing() { diff --git a/OpenSim/Framework/Servers/HttpServer/OSHttpServer/HttpResponse.cs b/OpenSim/Framework/Servers/HttpServer/OSHttpServer/HttpResponse.cs index 2c1a0bdf7e..6330512f98 100644 --- a/OpenSim/Framework/Servers/HttpServer/OSHttpServer/HttpResponse.cs +++ b/OpenSim/Framework/Servers/HttpServer/OSHttpServer/HttpResponse.cs @@ -245,9 +245,9 @@ namespace OSHttpServer sb.Append("Server: OSWebServer\r\n"); int keepaliveS = m_context.TimeoutKeepAlive / 1000; - if (Connection == ConnectionType.KeepAlive && keepaliveS > 0 && m_context.MAXRequests > 0) + if (Connection == ConnectionType.KeepAlive && keepaliveS > 0 && m_context.MaxPipeRequests > 0) { - sb.AppendFormat("Keep-Alive:timeout={0}, max={1}\r\n", keepaliveS, m_context.MAXRequests); + sb.AppendFormat("Keep-Alive:timeout={0}, max={1}\r\n", keepaliveS, m_context.MaxPipeRequests); sb.Append("Connection: Keep-Alive\r\n"); } else @@ -282,7 +282,7 @@ namespace OSHttpServer if (Sent) throw new InvalidOperationException("Everything have already been sent."); - if (m_context.MAXRequests == 0 || m_keepAlive == 0) + if (m_context.MaxPipeRequests == 0 || m_keepAlive == 0) { Connection = ConnectionType.Close; m_context.TimeoutKeepAlive = 0; diff --git a/OpenSim/Framework/Servers/HttpServer/OSHttpServer/IHttpClientContext.cs b/OpenSim/Framework/Servers/HttpServer/OSHttpServer/IHttpClientContext.cs index 8d4fc79c06..fe47cbc1d7 100644 --- a/OpenSim/Framework/Servers/HttpServer/OSHttpServer/IHttpClientContext.cs +++ b/OpenSim/Framework/Servers/HttpServer/OSHttpServer/IHttpClientContext.cs @@ -23,7 +23,7 @@ namespace OSHttpServer int contextID {get;} int TimeoutKeepAlive {get; set; } - int MAXRequests{get; set; } + int MaxPipeRequests{get; set; } bool CanSend(); bool IsSending();