public HttpCodec newCodec(OkHttpClient client, Interceptor.Chain chain, StreamAllocation streamAllocation) throws SocketException { if (http2Connection != null) { return new Http2Codec(client, chain, streamAllocation, http2Connection); } else { socket.setSoTimeout(chain.readTimeoutMillis()); source.timeout().timeout(chain.readTimeoutMillis(), MILLISECONDS); sink.timeout().timeout(chain.writeTimeoutMillis(), MILLISECONDS); return new Http1Codec(client, streamAllocation, source, sink); } }
/** * Reads until {@code in} is exhausted or the deadline has been reached. This is careful to not * extend the deadline if one exists already. */ public static boolean skipAll(Source source, int duration, TimeUnit timeUnit) throws IOException { long now = System.nanoTime(); long originalDuration = source.timeout().hasDeadline() ? source.timeout().deadlineNanoTime() - now : Long.MAX_VALUE; source.timeout().deadlineNanoTime(now + Math.min(originalDuration, timeUnit.toNanos(duration))); try { Buffer skipBuffer = new Buffer(); while (source.read(skipBuffer, 8192) != -1) { skipBuffer.clear(); } return true; // Success! The source has been exhausted. } catch (InterruptedIOException e) { return false; // We ran out of time before exhausting the source. } finally { if (originalDuration == Long.MAX_VALUE) { source.timeout().clearDeadline(); } else { source.timeout().deadlineNanoTime(now + originalDuration); } } }
long timeoutBefore = source.timeout().timeoutNanos(); source.timeout().clearTimeout(); try { b0 = source.readByte() & 0xff; } finally { source.timeout().timeout(timeoutBefore, TimeUnit.NANOSECONDS);
/** * Sets the delegate of {@code timeout} to {@link Timeout#NONE} and resets its underlying timeout * to the default configuration. Use this to avoid unexpected sharing of timeouts between pooled * connections. */ void detachTimeout(ForwardingTimeout timeout) { Timeout oldDelegate = timeout.delegate(); timeout.setDelegate(Timeout.NONE); oldDelegate.clearDeadline(); oldDelegate.clearTimeout(); }
@Test public synchronized void deadlineBeforeTimeout() { Timeout timeout = new Timeout(); timeout.timeout(5000, TimeUnit.MILLISECONDS); timeout.deadline(1000, TimeUnit.MILLISECONDS); double start = now(); try { timeout.waitUntilNotified(this); fail(); } catch (InterruptedIOException expected) { assertEquals("timeout", expected.getMessage()); } assertElapsed(1000.0, start); }
@Test public synchronized void notified() throws InterruptedIOException { Timeout timeout = new Timeout(); timeout.timeout(5000, TimeUnit.MILLISECONDS); double start = now(); executorService.schedule(new Runnable() { @Override public void run() { synchronized (WaitUntilNotifiedTest.this) { WaitUntilNotifiedTest.this.notify(); } } }, 1000, TimeUnit.MILLISECONDS); timeout.waitUntilNotified(this); assertElapsed(1000.0, start); }
@Test public synchronized void deadlineAlreadyReached() { Timeout timeout = new Timeout(); timeout.deadlineNanoTime(System.nanoTime()); double start = now(); try { timeout.waitUntilNotified(this); fail(); } catch (InterruptedIOException expected) { assertEquals("timeout", expected.getMessage()); } assertElapsed(0.0, start); }
public void connect(OkHttpClient client) { client = client.newBuilder() .eventListener(EventListener.NONE) .build(); call = client.newCall(request); call.timeout().clearTimeout(); call.enqueue(this); }
var t = new Timeout(function () { alert('this is a test'); }, 5000); console.log(t.cleared); // false t.clear(); console.log(t.cleared); // true
@Override public long deadlineNanoTime() { return delegate.deadlineNanoTime(); }
/** Waits for an OAuth session for this client to be set. */ public synchronized void awaitAccessToken(Timeout timeout) throws InterruptedIOException { while (session == null) { timeout.waitUntilNotified(this); } }
@Test public synchronized void deadline() { Timeout timeout = new Timeout(); timeout.deadline(1000, TimeUnit.MILLISECONDS); double start = now(); try { timeout.waitUntilNotified(this); fail(); } catch (InterruptedIOException expected) { assertEquals("timeout", expected.getMessage()); } assertElapsed(1000.0, start); }
@Test public synchronized void threadInterrupted() { Timeout timeout = new Timeout(); double start = now(); Thread.currentThread().interrupt(); try { timeout.waitUntilNotified(this); fail(); } catch (InterruptedIOException expected) { assertEquals("interrupted", expected.getMessage()); assertTrue(Thread.interrupted()); } assertElapsed(0.0, start); }
@Test public void getAndSetDelegate() { Timeout timeout1 = new Timeout(); Timeout timeout2 = new Timeout(); ForwardingTimeout forwardingTimeout = new ForwardingTimeout(timeout1); forwardingTimeout.timeout(5, TimeUnit.SECONDS); assertThat(timeout1.timeoutNanos()).isNotEqualTo(0L); assertThat(timeout2.timeoutNanos()).isEqualTo(0L); forwardingTimeout.clearTimeout(); assertThat(timeout1.timeoutNanos()).isEqualTo(0L); assertThat(timeout2.timeoutNanos()).isEqualTo(0L); assertThat(forwardingTimeout.delegate()).isEqualTo(timeout1); assertThat(forwardingTimeout.setDelegate(timeout2)).isEqualTo(forwardingTimeout); forwardingTimeout.timeout(5, TimeUnit.SECONDS); assertThat(timeout1.timeoutNanos()).isEqualTo(0L); assertThat(timeout2.timeoutNanos()).isNotEqualTo(0L); forwardingTimeout.clearTimeout(); assertThat(timeout1.timeoutNanos()).isEqualTo(0L); assertThat(timeout2.timeoutNanos()).isEqualTo(0L); assertThat(forwardingTimeout.delegate()).isEqualTo(timeout2); } }
@Override public boolean hasDeadline() { return delegate.hasDeadline(); }
@Override public Timeout clearDeadline() { return delegate.clearDeadline(); }
@Test public synchronized void threadInterruptedOnThrowIfReached() throws Exception { Timeout timeout = new Timeout(); Thread.currentThread().interrupt(); try { timeout.throwIfReached(); fail(); } catch (InterruptedIOException expected) { assertEquals("interrupted", expected.getMessage()); assertTrue(Thread.interrupted()); } }