mirror of https://github.com/zlatinb/muwire
message and per-sender throttles
parent
1f58ff6aba
commit
f2072be281
|
@ -0,0 +1,33 @@
|
|||
package com.muwire.core.util;
|
||||
|
||||
public class MessageThrottle {
|
||||
|
||||
private final long interval;
|
||||
private final int allowed;
|
||||
|
||||
private final long[] timestamps;
|
||||
private long lastMsg;
|
||||
private int idx;
|
||||
|
||||
public MessageThrottle(long interval, int allowed) {
|
||||
this.interval = interval;
|
||||
this.allowed = allowed;
|
||||
this.timestamps = new long[allowed];
|
||||
}
|
||||
|
||||
long lastMsg() {
|
||||
return lastMsg;
|
||||
}
|
||||
|
||||
public boolean allow(long now) {
|
||||
lastMsg = now;
|
||||
final long previous = timestamps[idx];
|
||||
if (previous == 0 || now - previous > interval) {
|
||||
timestamps[idx++] = now;
|
||||
if (idx == allowed)
|
||||
idx = 0;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,40 @@
|
|||
package com.muwire.core.util;
|
||||
|
||||
import com.muwire.core.Persona;
|
||||
import net.i2p.util.UIMessages;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
|
||||
public class SenderThrottle {
|
||||
private final long interval;
|
||||
private final int allowed;
|
||||
|
||||
private final Map<Persona, MessageThrottle> throttleMap =
|
||||
new HashMap<>();
|
||||
|
||||
public SenderThrottle(long interval, int allowed) {
|
||||
this.interval = interval;
|
||||
this.allowed = allowed;
|
||||
}
|
||||
|
||||
public boolean allow(long now, Persona sender) {
|
||||
MessageThrottle throttle = throttleMap.computeIfAbsent(sender,
|
||||
p -> {return new MessageThrottle(interval, allowed);});
|
||||
return throttle.allow(now);
|
||||
}
|
||||
|
||||
public int clear(long now) {
|
||||
int rv = 0;
|
||||
for (Iterator<Map.Entry<Persona, MessageThrottle>> iterator = throttleMap.entrySet().iterator();
|
||||
iterator.hasNext();) {
|
||||
Map.Entry<Persona, MessageThrottle> entry = iterator.next();
|
||||
if (now - entry.getValue().lastMsg() > interval) {
|
||||
iterator.remove();
|
||||
rv++;
|
||||
}
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,27 @@
|
|||
package com.muwire.core.util
|
||||
|
||||
import org.junit.Test
|
||||
|
||||
class MessageThrottleTest {
|
||||
|
||||
@Test
|
||||
public void testThrottleOne() {
|
||||
def throttle = new MessageThrottle(100, 1)
|
||||
assert throttle.allow(1)
|
||||
assert !throttle.allow(2)
|
||||
assert !throttle.allow(10)
|
||||
assert throttle.allow(102)
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAllowThree() {
|
||||
def throttle = new MessageThrottle(100, 3)
|
||||
assert throttle.allow(1)
|
||||
assert throttle.allow(2)
|
||||
assert throttle.allow(3)
|
||||
|
||||
assert !throttle.allow(101)
|
||||
|
||||
assert throttle.allow(102)
|
||||
}
|
||||
}
|
|
@ -0,0 +1,32 @@
|
|||
package com.muwire.core.util
|
||||
|
||||
import com.muwire.core.Personas
|
||||
import org.junit.Test
|
||||
|
||||
class SenderThrottleTest {
|
||||
|
||||
|
||||
private static Personas personas = new Personas()
|
||||
|
||||
@Test
|
||||
public void testSenders() {
|
||||
def throttle = new SenderThrottle(100, 1)
|
||||
assert throttle.allow(1, personas.persona1)
|
||||
assert throttle.allow(1, personas.persona2)
|
||||
|
||||
assert !throttle.allow(2, personas.persona1)
|
||||
assert !throttle.allow(2, personas.persona2)
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testClear() {
|
||||
def throttle = new SenderThrottle(100, 1)
|
||||
|
||||
assert throttle.clear(1) == 0
|
||||
|
||||
throttle.allow(1, personas.persona1)
|
||||
|
||||
assert throttle.clear(1) == 0
|
||||
assert throttle.clear( 102) == 1
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue