private void ProcessRequest(HttpListenerContext context) { if (context == null) return; Task.Factory.StartNew(() => { var ctx = new HttpListenerContextAdpater(context, SystemConfiguration, bufferPool); if (concurrentRequestSemaphore.Wait(TimeSpan.FromSeconds(5)) == false) { try { HandleTooBusyError(ctx); } catch (Exception e) { logger.WarnException("Could not send a too busy error to the client", e); } return; } try { Interlocked.Increment(ref physicalRequestsCount); #if DEBUG recentRequests.Enqueue(ctx.Request.RawUrl); while (recentRequests.Count > 50) { string _; recentRequests.TryDequeue(out _); } #endif if (ChangesQuery.IsMatch(ctx.GetRequestUrl())) HandleChangesRequest(ctx, () => { }); else HandleActualRequest(ctx); } finally { concurrentRequestSemaphore.Release(); } }); }
private void GetContext(IAsyncResult ar) { HttpListenerContextAdpater ctx; try { HttpListenerContext httpListenerContext = listener.EndGetContext(ar); ctx = new HttpListenerContextAdpater(httpListenerContext, SystemConfiguration, bufferPool); //setup waiting for the next request listener.BeginGetContext(GetContext, null); } catch (Exception) { // can't get current request / end new one, probably // listener shutdown return; } if (concurrentRequestSemaphore.Wait(TimeSpan.FromSeconds(5)) == false) { HandleTooBusyError(ctx); return; } try { Interlocked.Increment(ref physicalRequestsCount); if (ChangesQuery.IsMatch(ctx.GetRequestUrl())) HandleChangesRequest(ctx, () => { }); else HandleActualRequest(ctx); } finally { concurrentRequestSemaphore.Release(); } }