xref: /libevent-2.1.12/ratelim-internal.h (revision 8ac3c4c2)
1737c9cd8SNick Mathewson /*
2e49e2891SNick Mathewson  * Copyright (c) 2009-2012 Niels Provos and Nick Mathewson
3737c9cd8SNick Mathewson  *
4737c9cd8SNick Mathewson  * Redistribution and use in source and binary forms, with or without
5737c9cd8SNick Mathewson  * modification, are permitted provided that the following conditions
6737c9cd8SNick Mathewson  * are met:
7737c9cd8SNick Mathewson  * 1. Redistributions of source code must retain the above copyright
8737c9cd8SNick Mathewson  *    notice, this list of conditions and the following disclaimer.
9737c9cd8SNick Mathewson  * 2. Redistributions in binary form must reproduce the above copyright
10737c9cd8SNick Mathewson  *    notice, this list of conditions and the following disclaimer in the
11737c9cd8SNick Mathewson  *    documentation and/or other materials provided with the distribution.
12737c9cd8SNick Mathewson  * 3. The name of the author may not be used to endorse or promote products
13737c9cd8SNick Mathewson  *    derived from this software without specific prior written permission.
14737c9cd8SNick Mathewson  *
15737c9cd8SNick Mathewson  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16737c9cd8SNick Mathewson  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17737c9cd8SNick Mathewson  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18737c9cd8SNick Mathewson  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19737c9cd8SNick Mathewson  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20737c9cd8SNick Mathewson  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21737c9cd8SNick Mathewson  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22737c9cd8SNick Mathewson  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23737c9cd8SNick Mathewson  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24737c9cd8SNick Mathewson  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25737c9cd8SNick Mathewson  */
263f8c7cd0SNick Mathewson #ifndef RATELIM_INTERNAL_H_INCLUDED_
273f8c7cd0SNick Mathewson #define RATELIM_INTERNAL_H_INCLUDED_
28737c9cd8SNick Mathewson 
29737c9cd8SNick Mathewson #ifdef __cplusplus
30737c9cd8SNick Mathewson extern "C" {
31737c9cd8SNick Mathewson #endif
32737c9cd8SNick Mathewson 
33fbe64f21SEvan Jones #include "event2/util.h"
34737c9cd8SNick Mathewson 
35737c9cd8SNick Mathewson /** A token bucket is an internal structure that tracks how many bytes we are
36737c9cd8SNick Mathewson  * currently willing to read or write on a given bufferevent or group of
37737c9cd8SNick Mathewson  * bufferevents */
38737c9cd8SNick Mathewson struct ev_token_bucket {
39737c9cd8SNick Mathewson 	/** How many bytes are we willing to read or write right now? These
40737c9cd8SNick Mathewson 	 * values are signed so that we can do "defecit spending" */
412cbb1a16SNick Mathewson 	ev_ssize_t read_limit, write_limit;
42737c9cd8SNick Mathewson 	/** When was this bucket last updated?  Measured in abstract 'ticks'
43737c9cd8SNick Mathewson 	 * relative to the token bucket configuration. */
44737c9cd8SNick Mathewson 	ev_uint32_t last_updated;
45737c9cd8SNick Mathewson };
46737c9cd8SNick Mathewson 
47737c9cd8SNick Mathewson /** Configuration info for a token bucket or set of token buckets. */
48737c9cd8SNick Mathewson struct ev_token_bucket_cfg {
49737c9cd8SNick Mathewson 	/** How many bytes are we willing to read on average per tick? */
502cbb1a16SNick Mathewson 	size_t read_rate;
51737c9cd8SNick Mathewson 	/** How many bytes are we willing to read at most in any one tick? */
522cbb1a16SNick Mathewson 	size_t read_maximum;
53737c9cd8SNick Mathewson 	/** How many bytes are we willing to write on average per tick? */
542cbb1a16SNick Mathewson 	size_t write_rate;
55737c9cd8SNick Mathewson 	/** How many bytes are we willing to write at most in any one tick? */
562cbb1a16SNick Mathewson 	size_t write_maximum;
57737c9cd8SNick Mathewson 
58737c9cd8SNick Mathewson 	/* How long is a tick?  Note that fractions of a millisecond are
59737c9cd8SNick Mathewson 	 * ignored. */
60737c9cd8SNick Mathewson 	struct timeval tick_timeout;
61737c9cd8SNick Mathewson 
62737c9cd8SNick Mathewson 	/* How long is a tick, in milliseconds?  Derived from tick_timeout. */
63737c9cd8SNick Mathewson 	unsigned msec_per_tick;
64737c9cd8SNick Mathewson };
65737c9cd8SNick Mathewson 
66737c9cd8SNick Mathewson /** The current tick is 'current_tick': add bytes to 'bucket' as specified in
67737c9cd8SNick Mathewson  * 'cfg'. */
68*8ac3c4c2SNick Mathewson int ev_token_bucket_update_(struct ev_token_bucket *bucket,
69737c9cd8SNick Mathewson     const struct ev_token_bucket_cfg *cfg,
70737c9cd8SNick Mathewson     ev_uint32_t current_tick);
71737c9cd8SNick Mathewson 
72737c9cd8SNick Mathewson /** In which tick does 'tv' fall according to 'cfg'?  Note that ticks can
73737c9cd8SNick Mathewson  * overflow easily; your code needs to handle this. */
74*8ac3c4c2SNick Mathewson ev_uint32_t ev_token_bucket_get_tick_(const struct timeval *tv,
75737c9cd8SNick Mathewson     const struct ev_token_bucket_cfg *cfg);
76737c9cd8SNick Mathewson 
77737c9cd8SNick Mathewson /** Adjust 'bucket' to respect 'cfg', and note that it was last updated in
78737c9cd8SNick Mathewson  * 'current_tick'.  If 'reinitialize' is true, we are changing the
79737c9cd8SNick Mathewson  * configuration of 'bucket'; otherwise, we are setting it up for the first
80737c9cd8SNick Mathewson  * time.
81737c9cd8SNick Mathewson  */
82*8ac3c4c2SNick Mathewson int ev_token_bucket_init_(struct ev_token_bucket *bucket,
83737c9cd8SNick Mathewson     const struct ev_token_bucket_cfg *cfg,
84737c9cd8SNick Mathewson     ev_uint32_t current_tick,
85737c9cd8SNick Mathewson     int reinitialize);
86737c9cd8SNick Mathewson 
87*8ac3c4c2SNick Mathewson int bufferevent_remove_from_rate_limit_group_internal_(struct bufferevent *bev,
880bffe43aSNick Mathewson     int unsuspend);
890bffe43aSNick Mathewson 
90737c9cd8SNick Mathewson /** Decrease the read limit of 'b' by 'n' bytes */
91737c9cd8SNick Mathewson #define ev_token_bucket_decrement_read(b,n)	\
92737c9cd8SNick Mathewson 	do {					\
93737c9cd8SNick Mathewson 		(b)->read_limit -= (n);		\
94737c9cd8SNick Mathewson 	} while (0)
95737c9cd8SNick Mathewson /** Decrease the write limit of 'b' by 'n' bytes */
96737c9cd8SNick Mathewson #define ev_token_bucket_decrement_write(b,n)	\
97737c9cd8SNick Mathewson 	do {					\
98737c9cd8SNick Mathewson 		(b)->write_limit -= (n);	\
99737c9cd8SNick Mathewson 	} while (0)
100737c9cd8SNick Mathewson 
101737c9cd8SNick Mathewson #ifdef __cplusplus
102737c9cd8SNick Mathewson }
103737c9cd8SNick Mathewson #endif
104737c9cd8SNick Mathewson 
105737c9cd8SNick Mathewson #endif
106