aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.Server.Implementations/HttpServer/ThrottledStream.cs
diff options
context:
space:
mode:
Diffstat (limited to 'MediaBrowser.Server.Implementations/HttpServer/ThrottledStream.cs')
-rw-r--r--MediaBrowser.Server.Implementations/HttpServer/ThrottledStream.cs46
1 files changed, 31 insertions, 15 deletions
diff --git a/MediaBrowser.Server.Implementations/HttpServer/ThrottledStream.cs b/MediaBrowser.Server.Implementations/HttpServer/ThrottledStream.cs
index 067e53571..4bde30dac 100644
--- a/MediaBrowser.Server.Implementations/HttpServer/ThrottledStream.cs
+++ b/MediaBrowser.Server.Implementations/HttpServer/ThrottledStream.cs
@@ -15,6 +15,8 @@ namespace MediaBrowser.Server.Implementations.HttpServer
/// </summary>
public const long Infinite = 0;
+ public Func<long, long, long> ThrottleCallback { get; set; }
+
#region Private members
/// <summary>
/// The base stream.
@@ -278,22 +280,42 @@ namespace MediaBrowser.Server.Implementations.HttpServer
}
#endregion
- #region Protected methods
- /// <summary>
- /// Throttles for the specified buffer size in bytes.
- /// </summary>
- /// <param name="bufferSizeInBytes">The buffer size in bytes.</param>
- protected void Throttle(int bufferSizeInBytes)
+ private bool ThrottleCheck(int bufferSizeInBytes)
{
if (_bytesWritten < MinThrottlePosition)
{
- return;
+ return false;
}
// Make sure the buffer isn't empty.
if (_maximumBytesPerSecond <= 0 || bufferSizeInBytes <= 0)
{
- return;
+ return false;
+ }
+
+ if (ThrottleCallback != null)
+ {
+ var val = ThrottleCallback(_maximumBytesPerSecond, _bytesWritten);
+
+ if (val == 0)
+ {
+ return false;
+ }
+ }
+
+ return true;
+ }
+
+ #region Protected methods
+ /// <summary>
+ /// Throttles for the specified buffer size in bytes.
+ /// </summary>
+ /// <param name="bufferSizeInBytes">The buffer size in bytes.</param>
+ protected void Throttle(int bufferSizeInBytes)
+ {
+ if (!ThrottleCheck(bufferSizeInBytes))
+ {
+ return ;
}
_byteCount += bufferSizeInBytes;
@@ -332,13 +354,7 @@ namespace MediaBrowser.Server.Implementations.HttpServer
protected async Task ThrottleAsync(int bufferSizeInBytes, CancellationToken cancellationToken)
{
- if (_bytesWritten < MinThrottlePosition)
- {
- return;
- }
-
- // Make sure the buffer isn't empty.
- if (_maximumBytesPerSecond <= 0 || bufferSizeInBytes <= 0)
+ if (!ThrottleCheck(bufferSizeInBytes))
{
return;
}