我有一個將數據庫備份上傳到azure上的blob容器的應用程序。限制azure blob上傳速度的方法
我上傳文件8GB左右的規模,在11Mbps的平均速率,
一切工作正常不過,而斑被轉移到容器,它絕對殺死一切,網絡上的其他人。
所以我的問題是,有沒有辦法限制azure的上傳速度?
我有一個將數據庫備份上傳到azure上的blob容器的應用程序。限制azure blob上傳速度的方法
我上傳文件8GB左右的規模,在11Mbps的平均速率,
一切工作正常不過,而斑被轉移到容器,它絕對殺死一切,網絡上的其他人。
所以我的問題是,有沒有辦法限制azure的上傳速度?
Azure存儲本身不會提供特定的節流設置。但是:鑑於blob上傳本質上是一系列基於REST的塊blob PUT
操作,您可以通過執行自己的上傳實施並選擇將塊上傳到給定blob的頻率來執行自己的限制。
謝謝大衛,我現在就開始嘗試暗示這一點吧! –
大衛,你推薦這個人重新發明輪子。 –
@SaschaGottfried我推薦沒有這樣的事情。 OP想要的功能不存在。然而,塊上傳協議允許對塊進行這種管理(並且有很多關於上傳塊的文章 - 不需要重新創建)。 –
在網絡接口上設置最大上傳帶寬可能是一種更好的方法。這對您的客戶端應用程序是完全透明的,並且是一個好的設計目您的另一個QoS網絡中的軟件將無需自定義限制。
This SO question is about limiting network speed並列出了一些用於此目的的工具。如果運行數據庫備份的計算機正在使用Windows,請嘗試類似BWMeter以限制網絡速度。
一個更好的策略是配置網絡,使得Internet網關(WAN/DSL路由器)正在進行流量整形。這取決於產品是否足夠好。
這兩種方法的長期成本應低於在應用程序中實現自定義節流的替代方式。
您的問題沒有指定您希望實現上傳速度限制的編程語言(或原始REST)。但是我發現了一種使用官方的Azure存儲.NET SDK(Microsoft.WindowsAzure.Storage)的方法。
SDK本身不提供明確的速度限制。但是,Upload*Async()
函數(例如UploadFromFileAsync()
)通過採用IProgress<StorageProgress>
實現支持進度報告,並定期調用它的Report()
方法。 它似乎是同步調用它。
所以我們可以延遲Report()
來限制上傳速度。更棒的是,Report
會在BytesTransferred
上給你提供信息。所以如果你的進度處理程序跟蹤持續時間。然後你可以估計當前的上傳速度,並使用它來精確調節。
下面是這個想法的例子。請注意,這僅僅是這個想法的一個證明。我不到3分鐘就把它放在一起。速率估計和節流算法很粗糙,沒有很好的測試。
這裏是你將如何使用它
await blob.UploadFromFileAsync(
@"some_file.dat",
null, null, null,
new RateThrottleProgress(300 * 1024), // throttle at 300kb/s
CancellationToken.None);
指數回退節流實現
class RateThrottleProgress : IProgress<StorageProgress>
{
private readonly DateTime start = DateTime.Now;
private readonly long maxbps;
private long baseDelay, delay;
public RateThrottleProgress(long maxbps)
{
this.maxbps = maxbps;
baseDelay = 10;
delay = baseDelay;
}
public void Report(StorageProgress value)
{
double duration = (DateTime.Now - start).TotalSeconds;
double bps = value.BytesTransferred/duration;
if (bps > maxbps) delay *= 2;
else delay = Math.Max(baseDelay, delay/2);
Console.WriteLine($"current estimated upload speed: {bps/1024.0} KB/s. delay: {delay} ms");
Thread.Sleep(TimeSpan.FromMilliseconds(delay));
}
}
我也put below code snippet as a gist更好的版本控制和協作。
油門效果:
current estimated upload speed: 287.486007463505 KB/s. delay: 10 ms
current estimated upload speed: 290.086402388889 KB/s. delay: 10 ms
current estimated upload speed: 292.685419108659 KB/s. delay: 10 ms
current estimated upload speed: 295.28201245662 KB/s. delay: 10 ms
current estimated upload speed: 297.876060423937 KB/s. delay: 10 ms
current estimated upload speed: 300.469027029562 KB/s. delay: 20 ms
current estimated upload speed: 302.927815243916 KB/s. delay: 40 ms
current estimated upload speed: 305.112558483135 KB/s. delay: 80 ms
current estimated upload speed: 306.778888691779 KB/s. delay: 160 ms
current estimated upload speed: 307.367196107083 KB/s. delay: 320 ms <-- speed starts to drop from here...
current estimated upload speed: 305.910611140488 KB/s. delay: 640 ms
current estimated upload speed: 300.564767027164 KB/s. delay: 1280 ms
current estimated upload speed: 288.206861583389 KB/s. delay: 640 ms
current estimated upload speed: 283.672713628354 KB/s. delay: 320 ms
current estimated upload speed: 282.668039190231 KB/s. delay: 160 ms
current estimated upload speed: 283.351226090087 KB/s. delay: 80 ms
current estimated upload speed: 284.861107569046 KB/s. delay: 40 ms
current estimated upload speed: 286.781960850501 KB/s. delay: 20 ms
current estimated upload speed: 288.910675693183 KB/s. delay: 10 ms
current estimated upload speed: 291.140146046991 KB/s. delay: 10 ms
current estimated upload speed: 293.358817316007 KB/s. delay: 10 ms
這聽起來並不像是一個Azure的問題,而在你的最終的網絡配置問題。您需要調查QoS。 –
上傳速度夠快,網絡狀況良好。問題在於它的上傳速度太快了,我想以某種方式調節它。 –
「絕對會殺死該網絡上的所有其他設備」 - 這就是爲什麼我建議您調查QoS的原因。很明顯你的網絡不太好。 –