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.4KB

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