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.

SimpleRateLimitedOutputQueue.java 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*
  2. * Copyright (c) 2006-2017 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. /**
  25. * This is a simple rate limiting queue.
  26. * If more than 4 items are added in 4 seconds it will start limiting.
  27. * The first 4 items will be sent un-limited and then limiting will commence at
  28. * a rate of 1 per second.
  29. */
  30. public class SimpleRateLimitedOutputQueue extends OutputQueue {
  31. /** Current count. */
  32. private int count;
  33. /** Time last item was added. */
  34. private long lastItemTime;
  35. /** Are we limiting? */
  36. private boolean isLimiting;
  37. /** How many items are allowed before limiting? */
  38. private int items = 4;
  39. /** How many microseconds do we care about when checking for items? */
  40. private int limitTime = 4000;
  41. /** How long to wait in between each item when limiting? */
  42. private int waitTime = 3000;
  43. /** Always update the lastItemTime or only if its been > limitTime? */
  44. private boolean alwaysUpdateTime = true;
  45. /**
  46. * Create a new SimpleRateLimitedOutputQueue.
  47. */
  48. public SimpleRateLimitedOutputQueue() {
  49. super(QueueComparators.byPriorityThenNumber());
  50. }
  51. /**
  52. * Get the number of items needed to activate rate limiting.
  53. *
  54. * @return Number of items needed to activate rate limiting.
  55. */
  56. public int getItems() {
  57. return items;
  58. }
  59. /**
  60. * Set the number of items needed to activate rate limiting.
  61. *
  62. * @param items Number of items needed to activate rate limiting.
  63. */
  64. public void setItems(final int items) {
  65. this.items = items;
  66. }
  67. /**
  68. * Get the length of time that is used when checking for rate limiting. (If
  69. * more than getItems() number of lines are added less that this time apart
  70. * from each other then rate limiting is activated.)
  71. *
  72. * @return Number of items needed to activate rate limiting.
  73. */
  74. public int getLimitTime() {
  75. return limitTime;
  76. }
  77. /**
  78. * Set the length of time that is used when checking for rate limiting. (If
  79. * more than getItems() number of lines are added less that this time apart
  80. * from each other then rate limiting is activated.)
  81. *
  82. * @param limitTime Number of items needed to activate rate limiting.
  83. */
  84. public void setLimitTime(final int limitTime) {
  85. this.limitTime = limitTime;
  86. }
  87. /**
  88. * Get the length of time that we wait inbetween lines when limiting.
  89. *
  90. * @return length of time that we wait inbetween lines when limiting.
  91. */
  92. public int getWaitTime() {
  93. return waitTime;
  94. }
  95. /**
  96. * Set the length of time that we wait inbetween lines when limiting.
  97. *
  98. * @param waitTime length of time that we wait inbetween lines when limiting.
  99. */
  100. public void setWaitTime(final int waitTime) {
  101. this.waitTime = waitTime;
  102. }
  103. /**
  104. * Will the internal "lastItemTime" be updated every time an item is added,
  105. * or only after limitTime has passed?
  106. *
  107. * If true, assuming the default settings) items sent at 0, 3, 6, 9 will
  108. * activate rate limiting, if false it would need to be 0, 1, 2, 3.
  109. *
  110. * @return is LastItemTime always updated?
  111. */
  112. public boolean getAlwaysUpdateTime() {
  113. return alwaysUpdateTime;
  114. }
  115. /**
  116. * Set if the internal "lastItemTime" should be updated every time an item
  117. * is added, or only after limitTime has passed?
  118. *
  119. * If true, assuming the default settings) items sent at 0, 3, 6, 9 will
  120. * activate rate limiting, if false it would need to be 0, 1, 2, 3.
  121. *
  122. * @param alwaysUpdateTime Should LastItemTime always updated?
  123. */
  124. public void setAlwaysUpdateTime(final boolean alwaysUpdateTime) {
  125. this.alwaysUpdateTime = alwaysUpdateTime;
  126. }
  127. /**
  128. * Are we currently limiting?
  129. *
  130. * @return True if limiting is active.
  131. */
  132. public boolean isLimiting() {
  133. return isLimiting;
  134. }
  135. @Override
  136. protected void enqueue(final String line, final QueuePriority priority) {
  137. // Was the last line added less than limitTime ago?
  138. synchronized (this) {
  139. final boolean overTime = lastItemTime + limitTime > System.currentTimeMillis();
  140. if (overTime) {
  141. // If we are not currently limiting, and this is the items-th item
  142. // added in the last limitTime, start limiting.
  143. if (!isLimiting && ++count >= items) {
  144. isLimiting = true;
  145. count = 0;
  146. }
  147. } else if (isLimiting) {
  148. // It has been longer than limitTime and we are still shown as
  149. // limiting, check to see if the queue is empty or not, if it is
  150. // disable limiting.
  151. if (getQueue().isEmpty()) {
  152. isLimiting = false;
  153. }
  154. } else {
  155. // If it has been more than limitTime seconds since the last line
  156. // and we are not currently limiting, reset the count.
  157. count = 0;
  158. }
  159. if (alwaysUpdateTime || overTime) {
  160. lastItemTime = System.currentTimeMillis();
  161. }
  162. }
  163. super.enqueue(line, priority);
  164. }
  165. @Override
  166. protected void handleQueuedItems() {
  167. try {
  168. while (isQueueEnabled()) {
  169. send(getQueue().take().getLine());
  170. final boolean doSleep;
  171. synchronized (this) {
  172. doSleep = isLimiting;
  173. if (isLimiting && getQueue().isEmpty()) {
  174. isLimiting = false;
  175. }
  176. }
  177. if (doSleep) {
  178. try {
  179. Thread.sleep(waitTime);
  180. } catch (InterruptedException ex) {
  181. /* Do Nothing. */
  182. }
  183. }
  184. }
  185. } catch (InterruptedException ex) {
  186. // Do nothing
  187. }
  188. }
  189. }