Lines Matching +full:- +full:g +full:-
2 * Copyright (c) 2007-2012 Niels Provos and Nick Mathewson
3 * Copyright (c) 2002-2006 Niels Provos <provos@citi.umich.edu>
28 #include "evconfig-private.h"
42 #include "ratelim-internal.h"
44 #include "bufferevent-internal.h"
45 #include "mm-internal.h"
46 #include "util-internal.h"
47 #include "event-internal.h"
57 already used who-knows-how-much bandwidth this tick. We in ev_token_bucket_init_()
61 if (bucket->read_limit > (ev_int64_t) cfg->read_maximum) in ev_token_bucket_init_()
62 bucket->read_limit = cfg->read_maximum; in ev_token_bucket_init_()
63 if (bucket->write_limit > (ev_int64_t) cfg->write_maximum) in ev_token_bucket_init_()
64 bucket->write_limit = cfg->write_maximum; in ev_token_bucket_init_()
66 bucket->read_limit = cfg->read_rate; in ev_token_bucket_init_()
67 bucket->write_limit = cfg->write_rate; in ev_token_bucket_init_()
68 bucket->last_updated = current_tick; in ev_token_bucket_init_()
80 unsigned n_ticks = current_tick - bucket->last_updated; in ev_token_bucket_update_()
88 bucket->limit += n_ticks * cfg->rate; in ev_token_bucket_update_()
90 if (bucket->limit > cfg->maximum) in ev_token_bucket_update_()
91 bucket->limit = cfg->maximum; in ev_token_bucket_update_()
96 if ((cfg->read_maximum - bucket->read_limit) / n_ticks < cfg->read_rate) in ev_token_bucket_update_()
97 bucket->read_limit = cfg->read_maximum; in ev_token_bucket_update_()
99 bucket->read_limit += n_ticks * cfg->read_rate; in ev_token_bucket_update_()
102 if ((cfg->write_maximum - bucket->write_limit) / n_ticks < cfg->write_rate) in ev_token_bucket_update_()
103 bucket->write_limit = cfg->write_maximum; in ev_token_bucket_update_()
105 bucket->write_limit += n_ticks * cfg->write_rate; in ev_token_bucket_update_()
108 bucket->last_updated = current_tick; in ev_token_bucket_update_()
119 event_base_gettimeofday_cached(bev->bev.ev_base, &now); in bufferevent_update_buckets()
120 tick = ev_token_bucket_get_tick_(&now, bev->rate_limiting->cfg); in bufferevent_update_buckets()
121 if (tick != bev->rate_limiting->limit.last_updated) in bufferevent_update_buckets()
122 ev_token_bucket_update_(&bev->rate_limiting->limit, in bufferevent_update_buckets()
123 bev->rate_limiting->cfg, tick); in bufferevent_update_buckets()
138 ev_uint64_t msec = (ev_uint64_t)tv->tv_sec * 1000 + tv->tv_usec / 1000; in ev_token_bucket_get_tick_()
139 return (unsigned)(msec / cfg->msec_per_tick); in ev_token_bucket_get_tick_()
148 struct timeval g; in ev_token_bucket_cfg_new() local
150 g.tv_sec = 1; in ev_token_bucket_cfg_new()
151 g.tv_usec = 0; in ev_token_bucket_cfg_new()
152 tick_len = &g; in ev_token_bucket_cfg_new()
165 r->read_rate = read_rate; in ev_token_bucket_cfg_new()
166 r->write_rate = write_rate; in ev_token_bucket_cfg_new()
167 r->read_maximum = read_burst; in ev_token_bucket_cfg_new()
168 r->write_maximum = write_burst; in ev_token_bucket_cfg_new()
169 memcpy(&r->tick_timeout, tick_len, sizeof(struct timeval)); in ev_token_bucket_cfg_new()
170 r->msec_per_tick = (tick_len->tv_sec * 1000) + in ev_token_bucket_cfg_new()
171 (tick_len->tv_usec & COMMON_TIMEOUT_MICROSECONDS_MASK)/1000; in ev_token_bucket_cfg_new()
185 #define LOCK_GROUP(g) EVLOCK_LOCK((g)->lock, 0) argument
186 #define UNLOCK_GROUP(g) EVLOCK_UNLOCK((g)->lock, 0) argument
188 static int bev_group_suspend_reading_(struct bufferevent_rate_limit_group *g);
189 static int bev_group_suspend_writing_(struct bufferevent_rate_limit_group *g);
190 static void bev_group_unsuspend_reading_(struct bufferevent_rate_limit_group *g);
191 static void bev_group_unsuspend_writing_(struct bufferevent_rate_limit_group *g);
201 ev_ssize_t max_so_far = is_write?bev->max_single_write:bev->max_single_read; in bufferevent_get_rlim_max_()
206 #define GROUP_SUSPENDED(g) \ in bufferevent_get_rlim_max_() argument
207 (is_write ? (g)->write_suspended : (g)->read_suspended) in bufferevent_get_rlim_max_()
216 if (!bev->rate_limiting) in bufferevent_get_rlim_max_()
219 /* If rate-limiting is enabled at all, update the appropriate in bufferevent_get_rlim_max_()
224 if (bev->rate_limiting->cfg) { in bufferevent_get_rlim_max_()
226 max_so_far = LIM(bev->rate_limiting->limit); in bufferevent_get_rlim_max_()
228 if (bev->rate_limiting->group) { in bufferevent_get_rlim_max_()
229 struct bufferevent_rate_limit_group *g = in bufferevent_get_rlim_max_() local
230 bev->rate_limiting->group; in bufferevent_get_rlim_max_()
232 LOCK_GROUP(g); in bufferevent_get_rlim_max_()
233 if (GROUP_SUSPENDED(g)) { in bufferevent_get_rlim_max_()
238 bufferevent_suspend_write_(&bev->bev, in bufferevent_get_rlim_max_()
241 bufferevent_suspend_read_(&bev->bev, in bufferevent_get_rlim_max_()
247 share = LIM(g->rate_limit) / g->n_members; in bufferevent_get_rlim_max_()
248 if (share < g->min_share) in bufferevent_get_rlim_max_()
249 share = g->min_share; in bufferevent_get_rlim_max_()
251 UNLOCK_GROUP(g); in bufferevent_get_rlim_max_()
278 if (!bev->rate_limiting) in bufferevent_decrement_read_buckets_()
281 if (bev->rate_limiting->cfg) { in bufferevent_decrement_read_buckets_()
282 bev->rate_limiting->limit.read_limit -= bytes; in bufferevent_decrement_read_buckets_()
283 if (bev->rate_limiting->limit.read_limit <= 0) { in bufferevent_decrement_read_buckets_()
284 bufferevent_suspend_read_(&bev->bev, BEV_SUSPEND_BW); in bufferevent_decrement_read_buckets_()
285 if (event_add(&bev->rate_limiting->refill_bucket_event, in bufferevent_decrement_read_buckets_()
286 &bev->rate_limiting->cfg->tick_timeout) < 0) in bufferevent_decrement_read_buckets_()
287 r = -1; in bufferevent_decrement_read_buckets_()
288 } else if (bev->read_suspended & BEV_SUSPEND_BW) { in bufferevent_decrement_read_buckets_()
289 if (!(bev->write_suspended & BEV_SUSPEND_BW)) in bufferevent_decrement_read_buckets_()
290 event_del(&bev->rate_limiting->refill_bucket_event); in bufferevent_decrement_read_buckets_()
291 bufferevent_unsuspend_read_(&bev->bev, BEV_SUSPEND_BW); in bufferevent_decrement_read_buckets_()
295 if (bev->rate_limiting->group) { in bufferevent_decrement_read_buckets_()
296 LOCK_GROUP(bev->rate_limiting->group); in bufferevent_decrement_read_buckets_()
297 bev->rate_limiting->group->rate_limit.read_limit -= bytes; in bufferevent_decrement_read_buckets_()
298 bev->rate_limiting->group->total_read += bytes; in bufferevent_decrement_read_buckets_()
299 if (bev->rate_limiting->group->rate_limit.read_limit <= 0) { in bufferevent_decrement_read_buckets_()
300 bev_group_suspend_reading_(bev->rate_limiting->group); in bufferevent_decrement_read_buckets_()
301 } else if (bev->rate_limiting->group->read_suspended) { in bufferevent_decrement_read_buckets_()
302 bev_group_unsuspend_reading_(bev->rate_limiting->group); in bufferevent_decrement_read_buckets_()
304 UNLOCK_GROUP(bev->rate_limiting->group); in bufferevent_decrement_read_buckets_()
316 if (!bev->rate_limiting) in bufferevent_decrement_write_buckets_()
319 if (bev->rate_limiting->cfg) { in bufferevent_decrement_write_buckets_()
320 bev->rate_limiting->limit.write_limit -= bytes; in bufferevent_decrement_write_buckets_()
321 if (bev->rate_limiting->limit.write_limit <= 0) { in bufferevent_decrement_write_buckets_()
322 bufferevent_suspend_write_(&bev->bev, BEV_SUSPEND_BW); in bufferevent_decrement_write_buckets_()
323 if (event_add(&bev->rate_limiting->refill_bucket_event, in bufferevent_decrement_write_buckets_()
324 &bev->rate_limiting->cfg->tick_timeout) < 0) in bufferevent_decrement_write_buckets_()
325 r = -1; in bufferevent_decrement_write_buckets_()
326 } else if (bev->write_suspended & BEV_SUSPEND_BW) { in bufferevent_decrement_write_buckets_()
327 if (!(bev->read_suspended & BEV_SUSPEND_BW)) in bufferevent_decrement_write_buckets_()
328 event_del(&bev->rate_limiting->refill_bucket_event); in bufferevent_decrement_write_buckets_()
329 bufferevent_unsuspend_write_(&bev->bev, BEV_SUSPEND_BW); in bufferevent_decrement_write_buckets_()
333 if (bev->rate_limiting->group) { in bufferevent_decrement_write_buckets_()
334 LOCK_GROUP(bev->rate_limiting->group); in bufferevent_decrement_write_buckets_()
335 bev->rate_limiting->group->rate_limit.write_limit -= bytes; in bufferevent_decrement_write_buckets_()
336 bev->rate_limiting->group->total_written += bytes; in bufferevent_decrement_write_buckets_()
337 if (bev->rate_limiting->group->rate_limit.write_limit <= 0) { in bufferevent_decrement_write_buckets_()
338 bev_group_suspend_writing_(bev->rate_limiting->group); in bufferevent_decrement_write_buckets_()
339 } else if (bev->rate_limiting->group->write_suspended) { in bufferevent_decrement_write_buckets_()
340 bev_group_unsuspend_writing_(bev->rate_limiting->group); in bufferevent_decrement_write_buckets_()
342 UNLOCK_GROUP(bev->rate_limiting->group); in bufferevent_decrement_write_buckets_()
348 /** Stop reading on every bufferevent in <b>g</b> */
350 bev_group_suspend_reading_(struct bufferevent_rate_limit_group *g) in bev_group_suspend_reading_() argument
354 g->read_suspended = 1; in bev_group_suspend_reading_()
355 g->pending_unsuspend_read = 0; in bev_group_suspend_reading_()
363 LIST_FOREACH(bev, &g->members, rate_limiting->next_in_group) { in bev_group_suspend_reading_()
364 if (EVLOCK_TRY_LOCK_(bev->lock)) { in bev_group_suspend_reading_()
365 bufferevent_suspend_read_(&bev->bev, in bev_group_suspend_reading_()
367 EVLOCK_UNLOCK(bev->lock, 0); in bev_group_suspend_reading_()
373 /** Stop writing on every bufferevent in <b>g</b> */
375 bev_group_suspend_writing_(struct bufferevent_rate_limit_group *g) in bev_group_suspend_writing_() argument
379 g->write_suspended = 1; in bev_group_suspend_writing_()
380 g->pending_unsuspend_write = 0; in bev_group_suspend_writing_()
381 LIST_FOREACH(bev, &g->members, rate_limiting->next_in_group) { in bev_group_suspend_writing_()
382 if (EVLOCK_TRY_LOCK_(bev->lock)) { in bev_group_suspend_writing_()
383 bufferevent_suspend_write_(&bev->bev, in bev_group_suspend_writing_()
385 EVLOCK_UNLOCK(bev->lock, 0); in bev_group_suspend_writing_()
400 BEV_LOCK(&bev->bev); in bev_refill_callback_()
401 if (!bev->rate_limiting || !bev->rate_limiting->cfg) { in bev_refill_callback_()
402 BEV_UNLOCK(&bev->bev); in bev_refill_callback_()
407 event_base_gettimeofday_cached(bev->bev.ev_base, &now); in bev_refill_callback_()
409 bev->rate_limiting->cfg); in bev_refill_callback_()
410 ev_token_bucket_update_(&bev->rate_limiting->limit, in bev_refill_callback_()
411 bev->rate_limiting->cfg, in bev_refill_callback_()
415 if ((bev->read_suspended & BEV_SUSPEND_BW)) { in bev_refill_callback_()
416 if (bev->rate_limiting->limit.read_limit > 0) in bev_refill_callback_()
417 bufferevent_unsuspend_read_(&bev->bev, BEV_SUSPEND_BW); in bev_refill_callback_()
421 if ((bev->write_suspended & BEV_SUSPEND_BW)) { in bev_refill_callback_()
422 if (bev->rate_limiting->limit.write_limit > 0) in bev_refill_callback_()
423 bufferevent_unsuspend_write_(&bev->bev, BEV_SUSPEND_BW); in bev_refill_callback_()
435 event_add(&bev->rate_limiting->refill_bucket_event, in bev_refill_callback_()
436 &bev->rate_limiting->cfg->tick_timeout); in bev_refill_callback_()
438 BEV_UNLOCK(&bev->bev); in bev_refill_callback_()
453 if (!group->n_members) in bev_group_random_element_()
456 EVUTIL_ASSERT(! LIST_EMPTY(&group->members)); in bev_group_random_element_()
458 which = evutil_weakrand_range_(&group->weakrand_seed, group->n_members); in bev_group_random_element_()
460 bev = LIST_FIRST(&group->members); in bev_group_random_element_()
461 while (which--) in bev_group_random_element_()
462 bev = LIST_NEXT(bev, rate_limiting->next_in_group); in bev_group_random_element_()
467 /** Iterate over the elements of a rate-limiting group 'g' with a random
471 We do this in a half-baked effort to get fairness among group members.
472 XXX Round-robin or some kind of priority queue would be even more fair.
476 first = bev_group_random_element_(g); \
477 for (bev = first; bev != LIST_END(&g->members); \
478 bev = LIST_NEXT(bev, rate_limiting->next_in_group)) { \
481 for (bev = LIST_FIRST(&g->members); bev && bev != first; \
482 bev = LIST_NEXT(bev, rate_limiting->next_in_group)) { \
488 bev_group_unsuspend_reading_(struct bufferevent_rate_limit_group *g) in bev_group_unsuspend_reading_() argument
493 g->read_suspended = 0; in bev_group_unsuspend_reading_()
495 if (EVLOCK_TRY_LOCK_(bev->lock)) { in bev_group_unsuspend_reading_()
496 bufferevent_unsuspend_read_(&bev->bev, in bev_group_unsuspend_reading_()
498 EVLOCK_UNLOCK(bev->lock, 0); in bev_group_unsuspend_reading_()
503 g->pending_unsuspend_read = again; in bev_group_unsuspend_reading_()
507 bev_group_unsuspend_writing_(struct bufferevent_rate_limit_group *g) in bev_group_unsuspend_writing_() argument
511 g->write_suspended = 0; in bev_group_unsuspend_writing_()
514 if (EVLOCK_TRY_LOCK_(bev->lock)) { in bev_group_unsuspend_writing_()
515 bufferevent_unsuspend_write_(&bev->bev, in bev_group_unsuspend_writing_()
517 EVLOCK_UNLOCK(bev->lock, 0); in bev_group_unsuspend_writing_()
522 g->pending_unsuspend_write = again; in bev_group_unsuspend_writing_()
531 struct bufferevent_rate_limit_group *g = arg; in bev_group_refill_callback_() local
535 event_base_gettimeofday_cached(event_get_base(&g->master_refill_event), &now); in bev_group_refill_callback_()
537 LOCK_GROUP(g); in bev_group_refill_callback_()
539 tick = ev_token_bucket_get_tick_(&now, &g->rate_limit_cfg); in bev_group_refill_callback_()
540 ev_token_bucket_update_(&g->rate_limit, &g->rate_limit_cfg, tick); in bev_group_refill_callback_()
542 if (g->pending_unsuspend_read || in bev_group_refill_callback_()
543 (g->read_suspended && (g->rate_limit.read_limit >= g->min_share))) { in bev_group_refill_callback_()
544 bev_group_unsuspend_reading_(g); in bev_group_refill_callback_()
546 if (g->pending_unsuspend_write || in bev_group_refill_callback_()
547 (g->write_suspended && (g->rate_limit.write_limit >= g->min_share))){ in bev_group_refill_callback_()
548 bev_group_unsuspend_writing_(g); in bev_group_refill_callback_()
556 UNLOCK_GROUP(g); in bev_group_refill_callback_()
564 int r = -1; in bufferevent_set_rate_limit()
569 /* XXX reference-count cfg */ in bufferevent_set_rate_limit()
574 if (bevp->rate_limiting) { in bufferevent_set_rate_limit()
575 rlim = bevp->rate_limiting; in bufferevent_set_rate_limit()
576 rlim->cfg = NULL; in bufferevent_set_rate_limit()
579 if (event_initialized(&rlim->refill_bucket_event)) in bufferevent_set_rate_limit()
580 event_del(&rlim->refill_bucket_event); in bufferevent_set_rate_limit()
586 event_base_gettimeofday_cached(bev->ev_base, &now); in bufferevent_set_rate_limit()
589 if (bevp->rate_limiting && bevp->rate_limiting->cfg == cfg) { in bufferevent_set_rate_limit()
590 /* no-op */ in bufferevent_set_rate_limit()
594 if (bevp->rate_limiting == NULL) { in bufferevent_set_rate_limit()
598 bevp->rate_limiting = rlim; in bufferevent_set_rate_limit()
600 rlim = bevp->rate_limiting; in bufferevent_set_rate_limit()
602 reinit = rlim->cfg != NULL; in bufferevent_set_rate_limit()
604 rlim->cfg = cfg; in bufferevent_set_rate_limit()
605 ev_token_bucket_init_(&rlim->limit, cfg, tick, reinit); in bufferevent_set_rate_limit()
608 EVUTIL_ASSERT(event_initialized(&rlim->refill_bucket_event)); in bufferevent_set_rate_limit()
609 event_del(&rlim->refill_bucket_event); in bufferevent_set_rate_limit()
611 event_assign(&rlim->refill_bucket_event, bev->ev_base, in bufferevent_set_rate_limit()
612 -1, EV_FINALIZE, bev_refill_callback_, bevp); in bufferevent_set_rate_limit()
614 if (rlim->limit.read_limit > 0) { in bufferevent_set_rate_limit()
620 if (rlim->limit.write_limit > 0) { in bufferevent_set_rate_limit()
628 event_add(&rlim->refill_bucket_event, &cfg->tick_timeout); in bufferevent_set_rate_limit()
641 struct bufferevent_rate_limit_group *g; in bufferevent_rate_limit_group_new() local
648 g = mm_calloc(1, sizeof(struct bufferevent_rate_limit_group)); in bufferevent_rate_limit_group_new()
649 if (!g) in bufferevent_rate_limit_group_new()
651 memcpy(&g->rate_limit_cfg, cfg, sizeof(g->rate_limit_cfg)); in bufferevent_rate_limit_group_new()
652 LIST_INIT(&g->members); in bufferevent_rate_limit_group_new()
654 ev_token_bucket_init_(&g->rate_limit, cfg, tick, 0); in bufferevent_rate_limit_group_new()
656 event_assign(&g->master_refill_event, base, -1, EV_PERSIST|EV_FINALIZE, in bufferevent_rate_limit_group_new()
657 bev_group_refill_callback_, g); in bufferevent_rate_limit_group_new()
659 event_add(&g->master_refill_event, &cfg->tick_timeout); in bufferevent_rate_limit_group_new()
661 EVTHREAD_ALLOC_LOCK(g->lock, EVTHREAD_LOCKTYPE_RECURSIVE); in bufferevent_rate_limit_group_new()
663 bufferevent_rate_limit_group_set_min_share(g, 64); in bufferevent_rate_limit_group_new()
665 evutil_weakrand_seed_(&g->weakrand_seed, in bufferevent_rate_limit_group_new()
666 (ev_uint32_t) ((now.tv_sec + now.tv_usec) + (ev_intptr_t)g)); in bufferevent_rate_limit_group_new()
668 return g; in bufferevent_rate_limit_group_new()
673 struct bufferevent_rate_limit_group *g, in bufferevent_rate_limit_group_set_cfg() argument
677 if (!g || !cfg) in bufferevent_rate_limit_group_set_cfg()
678 return -1; in bufferevent_rate_limit_group_set_cfg()
680 LOCK_GROUP(g); in bufferevent_rate_limit_group_set_cfg()
682 &g->rate_limit_cfg.tick_timeout, &cfg->tick_timeout, ==); in bufferevent_rate_limit_group_set_cfg()
683 memcpy(&g->rate_limit_cfg, cfg, sizeof(g->rate_limit_cfg)); in bufferevent_rate_limit_group_set_cfg()
685 if (g->rate_limit.read_limit > (ev_ssize_t)cfg->read_maximum) in bufferevent_rate_limit_group_set_cfg()
686 g->rate_limit.read_limit = cfg->read_maximum; in bufferevent_rate_limit_group_set_cfg()
687 if (g->rate_limit.write_limit > (ev_ssize_t)cfg->write_maximum) in bufferevent_rate_limit_group_set_cfg()
688 g->rate_limit.write_limit = cfg->write_maximum; in bufferevent_rate_limit_group_set_cfg()
692 event_add(&g->master_refill_event, &cfg->tick_timeout); in bufferevent_rate_limit_group_set_cfg()
696 bufferevent_rate_limit_group_set_min_share(g, g->configured_min_share); in bufferevent_rate_limit_group_set_cfg()
698 UNLOCK_GROUP(g); in bufferevent_rate_limit_group_set_cfg()
704 struct bufferevent_rate_limit_group *g, in bufferevent_rate_limit_group_set_min_share() argument
708 return -1; in bufferevent_rate_limit_group_set_min_share()
710 g->configured_min_share = share; in bufferevent_rate_limit_group_set_min_share()
712 /* Can't set share to less than the one-tick maximum. IOW, at steady in bufferevent_rate_limit_group_set_min_share()
714 if (share > g->rate_limit_cfg.read_rate) in bufferevent_rate_limit_group_set_min_share()
715 share = g->rate_limit_cfg.read_rate; in bufferevent_rate_limit_group_set_min_share()
716 if (share > g->rate_limit_cfg.write_rate) in bufferevent_rate_limit_group_set_min_share()
717 share = g->rate_limit_cfg.write_rate; in bufferevent_rate_limit_group_set_min_share()
719 g->min_share = share; in bufferevent_rate_limit_group_set_min_share()
724 bufferevent_rate_limit_group_free(struct bufferevent_rate_limit_group *g) in bufferevent_rate_limit_group_free() argument
726 LOCK_GROUP(g); in bufferevent_rate_limit_group_free()
727 EVUTIL_ASSERT(0 == g->n_members); in bufferevent_rate_limit_group_free()
728 event_del(&g->master_refill_event); in bufferevent_rate_limit_group_free()
729 UNLOCK_GROUP(g); in bufferevent_rate_limit_group_free()
730 EVTHREAD_FREE_LOCK(g->lock, EVTHREAD_LOCKTYPE_RECURSIVE); in bufferevent_rate_limit_group_free()
731 mm_free(g); in bufferevent_rate_limit_group_free()
736 struct bufferevent_rate_limit_group *g) in bufferevent_add_to_rate_limit_group() argument
742 if (!bevp->rate_limiting) { in bufferevent_add_to_rate_limit_group()
747 return -1; in bufferevent_add_to_rate_limit_group()
749 event_assign(&rlim->refill_bucket_event, bev->ev_base, in bufferevent_add_to_rate_limit_group()
750 -1, EV_FINALIZE, bev_refill_callback_, bevp); in bufferevent_add_to_rate_limit_group()
751 bevp->rate_limiting = rlim; in bufferevent_add_to_rate_limit_group()
754 if (bevp->rate_limiting->group == g) { in bufferevent_add_to_rate_limit_group()
758 if (bevp->rate_limiting->group) in bufferevent_add_to_rate_limit_group()
761 LOCK_GROUP(g); in bufferevent_add_to_rate_limit_group()
762 bevp->rate_limiting->group = g; in bufferevent_add_to_rate_limit_group()
763 ++g->n_members; in bufferevent_add_to_rate_limit_group()
764 LIST_INSERT_HEAD(&g->members, bevp, rate_limiting->next_in_group); in bufferevent_add_to_rate_limit_group()
766 rsuspend = g->read_suspended; in bufferevent_add_to_rate_limit_group()
767 wsuspend = g->write_suspended; in bufferevent_add_to_rate_limit_group()
769 UNLOCK_GROUP(g); in bufferevent_add_to_rate_limit_group()
792 if (bevp->rate_limiting && bevp->rate_limiting->group) { in bufferevent_remove_from_rate_limit_group_internal_()
793 struct bufferevent_rate_limit_group *g = in bufferevent_remove_from_rate_limit_group_internal_() local
794 bevp->rate_limiting->group; in bufferevent_remove_from_rate_limit_group_internal_()
795 LOCK_GROUP(g); in bufferevent_remove_from_rate_limit_group_internal_()
796 bevp->rate_limiting->group = NULL; in bufferevent_remove_from_rate_limit_group_internal_()
797 --g->n_members; in bufferevent_remove_from_rate_limit_group_internal_()
798 LIST_REMOVE(bevp, rate_limiting->next_in_group); in bufferevent_remove_from_rate_limit_group_internal_()
799 UNLOCK_GROUP(g); in bufferevent_remove_from_rate_limit_group_internal_()
825 if (bevp->rate_limiting && bevp->rate_limiting->cfg) { in bufferevent_get_read_limit()
827 r = bevp->rate_limiting->limit.read_limit; in bufferevent_get_read_limit()
844 if (bevp->rate_limiting && bevp->rate_limiting->cfg) { in bufferevent_get_write_limit()
846 r = bevp->rate_limiting->limit.write_limit; in bufferevent_get_write_limit()
861 bevp->max_single_read = MAX_SINGLE_READ_DEFAULT; in bufferevent_set_max_single_read()
863 bevp->max_single_read = size; in bufferevent_set_max_single_read()
875 bevp->max_single_write = MAX_SINGLE_WRITE_DEFAULT; in bufferevent_set_max_single_write()
877 bevp->max_single_write = size; in bufferevent_set_max_single_write()
888 r = BEV_UPCAST(bev)->max_single_read; in bufferevent_get_max_single_read()
899 r = BEV_UPCAST(bev)->max_single_write; in bufferevent_get_max_single_write()
931 if (bufev_private->rate_limiting) { in bufferevent_get_token_bucket_cfg()
932 cfg = bufev_private->rate_limiting->cfg; in bufferevent_get_token_bucket_cfg()
950 r = grp->rate_limit.read_limit; in bufferevent_rate_limit_group_get_read_limit()
963 r = grp->rate_limit.write_limit; in bufferevent_rate_limit_group_get_write_limit()
976 EVUTIL_ASSERT(bevp->rate_limiting && bevp->rate_limiting->cfg); in bufferevent_decrement_read_limit()
977 old_limit = bevp->rate_limiting->limit.read_limit; in bufferevent_decrement_read_limit()
979 new_limit = (bevp->rate_limiting->limit.read_limit -= decr); in bufferevent_decrement_read_limit()
982 if (event_add(&bevp->rate_limiting->refill_bucket_event, in bufferevent_decrement_read_limit()
983 &bevp->rate_limiting->cfg->tick_timeout) < 0) in bufferevent_decrement_read_limit()
984 r = -1; in bufferevent_decrement_read_limit()
986 if (!(bevp->write_suspended & BEV_SUSPEND_BW)) in bufferevent_decrement_read_limit()
987 event_del(&bevp->rate_limiting->refill_bucket_event); in bufferevent_decrement_read_limit()
998 /* XXXX this is mostly copy-and-paste from in bufferevent_decrement_write_limit()
1005 EVUTIL_ASSERT(bevp->rate_limiting && bevp->rate_limiting->cfg); in bufferevent_decrement_write_limit()
1006 old_limit = bevp->rate_limiting->limit.write_limit; in bufferevent_decrement_write_limit()
1008 new_limit = (bevp->rate_limiting->limit.write_limit -= decr); in bufferevent_decrement_write_limit()
1011 if (event_add(&bevp->rate_limiting->refill_bucket_event, in bufferevent_decrement_write_limit()
1012 &bevp->rate_limiting->cfg->tick_timeout) < 0) in bufferevent_decrement_write_limit()
1013 r = -1; in bufferevent_decrement_write_limit()
1015 if (!(bevp->read_suspended & BEV_SUSPEND_BW)) in bufferevent_decrement_write_limit()
1016 event_del(&bevp->rate_limiting->refill_bucket_event); in bufferevent_decrement_write_limit()
1031 old_limit = grp->rate_limit.read_limit; in bufferevent_rate_limit_group_decrement_read()
1032 new_limit = (grp->rate_limit.read_limit -= decr); in bufferevent_rate_limit_group_decrement_read()
1051 old_limit = grp->rate_limit.write_limit; in bufferevent_rate_limit_group_decrement_write()
1052 new_limit = (grp->rate_limit.write_limit -= decr); in bufferevent_rate_limit_group_decrement_write()
1070 *total_read_out = grp->total_read; in bufferevent_rate_limit_group_get_totals()
1072 *total_written_out = grp->total_written; in bufferevent_rate_limit_group_get_totals()
1078 grp->total_read = grp->total_written = 0; in bufferevent_rate_limit_group_reset_totals()
1084 bev->rate_limiting = NULL; in bufferevent_ratelim_init_()
1085 bev->max_single_read = MAX_SINGLE_READ_DEFAULT; in bufferevent_ratelim_init_()
1086 bev->max_single_write = MAX_SINGLE_WRITE_DEFAULT; in bufferevent_ratelim_init_()