1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/ipc/chromium/src/third_party/libevent/ratelim-internal.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,105 @@ 1.4 +/* 1.5 + * Copyright (c) 2009-2012 Niels Provos and Nick Mathewson 1.6 + * 1.7 + * Redistribution and use in source and binary forms, with or without 1.8 + * modification, are permitted provided that the following conditions 1.9 + * are met: 1.10 + * 1. Redistributions of source code must retain the above copyright 1.11 + * notice, this list of conditions and the following disclaimer. 1.12 + * 2. Redistributions in binary form must reproduce the above copyright 1.13 + * notice, this list of conditions and the following disclaimer in the 1.14 + * documentation and/or other materials provided with the distribution. 1.15 + * 3. The name of the author may not be used to endorse or promote products 1.16 + * derived from this software without specific prior written permission. 1.17 + * 1.18 + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 1.19 + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 1.20 + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 1.21 + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 1.22 + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 1.23 + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 1.24 + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 1.25 + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 1.26 + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 1.27 + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 1.28 + */ 1.29 +#ifndef _RATELIM_INTERNAL_H_ 1.30 +#define _RATELIM_INTERNAL_H_ 1.31 + 1.32 +#ifdef __cplusplus 1.33 +extern "C" { 1.34 +#endif 1.35 + 1.36 +#include "event2/util.h" 1.37 + 1.38 +/** A token bucket is an internal structure that tracks how many bytes we are 1.39 + * currently willing to read or write on a given bufferevent or group of 1.40 + * bufferevents */ 1.41 +struct ev_token_bucket { 1.42 + /** How many bytes are we willing to read or write right now? These 1.43 + * values are signed so that we can do "defecit spending" */ 1.44 + ev_ssize_t read_limit, write_limit; 1.45 + /** When was this bucket last updated? Measured in abstract 'ticks' 1.46 + * relative to the token bucket configuration. */ 1.47 + ev_uint32_t last_updated; 1.48 +}; 1.49 + 1.50 +/** Configuration info for a token bucket or set of token buckets. */ 1.51 +struct ev_token_bucket_cfg { 1.52 + /** How many bytes are we willing to read on average per tick? */ 1.53 + size_t read_rate; 1.54 + /** How many bytes are we willing to read at most in any one tick? */ 1.55 + size_t read_maximum; 1.56 + /** How many bytes are we willing to write on average per tick? */ 1.57 + size_t write_rate; 1.58 + /** How many bytes are we willing to write at most in any one tick? */ 1.59 + size_t write_maximum; 1.60 + 1.61 + /* How long is a tick? Note that fractions of a millisecond are 1.62 + * ignored. */ 1.63 + struct timeval tick_timeout; 1.64 + 1.65 + /* How long is a tick, in milliseconds? Derived from tick_timeout. */ 1.66 + unsigned msec_per_tick; 1.67 +}; 1.68 + 1.69 +/** The current tick is 'current_tick': add bytes to 'bucket' as specified in 1.70 + * 'cfg'. */ 1.71 +int ev_token_bucket_update(struct ev_token_bucket *bucket, 1.72 + const struct ev_token_bucket_cfg *cfg, 1.73 + ev_uint32_t current_tick); 1.74 + 1.75 +/** In which tick does 'tv' fall according to 'cfg'? Note that ticks can 1.76 + * overflow easily; your code needs to handle this. */ 1.77 +ev_uint32_t ev_token_bucket_get_tick(const struct timeval *tv, 1.78 + const struct ev_token_bucket_cfg *cfg); 1.79 + 1.80 +/** Adjust 'bucket' to respect 'cfg', and note that it was last updated in 1.81 + * 'current_tick'. If 'reinitialize' is true, we are changing the 1.82 + * configuration of 'bucket'; otherwise, we are setting it up for the first 1.83 + * time. 1.84 + */ 1.85 +int ev_token_bucket_init(struct ev_token_bucket *bucket, 1.86 + const struct ev_token_bucket_cfg *cfg, 1.87 + ev_uint32_t current_tick, 1.88 + int reinitialize); 1.89 + 1.90 +int bufferevent_remove_from_rate_limit_group_internal(struct bufferevent *bev, 1.91 + int unsuspend); 1.92 + 1.93 +/** Decrease the read limit of 'b' by 'n' bytes */ 1.94 +#define ev_token_bucket_decrement_read(b,n) \ 1.95 + do { \ 1.96 + (b)->read_limit -= (n); \ 1.97 + } while (0) 1.98 +/** Decrease the write limit of 'b' by 'n' bytes */ 1.99 +#define ev_token_bucket_decrement_write(b,n) \ 1.100 + do { \ 1.101 + (b)->write_limit -= (n); \ 1.102 + } while (0) 1.103 + 1.104 +#ifdef __cplusplus 1.105 +} 1.106 +#endif 1.107 + 1.108 +#endif