long sendTxnWithOptionalBlockAndReturnCurrentTime(long timestampNanos, long timeoutNanos, boolean ignoreBackpressure) throws TimeoutException {
        if (m_doesAnyTuning) {
            long timestamp = TimeUnit.NANOSECONDS.toMillis(timestampNanos);
            while (true) {
                synchronized(this) {
                    // switch to a new block if 100ms has passed
                    // possibly compute a new target rate
                    ensureCurrentBlockIsKosher(timestamp);

                    assert((timestamp - m_currentBlockTimestamp) <= BLOCK_SIZE);

                    // don't let the time be before the start of the current block
                    // also ensure faketime - m_currentBlockTimestamp is positive
                    long faketime = timestamp < m_currentBlockTimestamp ? m_currentBlockTimestamp : timestamp;

                    long targetTxnsPerBlock = m_targetTxnsPerSecond / (1000 / BLOCK_SIZE);

                    // compute the percentage of the current 100ms block that has passed
                    double expectedTxnsSent =
                            targetTxnsPerBlock * (faketime - m_currentBlockTimestamp + 1.0) / BLOCK_SIZE;
                    expectedTxnsSent = Math.ceil(expectedTxnsSent);

                    assert(expectedTxnsSent <= targetTxnsPerBlock); // stupid fp math
                    assert((expectedTxnsSent >= 1.0) || (targetTxnsPerBlock == 0));

                    // if the rate is under target, no problems
                    if (((m_currentBlockSendCount < expectedTxnsSent) &&
                         (m_outstandingTxns < m_maxOutstandingTxns)) ||
                        (ignoreBackpressure == true)) {

                        // bookkeeping
                        ++m_currentBlockSendCount;
                        ++m_outstandingTxns;

                        // exit the while loop
                        break;
                    }
                }

                // if the rate is above target, pause for the smallest time possible
                try { Thread.sleep(1); } catch (InterruptedException e) {}
                timestampNanos = System.nanoTime();
                timestamp = TimeUnit.NANOSECONDS.toMillis(timestampNanos);
            }
        } else {
            if (ignoreBackpressure) return timestampNanos;
            boolean acquired = m_outstandingTxnsSemaphore.tryAcquire();

            if (!acquired) {
                try {
                    if (!m_outstandingTxnsSemaphore.tryAcquire(timeoutNanos, TimeUnit.NANOSECONDS)) {
                        throw new TimeoutException();
                    }
                } catch (InterruptedException e) {
                    Throwables.propagate(e);
                }
                return System.nanoTime();
            }
        }

        // this time may have changed if this call blocked during a sleep
        return timestampNanos;
    }
long sendTxnWithOptionalBlockAndReturnCurrentTime ( long timestampNanos , long timeoutNanos , boolean ignoreBackpressure ) throws TimeoutException { if ( m_doesAnyTuning ) { long timestamp = TimeUnit . NANOSECONDS . toMillis ( timestampNanos ) ; while ( true ) { synchronized ( this ) { ensureCurrentBlockIsKosher ( timestamp ) ; assert ( ( timestamp - m_currentBlockTimestamp ) <= BLOCK_SIZE ) ; long faketime = timestamp < m_currentBlockTimestamp ? m_currentBlockTimestamp : timestamp ; long targetTxnsPerBlock = m_targetTxnsPerSecond / ( 1000 / BLOCK_SIZE ) ; double expectedTxnsSent = targetTxnsPerBlock * ( faketime - m_currentBlockTimestamp + 1.0 ) / BLOCK_SIZE ; expectedTxnsSent = Math . ceil ( expectedTxnsSent ) ; assert ( expectedTxnsSent <= targetTxnsPerBlock ) ; assert ( ( expectedTxnsSent >= 1.0 ) || ( targetTxnsPerBlock == 0 ) ) ; if ( ( ( m_currentBlockSendCount < expectedTxnsSent ) && ( m_outstandingTxns < m_maxOutstandingTxns ) ) || ( ignoreBackpressure == true ) ) { ++ m_currentBlockSendCount ; ++ m_outstandingTxns ; break ; } } try { Thread . sleep ( 1 ) ; } catch ( InterruptedException e ) { } timestampNanos = System . nanoTime ( ) ; timestamp = TimeUnit . NANOSECONDS . toMillis ( timestampNanos ) ; } } else { if ( ignoreBackpressure ) return timestampNanos ; boolean acquired = m_outstandingTxnsSemaphore . tryAcquire ( ) ; if ( ! acquired ) { try { if ( ! m_outstandingTxnsSemaphore . tryAcquire ( timeoutNanos , TimeUnit . NANOSECONDS ) ) { throw new TimeoutException ( ) ; } } catch ( InterruptedException e ) { Throwables . propagate ( e ) ; } return System . nanoTime ( ) ; } } return timestampNanos ; }
@param timestamp The time as measured when the call is made.
@param ignoreBackpressure If true, never block.
@return The time as measured when the call returns.