Called the first time we get a buffered increment. Lazily creates the increment buffer and sets up a timer to regularly flush buffered increments.
()
| 1777 | * flush buffered increments. |
| 1778 | */ |
| 1779 | private synchronized void setupIncrementCoalescing() { |
| 1780 | // If multiple threads attempt to setup coalescing at the same time, the |
| 1781 | // first one to get here will make `increment_buffer' non-null, and thus |
| 1782 | // subsequent ones will return immediately. This is important to avoid |
| 1783 | // creating more than one FlushBufferedIncrementsTimer below. |
| 1784 | if (increment_buffer != null) { |
| 1785 | return; |
| 1786 | } |
| 1787 | makeIncrementBuffer(); // Volatile-write. |
| 1788 | |
| 1789 | // Start periodic buffered increment flushes. |
| 1790 | final class FlushBufferedIncrementsTimer implements TimerTask { |
| 1791 | public void run(final Timeout timeout) { |
| 1792 | try { |
| 1793 | flushBufferedIncrements(increment_buffer); |
| 1794 | } finally { |
| 1795 | final short interval = flush_interval; // Volatile-read. |
| 1796 | // Even if we paused or disabled the client side buffer by calling |
| 1797 | // setFlushInterval(0), we will continue to schedule this timer |
| 1798 | // forever instead of pausing it. Pausing it is troublesome because |
| 1799 | // we don't keep a reference to this timer, so we can't cancel it or |
| 1800 | // tell if it's running or not. So let's just KISS and assume that |
| 1801 | // if we need the timer once, we'll need it forever. If it's truly |
| 1802 | // not needed anymore, we'll just cause a bit of extra work to the |
| 1803 | // timer thread every 100ms, no big deal. |
| 1804 | newTimeout(this, interval > 0 ? interval : 100); |
| 1805 | } |
| 1806 | } |
| 1807 | } |
| 1808 | |
| 1809 | final short interval = flush_interval; // Volatile-read. |
| 1810 | // Handle the extremely unlikely yet possible racy case where: |
| 1811 | // flush_interval was > 0 |
| 1812 | // A buffered increment came in |
| 1813 | // It was the first one ever so we landed here |
| 1814 | // Meanwhile setFlushInterval(0) to disable buffering |
| 1815 | // In which case we just flush whatever we have in 1ms. |
| 1816 | timer.newTimeout(new FlushBufferedIncrementsTimer(), |
| 1817 | interval > 0 ? interval : 1, MILLISECONDS); |
| 1818 | } |
| 1819 | |
| 1820 | /** |
| 1821 | * Called the first time we get a buffered increment. |
no test coverage detected