]> granicus.if.org Git - libevent/commitdiff
Support negative arguments to _bufferevent_decrement_(read/write)_buckets()
authorNick Mathewson <nickm@torproject.org>
Wed, 24 Aug 2011 22:42:00 +0000 (18:42 -0400)
committerNick Mathewson <nickm@torproject.org>
Sun, 28 Aug 2011 18:39:11 +0000 (14:39 -0400)
bufferevent_ratelim.c

index 31d385b5a4998c11a43f297186dfc54f403d48b2..a2205f78ac4585a53797e1662c2ab39380f4529e 100644 (file)
@@ -189,6 +189,8 @@ ev_token_bucket_cfg_free(struct ev_token_bucket_cfg *cfg)
 
 static int _bev_group_suspend_reading(struct bufferevent_rate_limit_group *g);
 static int _bev_group_suspend_writing(struct bufferevent_rate_limit_group *g);
+static void _bev_group_unsuspend_reading(struct bufferevent_rate_limit_group *g);
+static void _bev_group_unsuspend_writing(struct bufferevent_rate_limit_group *g);
 
 /** Helper: figure out the maximum amount we should write if is_write, or
     the maximum amount we should read if is_read.  Return that maximum, or
@@ -285,6 +287,10 @@ _bufferevent_decrement_read_buckets(struct bufferevent_private *bev, ev_ssize_t
                        if (event_add(&bev->rate_limiting->refill_bucket_event,
                                &bev->rate_limiting->cfg->tick_timeout) < 0)
                                r = -1;
+               } else if (bev->read_suspended & BEV_SUSPEND_BW) {
+                       if (!(bev->write_suspended & BEV_SUSPEND_BW))
+                               event_del(&bev->rate_limiting->refill_bucket_event);
+                       bufferevent_unsuspend_read(&bev->bev, BEV_SUSPEND_BW);
                }
        }
 
@@ -294,6 +300,8 @@ _bufferevent_decrement_read_buckets(struct bufferevent_private *bev, ev_ssize_t
                bev->rate_limiting->group->total_read += bytes;
                if (bev->rate_limiting->group->rate_limit.read_limit <= 0) {
                        _bev_group_suspend_reading(bev->rate_limiting->group);
+               } else if (bev->rate_limiting->group->read_suspended) {
+                       _bev_group_unsuspend_reading(bev->rate_limiting->group);
                }
                UNLOCK_GROUP(bev->rate_limiting->group);
        }
@@ -317,6 +325,10 @@ _bufferevent_decrement_write_buckets(struct bufferevent_private *bev, ev_ssize_t
                        if (event_add(&bev->rate_limiting->refill_bucket_event,
                                &bev->rate_limiting->cfg->tick_timeout) < 0)
                                r = -1;
+               } else if (bev->write_suspended & BEV_SUSPEND_BW) {
+                       if (!(bev->read_suspended & BEV_SUSPEND_BW))
+                               event_del(&bev->rate_limiting->refill_bucket_event);
+                       bufferevent_unsuspend_write(&bev->bev, BEV_SUSPEND_BW);
                }
        }
 
@@ -326,6 +338,8 @@ _bufferevent_decrement_write_buckets(struct bufferevent_private *bev, ev_ssize_t
                bev->rate_limiting->group->total_written += bytes;
                if (bev->rate_limiting->group->rate_limit.write_limit <= 0) {
                        _bev_group_suspend_writing(bev->rate_limiting->group);
+               } else if (bev->rate_limiting->group->write_suspended) {
+                       _bev_group_unsuspend_writing(bev->rate_limiting->group);
                }
                UNLOCK_GROUP(bev->rate_limiting->group);
        }