ratelim-internal.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. * Copyright (c) 2009-2012 Niels Provos and Nick Mathewson
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. * 1. Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * 2. Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * 3. The name of the author may not be used to endorse or promote products
  13. * derived from this software without specific prior written permission.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  16. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  17. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  18. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  19. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  20. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  21. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  22. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  24. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #ifndef RATELIM_INTERNAL_H_INCLUDED_
  27. #define RATELIM_INTERNAL_H_INCLUDED_
  28. #ifdef __cplusplus
  29. extern "C" {
  30. #endif
  31. #include "event2/util.h"
  32. /** A token bucket is an internal structure that tracks how many bytes we are
  33. * currently willing to read or write on a given bufferevent or group of
  34. * bufferevents */
  35. struct ev_token_bucket {
  36. /** How many bytes are we willing to read or write right now? These
  37. * values are signed so that we can do "defecit spending" */
  38. ev_ssize_t read_limit, write_limit;
  39. /** When was this bucket last updated? Measured in abstract 'ticks'
  40. * relative to the token bucket configuration. */
  41. ev_uint32_t last_updated;
  42. };
  43. /** Configuration info for a token bucket or set of token buckets. */
  44. struct ev_token_bucket_cfg {
  45. /** How many bytes are we willing to read on average per tick? */
  46. size_t read_rate;
  47. /** How many bytes are we willing to read at most in any one tick? */
  48. size_t read_maximum;
  49. /** How many bytes are we willing to write on average per tick? */
  50. size_t write_rate;
  51. /** How many bytes are we willing to write at most in any one tick? */
  52. size_t write_maximum;
  53. /* How long is a tick? Note that fractions of a millisecond are
  54. * ignored. */
  55. struct timeval tick_timeout;
  56. /* How long is a tick, in milliseconds? Derived from tick_timeout. */
  57. unsigned msec_per_tick;
  58. };
  59. /** The current tick is 'current_tick': add bytes to 'bucket' as specified in
  60. * 'cfg'. */
  61. int ev_token_bucket_update_(struct ev_token_bucket *bucket,
  62. const struct ev_token_bucket_cfg *cfg,
  63. ev_uint32_t current_tick);
  64. /** In which tick does 'tv' fall according to 'cfg'? Note that ticks can
  65. * overflow easily; your code needs to handle this. */
  66. ev_uint32_t ev_token_bucket_get_tick_(const struct timeval *tv,
  67. const struct ev_token_bucket_cfg *cfg);
  68. /** Adjust 'bucket' to respect 'cfg', and note that it was last updated in
  69. * 'current_tick'. If 'reinitialize' is true, we are changing the
  70. * configuration of 'bucket'; otherwise, we are setting it up for the first
  71. * time.
  72. */
  73. int ev_token_bucket_init_(struct ev_token_bucket *bucket,
  74. const struct ev_token_bucket_cfg *cfg,
  75. ev_uint32_t current_tick,
  76. int reinitialize);
  77. int bufferevent_remove_from_rate_limit_group_internal_(struct bufferevent *bev,
  78. int unsuspend);
  79. /** Decrease the read limit of 'b' by 'n' bytes */
  80. #define ev_token_bucket_decrement_read(b,n) \
  81. do { \
  82. (b)->read_limit -= (n); \
  83. } while (0)
  84. /** Decrease the write limit of 'b' by 'n' bytes */
  85. #define ev_token_bucket_decrement_write(b,n) \
  86. do { \
  87. (b)->write_limit -= (n); \
  88. } while (0)
  89. #ifdef __cplusplus
  90. }
  91. #endif
  92. #endif