From 8cbc3fcf1aa9276b61ec5a8f5c6959a3f7b86815 Mon Sep 17 00:00:00 2001 From: Andrzej Kapolka Date: Sun, 27 Sep 2009 02:23:17 +0000 Subject: [PATCH] Throttle datagrams as well as streamed messages. git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@5974 542714f4-19e9-0310-aa3c-eee0fc999fb1 --- .../presents/client/BlockingCommunicator.java | 29 ++++++++++++++----- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/src/java/com/threerings/presents/client/BlockingCommunicator.java b/src/java/com/threerings/presents/client/BlockingCommunicator.java index f29a0e40b..a7da66e72 100644 --- a/src/java/com/threerings/presents/client/BlockingCommunicator.java +++ b/src/java/com/threerings/presents/client/BlockingCommunicator.java @@ -40,6 +40,7 @@ import java.nio.channels.SocketChannel; import com.samskivert.util.LoopingThread; import com.samskivert.util.Queue; import com.samskivert.util.StringUtil; +import com.samskivert.util.Throttle; import com.threerings.io.ByteBufferInputStream; import com.threerings.io.ByteBufferOutputStream; @@ -526,6 +527,23 @@ public class BlockingCommunicator extends Communicator return false; } + /** + * Throttles an outgoing message operation in a thread-safe manner. + */ + protected void throttleOutgoingMessage () + { + Throttle throttle = _client.getOutgoingMessageThrottle(); + synchronized(throttle) { + while (throttle.throttleOp()) { + try { + Thread.sleep(2); + } catch (InterruptedException ie) { + // no problem + } + } + } + } + /** * The reader encapsulates the authentication and message reading process. It calls back to the * {@link Communicator} class to do things, but the general flow of the reader thread is @@ -681,13 +699,7 @@ public class BlockingCommunicator extends Communicator } // make sure we're not exceeding our outgoing throttle rate - while (_client.getOutgoingMessageThrottle().throttleOp()) { - try { - Thread.sleep(2); - } catch (InterruptedException ie) { - // no problem - } - } + throttleOutgoingMessage(); try { // write the message out the socket @@ -892,6 +904,9 @@ public class BlockingCommunicator extends Communicator return; } + // make sure we're not exceeding our outgoing throttle rate + throttleOutgoingMessage(); + try { // write the message out the socket sendDatagram(msg);