Ejemplo n.º 1
0
            public void Dispose()
            {
                if (Parent == null)
                {
                    return; // disposed already
                }
                if (Context.DoNotReuse)
                {
                    Context.Dispose();
                    return;
                }

                if (Context.AllocatedMemory > Parent._maxContextSizeToKeepInBytes)
                {
                    Context.Dispose();
                    return;
                }

                if (Parent.LowMemoryFlag.IsRaised() && Context.PoolGeneration < Parent._generation)
                {
                    // releasing all the contexts which were created before we got the low memory event
                    Context.Dispose();
                    return;
                }

                Context.Reset(releaseAllocatedStringValues: true);
                // These contexts are reused, so we don't want to use LowerOrDie here.
                Context.InUse.Lower();
                Context.InPoolSince = DateTime.UtcNow;

                Parent.Push(Context);

                Parent  = null;
                Context = null;
            }
Ejemplo n.º 2
0
        protected LiveIoStatsCollector(IoChangesNotifications ioChanges, List <StorageEnvironmentWithType> environments, IEnumerable <DatabasePerformanceMetrics> performanceMetrics, JsonContextPoolBase <T> contextPool, CancellationToken resourceShutdown)
        {
            _ioChanges                   = ioChanges;
            _environments                = environments;
            _performanceMetrics          = performanceMetrics;
            _contextPool                 = contextPool;
            _resourceShutdown            = resourceShutdown;
            _perEnvironmentsFilesMetrics = new ConcurrentDictionary <string, BlockingCollection <IoMeterBuffer.MeterItem> >();
            _cts = new CancellationTokenSource();

            Task.Run(StartCollectingMetrics);
        }
        public async Task <bool> SendStatsOrHeartbeatToWebSocket <TContext>(Task <WebSocketReceiveResult> receive, WebSocket webSocket, JsonContextPoolBase <TContext> contextPool, MemoryStream ms, int timeToWait) where TContext : JsonOperationContext
        {
            if (receive.IsCompleted || webSocket.State != WebSocketState.Open)
            {
                return(false);
            }

            var tuple = await Stats.TryDequeueAsync(TimeSpan.FromMilliseconds(timeToWait));

            if (tuple.Item1 == false)
            {
                await webSocket.SendAsync(WebSocketHelper.Heartbeat, WebSocketMessageType.Text, true, CancellationToken);

                return(true);
            }

            ms.SetLength(0);

            using (contextPool.AllocateOperationContext(out TContext context))
                await using (var writer = new AsyncBlittableJsonTextWriter(context, ms))
                {
                    WriteStats(tuple.Item2, writer, context);
                    await writer.FlushAsync(CancellationToken);
                }

            ms.TryGetBuffer(out ArraySegment <byte> bytes);
            await webSocket.SendAsync(bytes, WebSocketMessageType.Text, true, CancellationToken);

            return(true);
        }