// The caller of this method must have acquired this.asyncLock
            async Task FlushCoreAsync(FlushReason reason, CancellationToken cancelToken)
            {
                RelayEventSource.Log.HybridHttpResponseStreamFlush(this.TrackingContext, reason.ToString());
                if (!this.responseCommandSent)
                {
                    var responseCommand = CreateResponseCommand(this.context);
                    responseCommand.Body = true;

                    // At this point we have no choice but to rendezvous
                    await this.connection.EnsureRendezvousAsync(cancelToken).ConfigureAwait(false);

                    // Send the response command over the rendezvous connection
                    await this.connection.SendResponseAsync(responseCommand, null, cancelToken).ConfigureAwait(false);

                    this.responseCommandSent = true;

                    if (this.writeBufferStream != null && this.writeBufferStream.Length > 0)
                    {
                        var writeBuffer = this.writeBufferStream.GetArraySegment();
                        await this.connection.SendBytesOverRendezvousAsync(writeBuffer, WebSocketMessageType.Binary, false, cancelToken).ConfigureAwait(false);

                        this.writeBufferStream.Dispose();
                        this.writeBufferStream = null;
                        this.CancelWriteBufferFlushTimer();
                    }
                }
            }
示例#2
0
 protected void SendBatch(List <Envelope <TRecord> > records, long[] metrics, FlushReason reason)
 {
     _logger?.LogDebug($"Sink {this.Id} sending {metrics[0]} records {metrics[1]} bytes for reason {reason}.");
     if (_buffer == null)
     {
         OnNextBatch(records);
     }
     else
     {
         _buffer.Add(records);
     }
 }
 private void Flush(FlushReason reason)
 {
     if (_queue.Count > 0)
     {
         _maxTimeSpanTimer.Change(Timeout.Infinite, Timeout.Infinite);
         _onBatch(_queue, _counts, reason);
         Reset();
         if (reason != FlushReason.Stop)
         {
             _maxTimeSpanTimer.Change((int)_interval.TotalMilliseconds, (int)_interval.TotalMilliseconds);
         }
     }
 }
 protected void SendBatch(List <Envelope <TRecord> > records, long[] metrics, FlushReason reason)
 {
     _logger?.LogDebug("Sink {0} sending {1} records {2} bytes for reason {3}.", this.Id, metrics[0], metrics[1], reason);
     if (_buffer == null)
     {
         this._logger?.LogTrace("Sending new batch of {0} records directly to sink", records.Count);
         OnNextBatch(records);
     }
     else
     {
         this._logger?.LogTrace("Adding new batch of {0} records to buffer", records.Count);
         _buffer.Add(records);
     }
 }
        public void TestBatch()
        {
            List <long>  list       = new List <long>();
            FlushReason  lastReason = FlushReason.AfterAdd;
            Batch <long> batch      = new Batch <long>(TimeSpan.FromSeconds(0.2), 1000L, l => l,
                                                       (lst, counts, reason) =>
            {
                list.AddRange(lst);
                lastReason = reason;
            });

            list.Clear();
            batch.Add(500);
            Thread.Sleep(1000);
            //Should flush
            Assert.Single(list);
            Assert.Equal(FlushReason.Timer, lastReason);

            list.Clear();
            batch.Add(500);
            batch.Add(700);
            //Should flush
            Assert.Single(list);
            Assert.Equal(FlushReason.BeforeAdd, lastReason);

            list.Clear();
            batch.Add(300);
            //Should flush
            Assert.Equal(2, list.Count);
            Assert.Equal(FlushReason.AfterAdd, lastReason);

            list.Clear();
            batch.Add(500);
            //Should not flush
            Assert.Empty(list);
            batch.Stop();
            //Should flush now
            Assert.Single(list);
            Assert.Equal(FlushReason.Stop, lastReason);
        }