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