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

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