Support IO token bucket capacity > refill rate

This commit is contained in:
Calvin Montgomery 2018-11-15 23:04:03 -08:00
parent 8456b6a125
commit 4c9e85b293
4 changed files with 21 additions and 7 deletions

View file

@ -397,11 +397,16 @@ function preprocessConfig(cfg) {
});
if (!cfg.io.throttle) {
cfg.io.throttle = {
'in-rate-limit': Infinity
};
cfg.io.throttle = {};
}
cfg.io.throttle = Object.assign({
'in-rate-limit': Infinity
}, cfg.io.throttle);
cfg.io.throttle = Object.assign({
'bucket-capacity': cfg.io.throttle['in-rate-limit']
}, cfg.io.throttle);
return cfg;
}

View file

@ -251,16 +251,17 @@ class IOServer {
}
setRateLimiter(socket) {
const thunk = () => Config.get('io.throttle.in-rate-limit');
const refillRate = () => Config.get('io.throttle.in-rate-limit');
const capacity = () => Config.get('io.throttle.bucket-capacity');
socket._inRateLimit = new TokenBucket(thunk, thunk);
socket._inRateLimit = new TokenBucket(capacity, refillRate);
socket.on('cytube:count-event', () => {
if (socket._inRateLimit.throttle()) {
LOGGER.warn(
'Kicking client %s: exceeded in-rate-limit of %d',
socket.context.ipAddress,
thunk()
refillRate()
);
socket.emit('kick', { reason: 'Rate limit exceeded' });