Changeset 626
- Timestamp:
- 11/07/08 22:43:57 (4 months ago)
- Location:
- trunk
- Files:
-
- 4 modified
-
c2s/c2s.c (modified) (2 diffs)
-
ChangeLog (modified) (1 diff)
-
util/rate.c (modified) (4 diffs)
-
util/util.h (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
trunk/c2s/c2s.c
r525 r626 59 59 } 60 60 61 log_debug(ZONE, "%d is throttled, delaying read", sess->fd->fd); 62 63 buf->len = 0; 64 return 0; 61 log_write(sess->c2s->log, LOG_NOTICE, "%d is throttled, disconnecting", sess->fd->fd); 62 63 /* Disconnect the user. Ideally we would just stop 64 reading from their socket until the throttle time 65 expires. But that's difficult. */ 66 sx_kill(s); 67 return -1; 65 68 } 66 69 … … 71 74 } 72 75 76 /* no limit, just read as much as we can */ 77 else 78 rlen = buf->len; 79 73 80 /* do the read */ 74 len = recv(sess->fd->fd, buf->data, buf->len, 0); 81 len = recv(sess->fd->fd, buf->data, rlen, 0); 82 83 /* update rate limits */ 84 if(sess->rate != NULL) 85 rate_add(sess->rate, len); 75 86 76 87 if(len < 0) { -
trunk/ChangeLog
r625 r626 1 2008-07-11 Mark Doliner <mark@meebo.com> 2 * Fix c2s's byte rate limiting. 3 * Make c2s's connection rate limiting and router's byte rate limiting 4 work better. 5 1 6 2008-07-09 Mark Doliner <mark@meebo.com> 2 7 * Add a note to sm.xml that our Berkeley DB storage module doesn't -
trunk/util/rate.c
r545 r626 48 48 void rate_add(rate_t rt, int count) 49 49 { 50 time_t now; 51 52 now = time(NULL); 53 54 /* rate expired */ 55 if(now - rt->time >= rt->seconds) 56 rate_reset(rt); 57 50 58 rt->count += count; 51 59 52 60 /* first event, so set the time */ 53 61 if(rt->time == 0) 54 rt->time = time(NULL);62 rt->time = now; 55 63 56 64 /* uhoh, they stuffed up */ 57 65 if(rt->count >= rt->total) 58 rt->bad = time(NULL);66 rt->bad = now; 59 67 } 60 68 … … 70 78 int rate_check(rate_t rt) 71 79 { 72 time_t now;73 74 80 /* not tracking */ 75 81 if(rt->time == 0) … … 80 86 return 1; 81 87 82 now = time(NULL);83 84 88 /* currently bad */ 85 89 if(rt->bad != 0) 86 90 { 87 91 /* wait over, they're good again */ 88 if( now- rt->bad >= rt->wait)92 if(time(NULL) - rt->bad >= rt->wait) 89 93 { 90 94 rate_reset(rt); … … 96 100 } 97 101 98 /* rate expired */99 if(now - rt->time >= rt->seconds)100 {101 rate_reset(rt);102 return 1;103 }104 105 102 /* they're inside the time, and not bad yet */ 106 103 return 1; -
trunk/util/util.h
r602 r626 268 268 JABBERD2_API void rate_free(rate_t rt); 269 269 JABBERD2_API void rate_reset(rate_t rt); 270 271 /** 272 * Add a number of events to the counter. This takes care of moving 273 * the sliding window, if we've moved outside the previous window. 274 */ 270 275 JABBERD2_API void rate_add(rate_t rt, int count); 276 277 /** 278 * @return The amount of events we have left before we hit the rate 279 * limit. This could be number of bytes, or number of 280 * connection attempts, etc. 281 */ 271 282 JABBERD2_API int rate_left(rate_t rt); 272 JABBERD2_API int rate_check(rate_t rt); /* 1 == good, 0 == bad */ 283 284 /** 285 * @return 1 if we're under the rate limit and everything is fine or 286 * 0 if the rate limit has been exceeded and we should throttle 287 * something. 288 */ 289 JABBERD2_API int rate_check(rate_t rt); 273 290 274 291 /*
