2010-11-19 4 views

답변

0

Proxy property을 대역폭 제한 HTTP 프록시 (예 : Squid can do this)에 연결할 수 있습니다. 어쩌면 편리한 해결책이 아니 겠지만 확실히 효과가있을 것입니다.

+1

를 단순화하기 위해 수신 같은 것을 사용하는 것이 좋습니다 코드에서 그 일을하는 경우. 종류의 나를 바보 같은 비트 내 자신의 throttler 롤링 느낌 : – 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; 
     } 
    } 
} 
관련 문제