2010-11-19 35 views

答えて

0

Proxy propertyを帯域幅制限HTTPプロキシに接続することができます(例:Squid can do this)。たぶん便利なソリューションではないかもしれませんが、間違いなく機能します。

+1

をRxのようなものを使用することをお勧めします私はこれが好きです。一種の私の気持ちは、私自身のスローラを少しばかげて感じさせます:) – Tom

+0

質問はコードで解決する方法で、それを達成するために別の外部ツールを使用する方法ではありません。 – SeriousM

1

あなたはコードでそれをやっている場合は、私は、タイマ等の使用を簡素化するために

class Uploader 
{ 
    /// <summary>Thread-safe flag to ensure that a packet isn't currently sending</summary> 
    private volatile bool isPacketSending = false; 

    /// <summary> 
    /// HTTP Posts a stream to a web address with a maximum bytes per second until the file is uploaded 
    /// </summary> 
    /// <param name="address">The web address to post the file to</param> 
    /// <param name="requestBody">The request body to stream at a maximum speed</param> 
    /// <param name="bytesPerSecond">The maximum number of bytes to send every second</param> 
    /// <returns>Returns an observable sequence of the bytes read so far</returns> 
    public IObservable<long> PostStreamThrottledAsync(Uri address, Stream requestBody, int bytesPerSecond) 
    { 
     if (!requestBody.CanRead) 
     { 
      throw new InvalidOperationException("Request body stream cannot be read from"); 
     } 

     return Observable.Using(
      () => 
       { 
        var client = new WebClient(); 
        return client.OpenWrite(address); 
       }, 
      outputStream => Observable.Return(0L).Concat(Observable.Interval(TimeSpan.FromSeconds(1))) 
         .TakeWhile(tick => SendPacket(requestBody, outputStream, bytesPerSecond) != 0) 
         .Select(_ => requestBody.Position)); 
    } 


    /// <summary> 
    /// Sends a packet up to the maximum bytes specified 
    /// </summary> 
    /// <param name="requestBody">The stream to read from</param> 
    /// <param name="output">The stream to write to</param> 
    /// <param name="bytesPerSecond">The number of bytes to send</param> 
    /// <returns>Returns the number of bytes actually sent; zero if at end of stream; -1 if we are exceeding throughput capacity.</returns> 
    private int SendPacket(Stream requestBody, Stream output, int bytesPerSecond) 
    { 
     if (isPacketSending) 
     { 
      return -1; 
     } 

     try 
     { 
      isPacketSending = true; 
      var buffer = new byte[bytesPerSecond]; 
      var bytesRead = requestBody.Read(buffer, 0, bytesPerSecond); 
      if (bytesRead != 0) 
      { 
       output.Write(buffer, 0, bytesRead); 
      } 

      return bytesRead; 
     } 
     finally 
     { 
      isPacketSending = false; 
     } 
    } 
} 
関連する問題