// }
int payLoadSize = 0;
Payload p = new Payload(m == null ? heartBeat : m);
if (_windowPolling) {
windowPolling = true;
_windowPolling = false;
} else if (windowPolling) {
// try {
// if (lock.tryAcquire(0, TimeUnit.SECONDS)) {
while (!queue.isEmpty() && payLoadSize < MAXIMUM_PAYLOAD_SIZE
&& !isWindowExceeded()) {
p.addMessage(queue.poll());
payLoadSize++;
try {
if (queue.isEmpty())
Thread.sleep(currentTimeMillis() - endWindow);
}
catch (Exception e) {
// just resume.
}
}
// }
// }
// finally {
// lock.release();
// }
if (!throttleIncoming && queue.size() > lastQueueSize) {
if (transmissionWindow < MAX_TRANSMISSION_WINDOW) {
transmissionWindow += 5;
System.err.println("[Congestion on queue -- New transmission window: "
+ transmissionWindow + "; Queue size: " + queue.size() + ")]");
} else {
throttleIncoming = true;
System.err.println("[Warning: A queue has become saturated and " +
"performance is now being degraded.]");
}
} else if (queue.isEmpty()) {
transmissionWindow = DEFAULT_TRANSMISSION_WINDOW;
throttleIncoming = false;
}
}
lastQueueSize = queue.size();
endWindow = (lastTransmission = currentTimeMillis()) + transmissionWindow;
return p;
}
catch (InterruptedException e) {
e.printStackTrace();
}
finally {
lock.release();
}
}
return new Payload(heartBeat);
}