-
Notifications
You must be signed in to change notification settings - Fork 1
/
ThrottleManager.cs
158 lines (142 loc) · 5.39 KB
/
ThrottleManager.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// Copyright (C) 2024, The Duplicati Team
// https://duplicati.com, [email protected]
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
namespace Duplicati.StreamUtil;
/// <summary>
/// Manager class for throttling data transfer rates.
/// </summary>
public sealed class ThrottleManager
{
/// <summary>
/// Minimum time interval to measure data transfer rate over.
/// </summary>
private readonly long MinimumMeasureInterval = TimeSpan.FromMilliseconds(1).Ticks;
/// <summary>
/// Minimum time interval to wait between measurements.
/// </summary>
private readonly long MinimumWaitInterval = TimeSpan.FromMilliseconds(1).Ticks;
/// <summary>
/// The interval between each reset
/// </summary>
private readonly long MeasureWindowInterval = TimeSpan.FromSeconds(1).Ticks;
/// <summary>
/// The start time of the current period.
/// </summary>
private long _periodStart = DateTime.Now.Ticks;
/// <summary>
/// The amount of data transferred in the current period.
/// </summary>
private long _periodCount = 0;
/// <summary>
/// Lock object for thread safety.
/// </summary>
private readonly object _lock = new();
/// <summary>
/// The maximum data transfer rate in bytes per second.
/// </summary>
private long _limit = 0;
/// <summary>
/// Gets or sets the maximum data transfer rate in bytes per second.
/// </summary>
public long Limit
{
get => _limit;
set
{
if (value < 0)
throw new ArgumentOutOfRangeException(nameof(value));
lock (_lock)
{
_limit = value;
ResetPeriod();
}
}
}
/// <summary>
/// Delays the current request if the data transfer rate exceeds the limit.
/// </summary>
/// <param name="size">The size of the data to transfer.</param>
/// <returns>The time to wait before or after completing the operation.</returns>
public TimeSpan DelayForSize(long size)
{
if (size == 0 || _limit == 0)
return TimeSpan.Zero;
lock (_lock)
_periodCount += size;
var elapsedTicks = DateTime.Now.Ticks - _periodStart;
if (elapsedTicks < MinimumMeasureInterval)
return TimeSpan.Zero;
var bps = _periodCount * TimeSpan.TicksPerSecond / elapsedTicks;
if (bps > _limit)
{
var delay = (_periodCount * TimeSpan.TicksPerSecond / _limit) - elapsedTicks;
if (delay > MinimumWaitInterval)
{
if (elapsedTicks > MeasureWindowInterval)
ResetPeriod(delay);
return TimeSpan.FromTicks(delay);
}
}
// If the transfer is slower than the limit, reset the period once it has elapsed
if (elapsedTicks > MeasureWindowInterval)
ResetPeriod();
return TimeSpan.Zero;
}
/// <summary>
/// Delays the current request if the data transfer rate exceeds the limit.
/// </summary>
/// <param name="size">The size of the data to transfer.</param>
public void SleepForSize(long size)
{
var delay = DelayForSize(size);
if (delay != TimeSpan.Zero)
Thread.Sleep(delay);
}
/// <summary>
/// Waits for the data transfer rate to drop below the limit.
/// </summary>
/// <param name="size">The size of the data to transfer.</param>
/// <param name="cancellationToken">A token to cancel the operation.</param>
/// <returns>A task that completes when the data transfer rate is below the limit.</returns>
public Task WaitForSize(long size, CancellationToken cancellationToken)
{
var delay = DelayForSize(size);
if (delay == TimeSpan.Zero)
return Task.CompletedTask;
return Task.Delay(delay, cancellationToken);
}
/// <summary>
/// Resets the data transfer rate measurement period.
/// </summary>
public void ResetPeriod()
=> ResetPeriod(0);
/// <summary>
/// Resets the data transfer rate measurement period.
/// </summary>
/// <param name="sleepOffset">The amount of time to sleep before resetting the period.</param>
private void ResetPeriod(long sleepOffset)
{
lock (_lock)
{
_periodStart = DateTime.Now.Ticks + sleepOffset;
_periodCount = 0;
}
}
}