+ /**
+ * Send a request to the backend and invoke a specified callback when it finishes. This method is safe to invoke
+ * from any thread.
+ *
+ * <p>This method may put the caller thread to sleep in order to throttle the request rate.
+ * The callback may be called before the sleep finishes.
+ *
+ * @param request Request to send
+ * @param callback Callback to invoke
+ */
+ public final void sendRequest(final Request<?, ?> request, final Consumer<Response<?, ?>> callback) {
+ final long now = currentTime();
+ final long delay = enqueueEntry(new ConnectionEntry(request, callback, now), now);
+ try {
+ TimeUnit.NANOSECONDS.sleep(delay);
+ } catch (InterruptedException e) {
+ Thread.currentThread().interrupt();
+ LOG.debug("Interrupted after sleeping {}ns", e, currentTime() - now);
+ }