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 8.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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(final OutputQueue outputQueue, final BlockingQueue<QueueItem> queue, final PrintWriter out) {
  55. super(outputQueue, queue, out);
  56. }
  57. /**
  58. * Get a QueueHandlerFactory that produces PriorityQueueHandlers.
  59. *
  60. * @return a QueueHandlerFactory that produces PrirortyQueueHandlers.
  61. */
  62. public static QueueHandlerFactory getFactory() {
  63. return SimpleRateLimitedQueueHandler::new;
  64. }
  65. /**
  66. * Get the number of items needed to activate rate limiting.
  67. *
  68. * @return Number of items needed to activate rate limiting.
  69. */
  70. public int getItems() {
  71. return items;
  72. }
  73. /**
  74. * Set the number of items needed to activate rate limiting.
  75. *
  76. * @param items Number of items needed to activate rate limiting.
  77. */
  78. public void setItems(final int items) {
  79. this.items = items;
  80. }
  81. /**
  82. * Get the length of time that is used when checking for rate limiting. (If
  83. * more than getItems() number of lines are added less that this time apart
  84. * from each other then rate limiting is activated.)
  85. *
  86. * @return Number of items needed to activate rate limiting.
  87. */
  88. public int getLimitTime() {
  89. return limitTime;
  90. }
  91. /**
  92. * Set the length of time that is used when checking for rate limiting. (If
  93. * more than getItems() number of lines are added less that this time apart
  94. * from each other then rate limiting is activated.)
  95. *
  96. * @param limitTime Number of items needed to activate rate limiting.
  97. */
  98. public void setLimitTime(final int limitTime) {
  99. this.limitTime = limitTime;
  100. }
  101. /**
  102. * Get the length of time that we wait inbetween lines when limiting.
  103. *
  104. * @return length of time that we wait inbetween lines when limiting.
  105. */
  106. public int getWaitTime() {
  107. return waitTime;
  108. }
  109. /**
  110. * Set the length of time that we wait inbetween lines when limiting.
  111. *
  112. * @param waitTime length of time that we wait inbetween lines when limiting.
  113. */
  114. public void setWaitTime(final int waitTime) {
  115. this.waitTime = waitTime;
  116. }
  117. /**
  118. * Will the internal "lastItemTime" be updated every time an item is added,
  119. * or only after limitTime has passed?
  120. *
  121. * If true, assuming the default settings) items sent at 0, 3, 6, 9 will
  122. * activate rate limiting, if false it would need to be 0, 1, 2, 3.
  123. *
  124. * @return is LastItemTime always updated?
  125. */
  126. public boolean getAlwaysUpdateTime() {
  127. return alwaysUpdateTime;
  128. }
  129. /**
  130. * Set if the internal "lastItemTime" should be updated every time an item
  131. * is added, or only after limitTime has passed?
  132. *
  133. * If true, assuming the default settings) items sent at 0, 3, 6, 9 will
  134. * activate rate limiting, if false it would need to be 0, 1, 2, 3.
  135. *
  136. * @param alwaysUpdateTime Should LastItemTime always updated?
  137. */
  138. public void setAlwaysUpdateTime(final boolean alwaysUpdateTime) {
  139. this.alwaysUpdateTime = alwaysUpdateTime;
  140. }
  141. /**
  142. * Are we currently limiting?
  143. *
  144. * @return True if limiting is active.
  145. */
  146. public boolean isLimiting() {
  147. return isLimiting;
  148. }
  149. /**
  150. * Compare queue items, if priorities differ, then higher priority items
  151. * will always be put further ahead in the queue (This queue ignores the
  152. * 10-second rule of the normal queue) otherwise the normal comparison is
  153. * used.
  154. */
  155. @Override
  156. public int compare(final QueueItem mainObject, final QueueItem otherObject) {
  157. if (mainObject.getPriority().compareTo(otherObject.getPriority()) == 0) {
  158. return super.compare(mainObject, otherObject);
  159. } else {
  160. return mainObject.getPriority().compareTo(otherObject.getPriority());
  161. }
  162. }
  163. @Override
  164. public QueueItem getQueueItem(final String line, final QueuePriority priority) {
  165. // Was the last line added less than limitTime ago?
  166. synchronized (this) {
  167. final boolean overTime = lastItemTime + limitTime > System.currentTimeMillis();
  168. if (overTime) {
  169. // If we are not currently limiting, and this is the items-th item
  170. // added in the last limitTime, start limiting.
  171. if (!isLimiting && ++count > (items - 1)) {
  172. isLimiting = true;
  173. count = 0;
  174. }
  175. } else if (isLimiting) {
  176. // It has been longer than limitTime and we are still shown as
  177. // limiting, check to see if the queue is empty or not, if it is
  178. // disable limiting.
  179. if (queue.isEmpty()) {
  180. isLimiting = false;
  181. }
  182. } else {
  183. // If it has been more than limitTime seconds since the last line
  184. // and we are not currently limiting, reset the count.
  185. count = 0;
  186. }
  187. if (alwaysUpdateTime || overTime) {
  188. lastItemTime = System.currentTimeMillis();
  189. }
  190. }
  191. return super.getQueueItem(line, priority);
  192. }
  193. @Override
  194. public void run() {
  195. try {
  196. while (outputQueue.isQueueEnabled()) {
  197. final QueueItem item = queue.take();
  198. sendLine(item.getLine());
  199. final boolean doSleep;
  200. synchronized (this) {
  201. doSleep = isLimiting;
  202. if (isLimiting && queue.isEmpty()) {
  203. isLimiting = false;
  204. }
  205. }
  206. if (doSleep) {
  207. try {
  208. Thread.sleep(waitTime);
  209. } catch (InterruptedException ex) { /* Do Nothing. */ }
  210. }
  211. }
  212. } catch (InterruptedException ex) {
  213. // Do nothing
  214. }
  215. }
  216. }