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