如何在 Java 中实现下载速率限制? [英] How can I implement a download rate limited in Java?

查看:45
本文介绍了如何在 Java 中实现下载速率限制?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为个人练习,我将用 Java 实现一个(简单的)下载器应用程序.它将在不同的线程中运行多个作业,这样我将在执行期间始终同时下载几个文件.

I'm going to implement a (simple) downloader application in Java as a personal exercise. It is going to run several jobs in different threads, in a way that I will be downloading a few files at the same time at all times during execution.

我希望能够定义在所有下载作业之间共享的下载速率限制,但即使对于单个下载任务,我也不知道该怎么做.我该怎么做呢?我应该尝试实施哪些解决方案?

I want to be able to define a download rate limit that is shared between all the download jobs, but I don't know how to do it even for a single download task. How should I go about doing this? What are the solutions I should try implementing?

谢谢.

推荐答案

我会从管理所有下载的 DownloadManager 开始.

I'd start with a DownloadManager that manages all downloads.

interface DownloadManager
{
    public InputStream registerDownload(InputStream stream);
}

所有想要参与托管带宽的代码都会在开始读取之前向下载管理器注册它的流.在 registerDownload() 方法中,管理器将给定的输入流包装在 ManagedBandwidthStream 中.

All code that wants to take part in managed bandwidth will register it's stream with the download manager before it starts reading from it. In it's registerDownload() method, the manager wraps the given input stream in a ManagedBandwidthStream.

   public class ManagedBandwidthStream extends InputStream
   {
      private DownloadManagerImpl owner;

      public ManagedBandwidthStream(
            InputStream original,
            DownloadManagerImpl owner
         )
      {
         super(original);
         this.owner = owner;
      }

      public int read(byte[] b, int offset, int length)
      {
          owner.read(this, b, offset, length);
      }

      // used by DownloadManager to actually read from the stream
      int actuallyRead(byte[] b, int offset, int length)
      {
          super.read(b, offset, length);
      }

      // also override other read() methods to delegate to the read() above
   }

流确保所有对 read() 的调用都被定向回下载管理器.

The stream ensures all calls to read() are directed back to the download manager.

class DownloadManagerImpl implements DownloadManager
{
   public InputStream registerDownload(InputStream in)
   {
       return new ManagedDownloadStream(in);
   }

   void read(ManagedDownloadStream source, byte[] b, int offset, int len)
   {
      // all your streams now call this method.
      // You can decide how much data to actually read.
      int allowed = getAllowedDataRead(source, len);
      int read = source.actuallyRead(b, offset, len);
      recordBytesRead(read);  // update counters for number of bytes read
   }
}

您的带宽分配策略是关于您如何实现 getAllowedDataRead().

Your bandwidth allocation strategy is then about how you implement getAllowedDataRead().

限制带宽的一种简单方法是,记录在给定时间段(例如 1 秒)内可以读取多少字节的计数器.每次调用 read 都会检查计数器并使用它来限制读取的实际字节数.计时器用于重置计数器.

A simple way of throttling the bandwidth is, keep a counter of how many more bytes can be read in a given period (e.g. 1 second). Each call to read examines the counter and uses that to restrict the actual number of bytes read. A timer is used to reset the counter.

在实践中,多个流之间的带宽分配可能会变得非常复杂,特别是为了避免饥饿和促进公平,但这应该会给您一个公平的开始.

In practice, the allocation of bandwith amongst multiple streams can get quite complex, espeically to avoid starvation and promote fairness, but this should give you a fair start.

这篇关于如何在 Java 中实现下载速率限制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆