You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SimpleRateLimitedQueueHandler.java 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /*
  2. * Copyright (c) 2006-2015 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package com.dmdirc.parser.irc.outputqueue;
  23. import com.dmdirc.parser.common.QueuePriority;
  24. import java.io.PrintWriter;
  25. import java.util.concurrent.BlockingQueue;
  26. /**
  27. * This is a simple rate limiting queue.
  28. * If more than 4 items are added in 4 seconds it will start limiting.
  29. * The first 4 items will be sent un-limited and then limiting will commence at
  30. * a rate of 1 per second.
  31. */
  32. public class SimpleRateLimitedQueueHandler extends QueueHandler {
  33. /** Current count. */
  34. private int count;
  35. /** Time last item was added. */
  36. private long lastItemTime;
  37. /** Are we limiting? */
  38. private boolean isLimiting;
  39. /** How many items are allowed before limiting? */
  40. private int items = 4;
  41. /** How many microseconds do we care about when checking for items? */
  42. private int limitTime = 4000;
  43. /** How long to wait in between each item when limiting? */
  44. private int waitTime = 3000;
  45. /** Always update the lastItemTime or only if its been > limitTime? */
  46. private boolean alwaysUpdateTime = true;
  47. /**
  48. * Create a new SimpleRateLimitedQueueHandler.
  49. *
  50. * @param outputQueue Owner of this Queue Handler
  51. * @param queue Queue to use
  52. * @param out Output Stream to use
  53. */
  54. public SimpleRateLimitedQueueHandler(
  55. final OutputQueue outputQueue,
  56. final BlockingQueue<QueueItem> queue,
  57. final PrintWriter out) {
  58. super(outputQueue, queue, QueueComparators.byPriorityThenNumber(), out);
  59. }
  60. /**
  61. * Get a QueueHandlerFactory that produces PriorityQueueHandlers.
  62. *
  63. * @return a QueueHandlerFactory that produces PrirortyQueueHandlers.
  64. */
  65. public static QueueHandlerFactory getFactory() {
  66. return SimpleRateLimitedQueueHandler::new;
  67. }
  68. /**
  69. * Get the number of items needed to activate rate limiting.
  70. *
  71. * @return Number of items needed to activate rate limiting.
  72. */
  73. public int getItems() {
  74. return items;
  75. }
  76. /**
  77. * Set the number of items needed to activate rate limiting.
  78. *
  79. * @param items Number of items needed to activate rate limiting.
  80. */
  81. public void setItems(final int items) {
  82. this.items = items;
  83. }
  84. /**
  85. * Get the length of time that is used when checking for rate limiting. (If
  86. * more than getItems() number of lines are added less that this time apart
  87. * from each other then rate limiting is activated.)
  88. *
  89. * @return Number of items needed to activate rate limiting.
  90. */
  91. public int getLimitTime() {
  92. return limitTime;
  93. }
  94. /**
  95. * Set the length of time that is used when checking for rate limiting. (If
  96. * more than getItems() number of lines are added less that this time apart
  97. * from each other then rate limiting is activated.)
  98. *
  99. * @param limitTime Number of items needed to activate rate limiting.
  100. */
  101. public void setLimitTime(final int limitTime) {
  102. this.limitTime = limitTime;
  103. }
  104. /**
  105. * Get the length of time that we wait inbetween lines when limiting.
  106. *
  107. * @return length of time that we wait inbetween lines when limiting.
  108. */
  109. public int getWaitTime() {
  110. return waitTime;
  111. }
  112. /**
  113. * Set the length of time that we wait inbetween lines when limiting.
  114. *
  115. * @param waitTime length of time that we wait inbetween lines when limiting.
  116. */
  117. public void setWaitTime(final int waitTime) {
  118. this.waitTime = waitTime;
  119. }
  120. /**
  121. * Will the internal "lastItemTime" be updated every time an item is added,
  122. * or only after limitTime has passed?
  123. *
  124. * If true, assuming the default settings) items sent at 0, 3, 6, 9 will
  125. * activate rate limiting, if false it would need to be 0, 1, 2, 3.
  126. *
  127. * @return is LastItemTime always updated?
  128. */
  129. public boolean getAlwaysUpdateTime() {
  130. return alwaysUpdateTime;
  131. }
  132. /**
  133. * Set if the internal "lastItemTime" should be updated every time an item
  134. * is added, or only after limitTime has passed?
  135. *
  136. * If true, assuming the default settings) items sent at 0, 3, 6, 9 will
  137. * activate rate limiting, if false it would need to be 0, 1, 2, 3.
  138. *
  139. * @param alwaysUpdateTime Should LastItemTime always updated?
  140. */
  141. public void setAlwaysUpdateTime(final boolean alwaysUpdateTime) {
  142. this.alwaysUpdateTime = alwaysUpdateTime;
  143. }
  144. /**
  145. * Are we currently limiting?
  146. *
  147. * @return True if limiting is active.
  148. */
  149. public boolean isLimiting() {
  150. return isLimiting;
  151. }
  152. @Override
  153. public QueueItem getQueueItem(final String line, final QueuePriority priority) {
  154. // Was the last line added less than limitTime ago?
  155. synchronized (this) {
  156. final boolean overTime = lastItemTime + limitTime > System.currentTimeMillis();
  157. if (overTime) {
  158. // If we are not currently limiting, and this is the items-th item
  159. // added in the last limitTime, start limiting.
  160. if (!isLimiting && ++count >= items) {
  161. isLimiting = true;
  162. count = 0;
  163. }
  164. } else if (isLimiting) {
  165. // It has been longer than limitTime and we are still shown as
  166. // limiting, check to see if the queue is empty or not, if it is
  167. // disable limiting.
  168. if (queue.isEmpty()) {
  169. isLimiting = false;
  170. }
  171. } else {
  172. // If it has been more than limitTime seconds since the last line
  173. // and we are not currently limiting, reset the count.
  174. count = 0;
  175. }
  176. if (alwaysUpdateTime || overTime) {
  177. lastItemTime = System.currentTimeMillis();
  178. }
  179. }
  180. return super.getQueueItem(line, priority);
  181. }
  182. @Override
  183. public void run() {
  184. try {
  185. while (outputQueue.isQueueEnabled()) {
  186. final QueueItem item = queue.take();
  187. sendLine(item.getLine());
  188. final boolean doSleep;
  189. synchronized (this) {
  190. doSleep = isLimiting;
  191. if (isLimiting && queue.isEmpty()) {
  192. isLimiting = false;
  193. }
  194. }
  195. if (doSleep) {
  196. try {
  197. Thread.sleep(waitTime);
  198. } catch (InterruptedException ex) { /* Do Nothing. */ }
  199. }
  200. }
  201. } catch (InterruptedException ex) {
  202. // Do nothing
  203. }
  204. }
  205. }