C# Client Library
A C# Client Library for the AnalyzeRe REST API
Loading...
Searching...
No Matches
AdaptiveBufferedBytesProducer.cs
Go to the documentation of this file.
1using System;
2
4{
7 public abstract class AdaptiveBufferedBytesProducer : ProducerConsumerBuffer<BufferedBytes>
8 {
13 private const int MaxQueueSize = 3;
14
17 private const int BufferScalingFactor = 4;
18
19 #region Public Properties
21 public int MaximumBufferLength { get; set; } = 33554432; // 2^25 (32 MiB)
22
30 public int MinimumBufferLength { get; set; } = 16384; // 2^14 (16 KiB)
31
33 public long TotalBytesRead { get; private set; }
34 #endregion Public Properties
35
36 #region Private Properties
37 // Stores the size of the last BufferedBytes byte array produced
38 private int _currentBufferSize;
39 #endregion Private Properties
40
41 #region Constructor
51 protected AdaptiveBufferedBytesProducer(int? min_buffer_length = null,
52 int? max_buffer_length = null)
53 : base(MaxQueueSize)
54 {
55 MinimumBufferLength = min_buffer_length ?? MinimumBufferLength;
56 MaximumBufferLength = max_buffer_length ?? MaximumBufferLength;
57 }
58 #endregion Constructor
59
60 #region Private Methods
62 protected override void OnStart()
63 {
66 _currentBufferSize = MinimumBufferLength;
67 }
68
73 protected abstract bool TryProduceNext(out BufferedBytes next, int buffer_size);
74
78 protected sealed override bool TryProduceNext(out BufferedBytes buffer)
79 {
80 if (!TryProduceNext(out buffer, _currentBufferSize))
81 return false;
82
83 // If the consumer is currently waiting, it took us too long to fill this buffer,
84 // so we should reduce the size of the next buffer so that we can deliver it faster.
86 {
87 while (_currentBufferSize > MinimumBufferLength && _currentBufferSize >= buffer.LengthFilled)
88 _currentBufferSize = Math.Max(_currentBufferSize / BufferScalingFactor, MinimumBufferLength);
89 }
90 // Otherwise, if the last buffer we produced is still queued up, the consumer is not
91 // blocked on this producer. We can try to increase the size of the next buffer
92 // we will produce to decrease the overhead of generating multiple smaller buffers.
93 else if (Produced.Count >= 1 && _currentBufferSize < MaximumBufferLength)
94 {
95 _currentBufferSize = Math.Min(_currentBufferSize * BufferScalingFactor, MaximumBufferLength);
96 }
97
98 TotalBytesRead += buffer.LengthFilled;
99 return true;
100 }
101 #endregion Private Methods
102 }
103}
Produces BufferedByte objects of a variable size depending on whether the consumer of this buffer is ...
int MaximumBufferLength
The max length of the each byte array to generate. (Default 32 MiB)
bool TryProduceNext(out BufferedBytes next, int buffer_size)
The method which asynchronously produces items.
override bool TryProduceNext(out BufferedBytes buffer)
Attempts to read from the stream.
AdaptiveBufferedBytesProducer(int? min_buffer_length=null, int? max_buffer_length=null)
Construct a new AdaptiveBufferedBytesProducer which will prepare increasingly large buffers of bytes ...
int MinimumBufferLength
The minimum length of the byte array to generate when the consumer of this buffer is currently waitin...
long TotalBytesRead
The total number of bytes that have been buffered since start.
override void OnStart()
Ensure the FirstBufferLength doesn't exceed the set buffer length.
Base class that consumes from a source asynchronously and produces a queue of some other resource to ...
bool IsConsumerWaiting
Determines whether the consumer of this buffer is currently waiting for it to produce some data.
BlockingCollection< TProduced > Produced
The collection of objects produced.
The structure containing an array of bytes and integer indicating the number of bytes in the array th...