public LoggingThreadPool(IKestrelTrace log)
        {
            _log = log;

            // Curry and capture log in closures once
            _runAction = (o) =>
            {
                try
                {
                    ((Action)o)();
                }
                catch (Exception e)
                {
                    _log.ApplicationError(e);
                }
            };

            _completeTcs = (o) =>
            {
                try
                {
                    ((TaskCompletionSource<object>)o).TrySetResult(null);
                }
                catch (Exception e)
                {
                    _log.ApplicationError(e);
                }
            };
        }
 public SocketOutput(KestrelThread thread, UvStreamHandle socket, long connectionId, IKestrelTrace log)
 {
     _thread = thread;
     _socket = socket;
     _connectionId = connectionId;
     _log = log;
     _callbacksPending = new Queue<CallbackContext>();
 }
 public KestrelThread(KestrelEngine engine)
 {
     _engine = engine;
     _appLifetime = engine.AppLifetime;
     _log = engine.Log;
     _threadPool = engine.ThreadPool;
     _loop = new UvLoopHandle(_log);
     _post = new UvAsyncHandle(_log);
     _thread = new Thread(ThreadStart);
     _thread.Name = "KestrelThread - libuv";
     QueueCloseHandle = PostCloseHandle;
 }
        public FilteredStreamAdapter(
            Stream filteredStream,
            MemoryPool2 memory,
            IKestrelTrace logger)
        {
            SocketInput = new SocketInput(memory);
            SocketOutput = new StreamSocketOutput(filteredStream);

            _log = logger;
            _filteredStream = filteredStream;
            _socketInputStream = new SocketInputStream(SocketInput);

            _filteredStream.CopyToAsync(_socketInputStream).ContinueWith((task, state) =>
            {
                ((FilteredStreamAdapter)state).OnStreamClose(task);
            }, this);
        }
Beispiel #5
0
        public Http3OutputProducer(
            Http3FrameWriter frameWriter,
            MemoryPool <byte> pool,
            Http3Stream stream,
            IKestrelTrace log)
        {
            _frameWriter = frameWriter;
            _memoryPool  = pool;
            _stream      = stream;
            _log         = log;

            var pipe = CreateDataPipe(pool);

            _pipeWriter = pipe.Writer;
            _pipeReader = pipe.Reader;

            _flusher = new TimingPipeFlusher(_pipeWriter, timeoutControl: null, log);
            _dataWriteProcessingTask = ProcessDataWrites();
        }
 public SocketOutput(
     KestrelThread thread,
     UvStreamHandle socket,
     Connection connection,
     string connectionId,
     IKestrelTrace log,
     IThreadPool threadPool)
 {
     _thread               = thread;
     _socket               = socket;
     _connection           = connection;
     _connectionId         = connectionId;
     _log                  = log;
     _threadPool           = threadPool;
     _tasksPending         = new Queue <WaitingTask>(_initialTaskQueues);
     _writeContextPool     = new Queue <WriteContext>(_maxPooledWriteContexts);
     _writeReqPool         = thread.WriteReqPool;
     _maxBytesPreCompleted = connection.ServerOptions.Limits.MaxResponseBufferSize;
 }
Beispiel #7
0
        public Http2OutputProducer(Http2Stream stream, Http2StreamContext context, StreamOutputFlowControl flowControl)
        {
            _stream      = stream;
            _frameWriter = context.FrameWriter;
            _flowControl = flowControl;
            _memoryPool  = context.MemoryPool;
            _log         = context.ServiceContext.Log;

            _pipe = CreateDataPipe(_memoryPool);

            _pipeWriter = new ConcurrentPipeWriter(_pipe.Writer, _memoryPool, _dataWriterLock);
            _pipeReader = _pipe.Reader;

            // No need to pass in timeoutControl here, since no minDataRates are passed to the TimingPipeFlusher.
            // The minimum output data rate is enforced at the connection level by Http2FrameWriter.
            _flusher = new TimingPipeFlusher(_pipeWriter, timeoutControl: null, _log);

            _dataWriteProcessingTask = ProcessDataWrites();
        }
        public Http2OutputProducer(
            int streamId,
            Http2FrameWriter frameWriter,
            StreamOutputFlowControl flowControl,
            ITimeoutControl timeoutControl,
            MemoryPool <byte> pool,
            Http2Stream stream,
            IKestrelTrace log)
        {
            _streamId    = streamId;
            _frameWriter = frameWriter;
            _flowControl = flowControl;
            _stream      = stream;
            _log         = log;

            _dataPipe = CreateDataPipe(pool);
            _flusher  = new TimingPipeFlusher(_dataPipe.Writer, timeoutControl, log);
            _dataWriteProcessingTask = ProcessDataWrites();
        }
Beispiel #9
0
 public Http2FrameWriter(
     PipeWriter outputPipeWriter,
     ConnectionContext connectionContext,
     Http2Connection http2Connection,
     OutputFlowControl connectionOutputFlowControl,
     ITimeoutControl timeoutControl,
     string connectionId,
     IKestrelTrace log)
 {
     _outputWriter                = outputPipeWriter;
     _connectionContext           = connectionContext;
     _http2Connection             = http2Connection;
     _connectionOutputFlowControl = connectionOutputFlowControl;
     _connectionId                = connectionId;
     _log                  = log;
     _flusher              = new TimingPipeFlusher(_outputWriter, timeoutControl);
     _outgoingFrame        = new Http2Frame();
     _headerEncodingBuffer = new byte[_maxFrameSize];
 }
Beispiel #10
0
        public LoggingThreadPool(IKestrelTrace log)
        {
            _log = log;

            // Curry and capture log in closures once
            _runAction = (o) =>
            {
                try
                {
                    ((Action)o)();
                }
                catch (Exception e)
                {
                    _log.LogError(0, e, "LoggingThreadPool.Run");
                }
            };

            _completeTcs = (o) =>
            {
                try
                {
                    ((TaskCompletionSource <object>)o).TrySetResult(null);
                }
                catch (Exception e)
                {
                    _log.LogError(0, e, "LoggingThreadPool.Complete");
                }
            };

            _cancelTcs = (o) =>
            {
                try
                {
                    ((TaskCompletionSource <object>)o).TrySetCanceled();
                }
                catch (Exception e)
                {
                    _log.LogError(0, e, "LoggingThreadPool.Cancel");
                }
            };
        }
        public Http1OutputProducer(
            PipeWriter pipeWriter,
            string connectionId,
            ConnectionContext connectionContext,
            MemoryPool <byte> memoryPool,
            IKestrelTrace log,
            ITimeoutControl timeoutControl,
            IHttpMinResponseDataRateFeature minResponseDataRateFeature,
            IHttpOutputAborter outputAborter)
        {
            // Allow appending more data to the PipeWriter when a flush is pending.
            _pipeWriter        = new ConcurrentPipeWriter(pipeWriter, memoryPool, _contextLock);
            _connectionId      = connectionId;
            _connectionContext = connectionContext;
            _memoryPool        = memoryPool;
            _log = log;
            _minResponseDataRateFeature = minResponseDataRateFeature;
            _outputAborter = outputAborter;

            _flusher = new TimingPipeFlusher(_pipeWriter, timeoutControl, log);
        }
Beispiel #12
0
 public Proto2FrameWriter(
     PipeWriter outputPipeWriter,
     ConnectionContext connectionContext,
     Proto2Connection http2Connection,
     OutputFlowControl connectionOutputFlowControl,
     ITimeoutControl timeoutControl,
     MinDataRate minResponseDataRate,
     string connectionId,
     IKestrelTrace log)
 {
     _outputWriter                = outputPipeWriter;
     _connectionContext           = connectionContext;
     _http2Connection             = http2Connection;
     _connectionOutputFlowControl = connectionOutputFlowControl;
     _connectionId                = connectionId;
     _log                  = log;
     _timeoutControl       = timeoutControl;
     _minResponseDataRate  = minResponseDataRate;
     _flusher              = new TimingPipeFlusher(_outputWriter, timeoutControl, log);
     _outgoingFrame        = new Proto2Frame();
     _headerEncodingBuffer = new byte[_maxFrameSize];
 }
        public FilteredStreamAdapter(
            Stream filteredStream,
            MemoryPool2 memory,
            IKestrelTrace logger,
            IThreadPool threadPool)
        {
            SocketInput = new SocketInput(memory, threadPool);
            SocketOutput = new StreamSocketOutput(filteredStream, memory);

            _log = logger;
            _filteredStream = filteredStream;
            _socketInputStream = new SocketInputStream(SocketInput);

            var block = memory.Lease();
            // Use pooled block for copy
            _filteredStream.CopyToAsync(_socketInputStream, block).ContinueWith((task, state) =>
            {
                var returnedBlock = task.Result;
                returnedBlock.Pool.Return(returnedBlock);

                ((FilteredStreamAdapter)state).OnStreamClose(task);
            }, this);
        }
        public SocketOutput(
            KestrelThread thread,
            UvStreamHandle socket,
            MemoryPool memory,
            Connection connection,
            string connectionId,
            IKestrelTrace log,
            IThreadPool threadPool,
            Queue <UvWriteReq> writeReqPool)
        {
            _thread           = thread;
            _socket           = socket;
            _connection       = connection;
            _connectionId     = connectionId;
            _log              = log;
            _threadPool       = threadPool;
            _tasksPending     = new Queue <WaitingTask>(_initialTaskQueues);
            _writeContextPool = new Queue <WriteContext>(_maxPooledWriteContexts);
            _writeReqPool     = writeReqPool;

            _head = memory.Lease();
            _tail = _head;
        }
Beispiel #15
0
        //private int _unflushedBytes;

        public Http3FrameWriter(PipeWriter output, ConnectionContext connectionContext, ITimeoutControl timeoutControl, MinDataRate?minResponseDataRate, string connectionId, MemoryPool <byte> memoryPool, IKestrelTrace log, IStreamIdFeature streamIdFeature, Http3PeerSettings clientPeerSettings, IHttp3Stream http3Stream)
        {
            _outputWriter        = output;
            _connectionContext   = connectionContext;
            _timeoutControl      = timeoutControl;
            _minResponseDataRate = minResponseDataRate;
            _connectionId        = connectionId;
            _memoryPool          = memoryPool;
            _log                  = log;
            _streamIdFeature      = streamIdFeature;
            _http3Stream          = http3Stream;
            _outgoingFrame        = new Http3RawFrame();
            _flusher              = new TimingPipeFlusher(_outputWriter, timeoutControl, log);
            _headerEncodingBuffer = new byte[_maxFrameSize];

            // Note that max total header size value doesn't react to settings change during a stream.
            // Unlikely to be a problem in practice:
            // - Settings rarely change after the start of a connection.
            // - Response header size limits are a best-effort requirement in the spec.
            _maxTotalHeaderSize = clientPeerSettings.MaxRequestHeaderFieldSectionSize > int.MaxValue
                ? int.MaxValue
                : (int)clientPeerSettings.MaxRequestHeaderFieldSectionSize;
        }
Beispiel #16
0
        public KestrelThread(KestrelEngine engine)
        {
            _engine          = engine;
            _appLifetime     = engine.AppLifetime;
            _log             = engine.Log;
            _threadPool      = engine.ThreadPool;
            _shutdownTimeout = engine.ServerOptions.ShutdownTimeout;
            _loop            = new UvLoopHandle(_log);
            _post            = new UvAsyncHandle(_log);
            _thread          = new Thread(ThreadStart);
            _thread.Name     = "KestrelThread - libuv";
            _heartbeatTimer  = new UvTimerHandle(_log);
#if !DEBUG
            // Mark the thread as being as unimportant to keeping the process alive.
            // Don't do this for debug builds, so we know if the thread isn't terminating.
            _thread.IsBackground = true;
#endif
            QueueCloseHandle      = PostCloseHandle;
            QueueCloseAsyncHandle = EnqueueCloseHandle;
            Memory            = new MemoryPool();
            WriteReqPool      = new WriteReqPool(this, _log);
            ConnectionManager = new ConnectionManager(this, _threadPool);
        }
        private void TickBodyWithMinimumDataRate(IKestrelTrace logger, int bytesPerSecond)
        {
            var gracePeriod = TimeSpan.FromSeconds(5);

            _httpConnectionContext.ServiceContext.ServerOptions.Limits.MinRequestBodyDataRate =
                new MinDataRate(bytesPerSecond: bytesPerSecond, gracePeriod: gracePeriod);

            _httpConnectionContext.ServiceContext.Log = logger;

            _httpConnection.Initialize(_httpConnectionContext.Transport);
            _httpConnection.Http1Connection.Reset();

            // Initialize timestamp
            var now = DateTimeOffset.UtcNow;

            _httpConnection.Tick(now);

            _httpConnection.StartTimingReads();

            // Tick after grace period w/ low data rate
            now += gracePeriod + TimeSpan.FromSeconds(1);
            _httpConnection.BytesRead(1);
            _httpConnection.Tick(now);
        }
        public SocketOutput(
            KestrelThread thread,
            UvStreamHandle socket,
            MemoryPool2 memory,
            Connection connection,
            long connectionId,
            IKestrelTrace log,
            IThreadPool threadPool,
            Queue<UvWriteReq> writeReqPool)
        {
            _thread = thread;
            _socket = socket;
            _connection = connection;
            _connectionId = connectionId;
            _log = log;
            _threadPool = threadPool;
            _tasksPending = new Queue<TaskCompletionSource<object>>(_initialTaskQueues);
            _tasksCompleted = new Queue<TaskCompletionSource<object>>(_initialTaskQueues);
            _writeContextPool = new Queue<WriteContext>(_maxPooledWriteContexts);
            _writeReqPool = writeReqPool;

            _head = memory.Lease();
            _tail = _head;
        }
        public FilteredStreamAdapter(
            Stream filteredStream,
            MemoryPool2 memory,
            IKestrelTrace logger,
            IThreadPool threadPool)
        {
            SocketInput  = new SocketInput(memory, threadPool);
            SocketOutput = new StreamSocketOutput(filteredStream, memory);

            _log               = logger;
            _filteredStream    = filteredStream;
            _socketInputStream = new SocketInputStream(SocketInput);

            var block = memory.Lease();

            // Use pooled block for copy
            _filteredStream.CopyToAsync(_socketInputStream, block).ContinueWith((task, state) =>
            {
                var returnedBlock = task.Result;
                returnedBlock.Pool.Return(returnedBlock);

                ((FilteredStreamAdapter)state).OnStreamClose(task);
            }, this);
        }
Beispiel #20
0
        private void TickBodyWithMinimumDataRate(IKestrelTrace logger, int bytesPerSecond)
        {
            var gracePeriod = TimeSpan.FromSeconds(5);

            _frameConnectionContext.ServiceContext.ServerOptions.Limits.MinRequestBodyDataRate =
                new MinDataRate(bytesPerSecond: bytesPerSecond, gracePeriod: gracePeriod);

            _frameConnectionContext.ServiceContext.Log = logger;

            _frameConnection.CreateFrame(new DummyApplication(), _frameConnectionContext.Input.Reader, _frameConnectionContext.Output);
            _frameConnection.Frame.Reset();

            // Initialize timestamp
            var now = DateTimeOffset.UtcNow;

            _frameConnection.Tick(now);

            _frameConnection.StartTimingReads();

            // Tick after grace period w/ low data rate
            now += gracePeriod + TimeSpan.FromSeconds(1);
            _frameConnection.BytesRead(1);
            _frameConnection.Tick(now);
        }
Beispiel #21
0
        public static ServiceContext CreateServiceContext(
            KestrelServerOptions serverOptions,
            IHttpParser <Http1ParsingHandler> httpParser = null,
            IKestrelTrace log        = null,
            PipeScheduler scheduler  = null,
            ISystemClock systemClock = null,
            DateHeaderValueManager dateHeaderValueManager = null,
            ConnectionManager connectionManager           = null,
            Heartbeat heartbeat = null)
        {
            var context = new ServiceContext
            {
                Log                    = log,
                Scheduler              = scheduler,
                HttpParser             = httpParser,
                SystemClock            = systemClock,
                DateHeaderValueManager = dateHeaderValueManager,
                ConnectionManager      = connectionManager,
                Heartbeat              = heartbeat,
                ServerOptions          = serverOptions
            };

            return(context);
        }
Beispiel #22
0
 public Http2FrameWriter(
     PipeWriter outputPipeWriter,
     ConnectionContext connectionContext,
     Http2Connection http2Connection,
     OutputFlowControl connectionOutputFlowControl,
     ITimeoutControl timeoutControl,
     MinDataRate minResponseDataRate,
     string connectionId,
     MemoryPool <byte> memoryPool,
     IKestrelTrace log)
 {
     // Allow appending more data to the PipeWriter when a flush is pending.
     _outputWriter                = new ConcurrentPipeWriter(outputPipeWriter, memoryPool, _writeLock);
     _connectionContext           = connectionContext;
     _http2Connection             = http2Connection;
     _connectionOutputFlowControl = connectionOutputFlowControl;
     _connectionId                = connectionId;
     _log                  = log;
     _timeoutControl       = timeoutControl;
     _minResponseDataRate  = minResponseDataRate;
     _flusher              = new TimingPipeFlusher(_outputWriter, timeoutControl, log);
     _outgoingFrame        = new Http2Frame();
     _headerEncodingBuffer = new byte[_maxFrameSize];
 }
 protected UvRequest(IKestrelTrace logger) : base (logger)
 {
 }
Beispiel #24
0
 public Heartbeat(IHeartbeatHandler[] callbacks, ISystemClock systemClock, IDebugger debugger, IKestrelTrace trace)
 {
     _callbacks   = callbacks;
     _systemClock = systemClock;
     _debugger    = debugger;
     _trace       = trace;
     _interval    = Interval;
 }
 public NetworkingTests()
 {
     var engine = new KestrelEngine(new TestServiceContext());
     _uv = engine.Libuv;
     _logger = engine.Log;
 }
 private IHttpParser <RequestHandler> CreateParser(IKestrelTrace log) => new HttpParser <RequestHandler>(log.IsEnabled(LogLevel.Information));
Beispiel #27
0
 public UvWriteReq(IKestrelTrace logger) : base(logger)
 {
 }
 // For Testing
 internal ConnectionLimitMiddleware(ConnectionDelegate next, ResourceCounter concurrentConnectionCounter, IKestrelTrace trace)
 {
     _next = next;
     _concurrentConnectionCounter = concurrentConnectionCounter;
     _trace = trace;
 }
 public UvConnectRequest(IKestrelTrace logger) : base(logger)
 {
 }
Beispiel #30
0
 protected UvRequest(IKestrelTrace logger) : base(logger)
 {
 }
 public MockSocket(int threadId, IKestrelTrace logger)
     : base(logger)
 {
     // Set the handle to something other than IntPtr.Zero
     // so handle.Validate doesn't fail in Libuv.write
     handle = (IntPtr)1;
     _threadId = threadId;
 }
 public UvWriteReq(IKestrelTrace logger)
     : base(logger)
 {
 }
 public UvShutdownReq(IKestrelTrace logger) : base (logger)
 {
 }
 public UvConnectRequest(IKestrelTrace logger) : base (logger)
 {
 }
Beispiel #35
0
 public TestHttpOutputProducer(Pipe pipe, string connectionId, ConnectionContext connectionContext, IKestrelTrace log, ITimeoutControl timeoutControl, IHttpMinResponseDataRateFeature minResponseDataRateFeature) : base(pipe.Writer, connectionId, connectionContext, log, timeoutControl, minResponseDataRateFeature)
 {
     Pipe = pipe;
 }
 public ConnectionLimitMiddleware(ConnectionDelegate next, long connectionLimit, IKestrelTrace trace)
     : this(next, ResourceCounter.Quota(connectionLimit), trace)
 {
 }
 protected UvStreamHandle(IKestrelTrace logger) : base(logger)
 {
 }
 protected UvHandle(IKestrelTrace logger) : base (logger)
 {
 }
 public UvLoopHandle(IKestrelTrace logger) : base(logger)
 {
 }
Beispiel #40
0
 public TestServiceContext(ILoggerFactory loggerFactory, IKestrelTrace kestrelTrace)
 {
     Initialize(loggerFactory, new CompositeKestrelTrace(kestrelTrace, CreateLoggingTrace(loggerFactory)));
 }
Beispiel #41
0
 public CompositeKestrelTrace(IKestrelTrace kestrelTrace, KestrelTrace kestrelTrace1)
 {
     _trace1 = kestrelTrace;
     _trace2 = kestrelTrace1;
 }
Beispiel #42
0
 public UvTimerHandle(IKestrelTrace logger) : base(logger)
 {
 }
Beispiel #43
0
 public SocketOutput(KestrelThread thread, UvStreamHandle socket, long connectionId, IKestrelTrace log)
 {
     _thread           = thread;
     _socket           = socket;
     _connectionId     = connectionId;
     _log              = log;
     _callbacksPending = new Queue <CallbackContext>();
 }
 public UvAsyncHandle(IKestrelTrace logger) : base(logger)
 {
 }
 public MultipleLoopTests()
 {
     var engine = new KestrelEngine(new TestServiceContext());
     _uv = engine.Libuv;
     _logger = engine.Log;
 }
        private static MultiplexedConnectionDelegate EnforceConnectionLimit(MultiplexedConnectionDelegate innerDelegate, long?connectionLimit, IKestrelTrace trace)
        {
            if (!connectionLimit.HasValue)
            {
                return(innerDelegate);
            }

            return(new ConnectionLimitMiddleware <MultiplexedConnectionContext>(c => innerDelegate(c), connectionLimit.Value, trace).OnConnectionAsync);
        }
 public UvPipeHandle(IKestrelTrace logger) : base(logger)
 {
 }