d67604baa2ed8063b28f9d038b354a819ca7280f
[sonitus.git] / src / main / java / net / pterodactylus / sonitus / data / filter / RateLimitingFilter.java
1 /*
2  * Sonitus - DelayFilter.java - Copyright © 2013 David Roden
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 package net.pterodactylus.sonitus.data.filter;
19
20 import java.io.IOException;
21 import java.util.logging.Logger;
22
23 import net.pterodactylus.sonitus.data.Metadata;
24
25 import com.google.common.eventbus.EventBus;
26
27 /**
28  * Rate limiting filter that only passes a specified amount of data per second
29  * from its {@link net.pterodactylus.sonitus.data.Source} to its {@link
30  * net.pterodactylus.sonitus.data.Sink}.
31  *
32  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
33  */
34 public class RateLimitingFilter extends DummyFilter {
35
36         /** The logger. */
37         private static final Logger logger = Logger.getLogger(RateLimitingFilter.class.getName());
38
39         /** The limiting rate in bytes/second. */
40         private final int rate;
41
42         /** The start time. */
43         private long startTime;
44
45         /** The number of bytes. */
46         private long counter;
47
48         /**
49          * Creates a new rate limiting filter.
50          *
51          * @param eventBus
52          *              The event bus
53          * @param name
54          *              The name of the filter
55          * @param rate
56          *              The limiting rate (in bytes/second)
57          */
58         public RateLimitingFilter(EventBus eventBus, String name, int rate) {
59                 this(eventBus, name, rate, 0);
60         }
61
62         /**
63          * Creates a new rate limiting filter.
64          *
65          * @param eventBus
66          *              The event bus
67          * @param name
68          *              The name of the filter
69          * @param rate
70          *              The limiting rate (in bytes/second)
71          * @param fastStartTime
72          *              The amount of time at the start of the filtering during which no delay
73          *              will occur (in milliseconds)
74          */
75         public RateLimitingFilter(EventBus eventBus, String name, int rate, long fastStartTime) {
76                 super(eventBus, name);
77                 this.rate = rate;
78                 this.counter = (long) (-rate * (fastStartTime / 1000.0));
79         }
80
81         //
82         // FILTER METHODS
83         //
84
85         @Override
86         public void open(Metadata metadata) throws IOException {
87                 super.open(metadata);
88                 startTime = System.currentTimeMillis();
89         }
90
91         @Override
92         public void process(byte[] buffer) throws IOException {
93                 super.process(buffer);
94                 /* delay. */
95                 counter += buffer.length;
96                 long waitTime = (long) (counter / (rate / 1000.0));
97                 while ((System.currentTimeMillis() - startTime) < waitTime) {
98                         try {
99                                 long limitDelay = waitTime - (System.currentTimeMillis() - startTime);
100                                 logger.finest(String.format("Waiting %d ms...", limitDelay));
101                                 Thread.sleep(limitDelay);
102                         } catch (InterruptedException ie1) {
103                                 /* ignore, keep looping. */
104                         }
105                 }
106                 logger.finest(String.format("Processed %d Bytes during %d ms, that’s %.1f bytes/s.", counter, System.currentTimeMillis() - startTime, counter / ((System.currentTimeMillis() - startTime) / 1000.0)));
107         }
108
109 }