Let our peers send up to 100 messages a second after which point we will

politely complain that they're talking a lot but not disconnect them.


git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@4871 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
Michael Bayne
2007-11-12 21:29:57 +00:00
parent ae6e8f1bdb
commit f471da8a49
2 changed files with 44 additions and 7 deletions
@@ -21,6 +21,8 @@
package com.threerings.presents.peer.server;
import com.samskivert.util.Throttle;
import com.threerings.presents.dobj.DObject;
import com.threerings.presents.net.BootstrapData;
import com.threerings.presents.server.PresentsClient;
@@ -28,6 +30,8 @@ import com.threerings.presents.server.PresentsClient;
import com.threerings.presents.peer.data.NodeObject;
import com.threerings.presents.peer.net.PeerBootstrapData;
import static com.threerings.presents.Log.log;
/**
* Manages a peer connection.
*/
@@ -98,6 +102,24 @@ public class PeerClient extends PresentsClient
}
}
@Override // from PresentsClient
protected Throttle createIncomingMessageThrottle ()
{
// more than 100 messages per second and we complain about it
return new Throttle(100, 1000L);
}
@Override // from PresentsClient
protected void handleThrottleExceeded ()
{
long now = System.currentTimeMillis();
if (now > _nextThrottleWarning) {
log.warning("Peer sent more than 100 messages in one second " + this + ".");
_nextThrottleWarning = now + 5000L; // don't warn more than once every 5 seconds
}
}
protected PeerManager _peermgr;
protected int _cloid;
protected long _nextThrottleWarning;
}
@@ -360,10 +360,7 @@ public class PresentsClient
return;
} else if (_throttle.throttleOp(message.received)) {
Log.warning("Client sent more than 100 messages in 10 seconds, forcing " +
"disconnect " + this + ".");
safeEndSession();
_throttle = null;
handleThrottleExceeded();
}
// we dispatch to a message dispatcher that is specialized for the particular class of
@@ -502,6 +499,25 @@ public class PresentsClient
});
}
/**
* Creates our incoming messaeg throttle.
*/
protected Throttle createIncomingMessageThrottle ()
{
// more than 100 messages in 10 seconds and you're audi like 5000
return new Throttle(100, 10 * 1000L);
}
/**
* Called when a client exceeds their allotted incoming messages per second throttle.
*/
protected void handleThrottleExceeded ()
{
Log.warning("Client sent more than 100 messages in 10 seconds, disconnecting " + this + ".");
safeEndSession();
_throttle = null;
}
/**
* Makes a note that this client is no longer subscribed to this object. The subscription map
* is used to clean up after the client when it goes away.
@@ -970,9 +986,8 @@ public class PresentsClient
* session. */
protected int _connectTime;
/** Prevent the client from sending too many messages too frequently. 100 messages in 10
* seconds and you're audi like 5000. */
protected Throttle _throttle = new Throttle(100, 10 * 1000L);
/** Prevent the client from sending too many messages too frequently. */
protected Throttle _throttle = createIncomingMessageThrottle();
// keep these for kicks and giggles
protected int _messagesIn;