michael@0: /* michael@0: * Copyright (c) 2009-2012 Niels Provos and Nick Mathewson michael@0: * michael@0: * Redistribution and use in source and binary forms, with or without michael@0: * modification, are permitted provided that the following conditions michael@0: * are met: michael@0: * 1. Redistributions of source code must retain the above copyright michael@0: * notice, this list of conditions and the following disclaimer. michael@0: * 2. Redistributions in binary form must reproduce the above copyright michael@0: * notice, this list of conditions and the following disclaimer in the michael@0: * documentation and/or other materials provided with the distribution. michael@0: * 3. The name of the author may not be used to endorse or promote products michael@0: * derived from this software without specific prior written permission. michael@0: * michael@0: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR michael@0: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES michael@0: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. michael@0: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, michael@0: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT michael@0: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, michael@0: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY michael@0: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT michael@0: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF michael@0: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. michael@0: */ michael@0: #ifndef _RATELIM_INTERNAL_H_ michael@0: #define _RATELIM_INTERNAL_H_ michael@0: michael@0: #ifdef __cplusplus michael@0: extern "C" { michael@0: #endif michael@0: michael@0: #include "event2/util.h" michael@0: michael@0: /** A token bucket is an internal structure that tracks how many bytes we are michael@0: * currently willing to read or write on a given bufferevent or group of michael@0: * bufferevents */ michael@0: struct ev_token_bucket { michael@0: /** How many bytes are we willing to read or write right now? These michael@0: * values are signed so that we can do "defecit spending" */ michael@0: ev_ssize_t read_limit, write_limit; michael@0: /** When was this bucket last updated? Measured in abstract 'ticks' michael@0: * relative to the token bucket configuration. */ michael@0: ev_uint32_t last_updated; michael@0: }; michael@0: michael@0: /** Configuration info for a token bucket or set of token buckets. */ michael@0: struct ev_token_bucket_cfg { michael@0: /** How many bytes are we willing to read on average per tick? */ michael@0: size_t read_rate; michael@0: /** How many bytes are we willing to read at most in any one tick? */ michael@0: size_t read_maximum; michael@0: /** How many bytes are we willing to write on average per tick? */ michael@0: size_t write_rate; michael@0: /** How many bytes are we willing to write at most in any one tick? */ michael@0: size_t write_maximum; michael@0: michael@0: /* How long is a tick? Note that fractions of a millisecond are michael@0: * ignored. */ michael@0: struct timeval tick_timeout; michael@0: michael@0: /* How long is a tick, in milliseconds? Derived from tick_timeout. */ michael@0: unsigned msec_per_tick; michael@0: }; michael@0: michael@0: /** The current tick is 'current_tick': add bytes to 'bucket' as specified in michael@0: * 'cfg'. */ michael@0: int ev_token_bucket_update(struct ev_token_bucket *bucket, michael@0: const struct ev_token_bucket_cfg *cfg, michael@0: ev_uint32_t current_tick); michael@0: michael@0: /** In which tick does 'tv' fall according to 'cfg'? Note that ticks can michael@0: * overflow easily; your code needs to handle this. */ michael@0: ev_uint32_t ev_token_bucket_get_tick(const struct timeval *tv, michael@0: const struct ev_token_bucket_cfg *cfg); michael@0: michael@0: /** Adjust 'bucket' to respect 'cfg', and note that it was last updated in michael@0: * 'current_tick'. If 'reinitialize' is true, we are changing the michael@0: * configuration of 'bucket'; otherwise, we are setting it up for the first michael@0: * time. michael@0: */ michael@0: int ev_token_bucket_init(struct ev_token_bucket *bucket, michael@0: const struct ev_token_bucket_cfg *cfg, michael@0: ev_uint32_t current_tick, michael@0: int reinitialize); michael@0: michael@0: int bufferevent_remove_from_rate_limit_group_internal(struct bufferevent *bev, michael@0: int unsuspend); michael@0: michael@0: /** Decrease the read limit of 'b' by 'n' bytes */ michael@0: #define ev_token_bucket_decrement_read(b,n) \ michael@0: do { \ michael@0: (b)->read_limit -= (n); \ michael@0: } while (0) michael@0: /** Decrease the write limit of 'b' by 'n' bytes */ michael@0: #define ev_token_bucket_decrement_write(b,n) \ michael@0: do { \ michael@0: (b)->write_limit -= (n); \ michael@0: } while (0) michael@0: michael@0: #ifdef __cplusplus michael@0: } michael@0: #endif michael@0: michael@0: #endif