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:
@@ -21,6 +21,8 @@
|
|||||||
|
|
||||||
package com.threerings.presents.peer.server;
|
package com.threerings.presents.peer.server;
|
||||||
|
|
||||||
|
import com.samskivert.util.Throttle;
|
||||||
|
|
||||||
import com.threerings.presents.dobj.DObject;
|
import com.threerings.presents.dobj.DObject;
|
||||||
import com.threerings.presents.net.BootstrapData;
|
import com.threerings.presents.net.BootstrapData;
|
||||||
import com.threerings.presents.server.PresentsClient;
|
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.data.NodeObject;
|
||||||
import com.threerings.presents.peer.net.PeerBootstrapData;
|
import com.threerings.presents.peer.net.PeerBootstrapData;
|
||||||
|
|
||||||
|
import static com.threerings.presents.Log.log;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Manages a peer connection.
|
* 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 PeerManager _peermgr;
|
||||||
protected int _cloid;
|
protected int _cloid;
|
||||||
|
protected long _nextThrottleWarning;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -360,10 +360,7 @@ public class PresentsClient
|
|||||||
return;
|
return;
|
||||||
|
|
||||||
} else if (_throttle.throttleOp(message.received)) {
|
} else if (_throttle.throttleOp(message.received)) {
|
||||||
Log.warning("Client sent more than 100 messages in 10 seconds, forcing " +
|
handleThrottleExceeded();
|
||||||
"disconnect " + this + ".");
|
|
||||||
safeEndSession();
|
|
||||||
_throttle = null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// we dispatch to a message dispatcher that is specialized for the particular class of
|
// 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
|
* 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.
|
* is used to clean up after the client when it goes away.
|
||||||
@@ -970,9 +986,8 @@ public class PresentsClient
|
|||||||
* session. */
|
* session. */
|
||||||
protected int _connectTime;
|
protected int _connectTime;
|
||||||
|
|
||||||
/** Prevent the client from sending too many messages too frequently. 100 messages in 10
|
/** Prevent the client from sending too many messages too frequently. */
|
||||||
* seconds and you're audi like 5000. */
|
protected Throttle _throttle = createIncomingMessageThrottle();
|
||||||
protected Throttle _throttle = new Throttle(100, 10 * 1000L);
|
|
||||||
|
|
||||||
// keep these for kicks and giggles
|
// keep these for kicks and giggles
|
||||||
protected int _messagesIn;
|
protected int _messagesIn;
|
||||||
|
|||||||
Reference in New Issue
Block a user