当前位置: 首页>>代码示例>>Java>>正文


Java RealConnection类代码示例

本文整理汇总了Java中okhttp3.internal.connection.RealConnection的典型用法代码示例。如果您正苦于以下问题:Java RealConnection类的具体用法?Java RealConnection怎么用?Java RealConnection使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。


RealConnection类属于okhttp3.internal.connection包,在下文中一共展示了RealConnection类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: inUseConnectionsNotEvicted

import okhttp3.internal.connection.RealConnection; //导入依赖的package包/类
@Test public void inUseConnectionsNotEvicted() throws Exception {
  ConnectionPool pool = new ConnectionPool(Integer.MAX_VALUE, 100L, TimeUnit.NANOSECONDS);
  pool.cleanupRunning = true; // Prevent the cleanup runnable from being started.

  RealConnection c1 = newConnection(pool, routeA1, 50L);
  synchronized (pool) {
    StreamAllocation streamAllocation = new StreamAllocation(pool, addressA, null);
    streamAllocation.acquire(c1);
  }

  // Running at time 50, the pool returns that nothing can be evicted until time 150.
  assertEquals(100L, pool.cleanup(50L));
  assertEquals(1, pool.connectionCount());
  assertFalse(c1.socket().isClosed());

  // Running at time 60, the pool returns that nothing can be evicted until time 160.
  assertEquals(100L, pool.cleanup(60L));
  assertEquals(1, pool.connectionCount());
  assertFalse(c1.socket().isClosed());

  // Running at time 160, the pool returns that nothing can be evicted until time 260.
  assertEquals(100L, pool.cleanup(160L));
  assertEquals(1, pool.connectionCount());
  assertFalse(c1.socket().isClosed());
}
 
开发者ID:weiwenqiang,项目名称:GitHub,代码行数:26,代码来源:ConnectionPoolTest.java

示例2: oldestConnectionsEvictedIfIdleLimitExceeded

import okhttp3.internal.connection.RealConnection; //导入依赖的package包/类
@Test public void oldestConnectionsEvictedIfIdleLimitExceeded() throws Exception {
  ConnectionPool pool = new ConnectionPool(2, 100L, TimeUnit.NANOSECONDS);
  pool.cleanupRunning = true; // Prevent the cleanup runnable from being started.

  RealConnection c1 = newConnection(pool, routeA1, 50L);
  RealConnection c2 = newConnection(pool, routeB1, 75L);

  // With 2 connections, there's no need to evict until the connections time out.
  assertEquals(50L, pool.cleanup(100L));
  assertEquals(2, pool.connectionCount());
  assertFalse(c1.socket().isClosed());
  assertFalse(c2.socket().isClosed());

  // Add a third connection
  RealConnection c3 = newConnection(pool, routeC1, 75L);

  // The third connection bounces the first.
  assertEquals(0L, pool.cleanup(100L));
  assertEquals(2, pool.connectionCount());
  assertTrue(c1.socket().isClosed());
  assertFalse(c2.socket().isClosed());
  assertFalse(c3.socket().isClosed());
}
 
开发者ID:weiwenqiang,项目名称:GitHub,代码行数:24,代码来源:ConnectionPoolTest.java

示例3: inUseConnectionsNotEvicted

import okhttp3.internal.connection.RealConnection; //导入依赖的package包/类
@Test public void inUseConnectionsNotEvicted() throws Exception {
  ConnectionPool pool = new ConnectionPool(Integer.MAX_VALUE, 100L, TimeUnit.NANOSECONDS);
  pool.cleanupRunning = true; // Prevent the cleanup runnable from being started.

  RealConnection c1 = newConnection(pool, routeA1, 50L);
  synchronized (pool) {
    StreamAllocation streamAllocation = new StreamAllocation(pool, addressA, null,
        EventListener.NONE, null);
    streamAllocation.acquire(c1, true);
  }

  // Running at time 50, the pool returns that nothing can be evicted until time 150.
  assertEquals(100L, pool.cleanup(50L));
  assertEquals(1, pool.connectionCount());
  assertFalse(c1.socket().isClosed());

  // Running at time 60, the pool returns that nothing can be evicted until time 160.
  assertEquals(100L, pool.cleanup(60L));
  assertEquals(1, pool.connectionCount());
  assertFalse(c1.socket().isClosed());

  // Running at time 160, the pool returns that nothing can be evicted until time 260.
  assertEquals(100L, pool.cleanup(160L));
  assertEquals(1, pool.connectionCount());
  assertFalse(c1.socket().isClosed());
}
 
开发者ID:weiwenqiang,项目名称:GitHub,代码行数:27,代码来源:ConnectionPoolTest.java

示例4: pruneAndGetAllocationCount

import okhttp3.internal.connection.RealConnection; //导入依赖的package包/类
/**
 * Prunes any leaked allocations and then returns the number of remaining live allocations on
 * {@code connection}. Allocations are leaked if the connection is tracking them but the
 * application code has abandoned them. Leak detection is imprecise and relies on garbage
 * collection.
 */
private int pruneAndGetAllocationCount(RealConnection connection, long now) {
  List<Reference<StreamAllocation>> references = connection.allocations;
  for (int i = 0; i < references.size(); ) {
    Reference<StreamAllocation> reference = references.get(i);

    if (reference.get() != null) {
      i++;
      continue;
    }

    // We've discovered a leaked allocation. This is an application bug.
    Platform.get().log(WARN, "A connection to " + connection.route().address().url()
        + " was leaked. Did you forget to close a response body?", null);
    references.remove(i);
    connection.noNewStreams = true;

    // If this was the last allocation, the connection is eligible for immediate eviction.
    if (references.isEmpty()) {
      connection.idleAtNanos = now - keepAliveDurationNs;
      return 0;
    }
  }

  return references.size();
}
 
开发者ID:RunningTheSnail,项目名称:Okhttp,代码行数:32,代码来源:ConnectionPool.java

示例5: inUseConnectionsNotEvicted

import okhttp3.internal.connection.RealConnection; //导入依赖的package包/类
@Test public void inUseConnectionsNotEvicted() throws Exception {
  ConnectionPool pool = new ConnectionPool(Integer.MAX_VALUE, 100L, TimeUnit.NANOSECONDS);
  pool.cleanupRunning = true; // Prevent the cleanup runnable from being started.

  RealConnection c1 = newConnection(pool, routeA1, 50L);
  StreamAllocation streamAllocation = new StreamAllocation(pool, addressA);
  streamAllocation.acquire(c1);

  // Running at time 50, the pool returns that nothing can be evicted until time 150.
  assertEquals(100L, pool.cleanup(50L));
  assertEquals(1, pool.connectionCount());
  assertFalse(c1.socket.isClosed());

  // Running at time 60, the pool returns that nothing can be evicted until time 160.
  assertEquals(100L, pool.cleanup(60L));
  assertEquals(1, pool.connectionCount());
  assertFalse(c1.socket.isClosed());

  // Running at time 160, the pool returns that nothing can be evicted until time 260.
  assertEquals(100L, pool.cleanup(160L));
  assertEquals(1, pool.connectionCount());
  assertFalse(c1.socket.isClosed());
}
 
开发者ID:RunningTheSnail,项目名称:Okhttp,代码行数:24,代码来源:ConnectionPoolTest.java

示例6: oldestConnectionsEvictedIfIdleLimitExceeded

import okhttp3.internal.connection.RealConnection; //导入依赖的package包/类
@Test public void oldestConnectionsEvictedIfIdleLimitExceeded() throws Exception {
  ConnectionPool pool = new ConnectionPool(2, 100L, TimeUnit.NANOSECONDS);
  pool.cleanupRunning = true; // Prevent the cleanup runnable from being started.

  RealConnection c1 = newConnection(pool, routeA1, 50L);
  RealConnection c2 = newConnection(pool, routeB1, 75L);

  // With 2 connections, there's no need to evict until the connections time out.
  assertEquals(50L, pool.cleanup(100L));
  assertEquals(2, pool.connectionCount());
  assertFalse(c1.socket.isClosed());
  assertFalse(c2.socket.isClosed());

  // Add a third connection
  RealConnection c3 = newConnection(pool, routeC1, 75L);

  // The third connection bounces the first.
  assertEquals(0L, pool.cleanup(100L));
  assertEquals(2, pool.connectionCount());
  assertTrue(c1.socket.isClosed());
  assertFalse(c2.socket.isClosed());
  assertFalse(c3.socket.isClosed());
}
 
开发者ID:RunningTheSnail,项目名称:Okhttp,代码行数:24,代码来源:ConnectionPoolTest.java

示例7: idleConnectionCount

import okhttp3.internal.connection.RealConnection; //导入依赖的package包/类
/** Returns the number of idle connections in the pool. */
public synchronized int idleConnectionCount() {
  int total = 0;
  for (RealConnection connection : connections) {
    if (connection.allocations.isEmpty()) total++;
  }
  return total;
}
 
开发者ID:weiwenqiang,项目名称:GitHub,代码行数:9,代码来源:ConnectionPool.java

示例8: get

import okhttp3.internal.connection.RealConnection; //导入依赖的package包/类
/**
 * Returns a recycled connection to {@code address}, or null if no such connection exists. The
 * route is null if the address has not yet been routed.
 */
RealConnection get(Address address, StreamAllocation streamAllocation, Route route) {
  assert (Thread.holdsLock(this));
  for (RealConnection connection : connections) {
    if (connection.isEligible(address, route)) {
      streamAllocation.acquire(connection);
      return connection;
    }
  }
  return null;
}
 
开发者ID:weiwenqiang,项目名称:GitHub,代码行数:15,代码来源:ConnectionPool.java

示例9: deduplicate

import okhttp3.internal.connection.RealConnection; //导入依赖的package包/类
/**
 * Replaces the connection held by {@code streamAllocation} with a shared connection if possible.
 * This recovers when multiple multiplexed connections are created concurrently.
 */
Socket deduplicate(Address address, StreamAllocation streamAllocation) {
  assert (Thread.holdsLock(this));
  for (RealConnection connection : connections) {
    if (connection.isEligible(address, null)
        && connection.isMultiplexed()
        && connection != streamAllocation.connection()) {
      return streamAllocation.releaseAndAcquire(connection);
    }
  }
  return null;
}
 
开发者ID:weiwenqiang,项目名称:GitHub,代码行数:16,代码来源:ConnectionPool.java

示例10: put

import okhttp3.internal.connection.RealConnection; //导入依赖的package包/类
void put(RealConnection connection) {
  assert (Thread.holdsLock(this));
  if (!cleanupRunning) {
    cleanupRunning = true;
    executor.execute(cleanupRunnable);
  }
  connections.add(connection);
}
 
开发者ID:weiwenqiang,项目名称:GitHub,代码行数:9,代码来源:ConnectionPool.java

示例11: connectionBecameIdle

import okhttp3.internal.connection.RealConnection; //导入依赖的package包/类
/**
 * Notify this pool that {@code connection} has become idle. Returns true if the connection has
 * been removed from the pool and should be closed.
 */
boolean connectionBecameIdle(RealConnection connection) {
  assert (Thread.holdsLock(this));
  if (connection.noNewStreams || maxIdleConnections == 0) {
    connections.remove(connection);
    return true;
  } else {
    notifyAll(); // Awake the cleanup thread: we may have exceeded the idle connection limit.
    return false;
  }
}
 
开发者ID:weiwenqiang,项目名称:GitHub,代码行数:15,代码来源:ConnectionPool.java

示例12: pruneAndGetAllocationCount

import okhttp3.internal.connection.RealConnection; //导入依赖的package包/类
/**
 * Prunes any leaked allocations and then returns the number of remaining live allocations on
 * {@code connection}. Allocations are leaked if the connection is tracking them but the
 * application code has abandoned them. Leak detection is imprecise and relies on garbage
 * collection.
 */
private int pruneAndGetAllocationCount(RealConnection connection, long now) {
  List<Reference<StreamAllocation>> references = connection.allocations;
  for (int i = 0; i < references.size(); ) {
    Reference<StreamAllocation> reference = references.get(i);

    if (reference.get() != null) {
      i++;
      continue;
    }

    // We've discovered a leaked allocation. This is an application bug.
    StreamAllocation.StreamAllocationReference streamAllocRef =
        (StreamAllocation.StreamAllocationReference) reference;
    String message = "A connection to " + connection.route().address().url()
        + " was leaked. Did you forget to close a response body?";
    Platform.get().logCloseableLeak(message, streamAllocRef.callStackTrace);

    references.remove(i);
    connection.noNewStreams = true;

    // If this was the last allocation, the connection is eligible for immediate eviction.
    if (references.isEmpty()) {
      connection.idleAtNanos = now - keepAliveDurationNs;
      return 0;
    }
  }

  return references.size();
}
 
开发者ID:weiwenqiang,项目名称:GitHub,代码行数:36,代码来源:ConnectionPool.java

示例13: RealInterceptorChain

import okhttp3.internal.connection.RealConnection; //导入依赖的package包/类
public RealInterceptorChain(List<Interceptor> interceptors, StreamAllocation streamAllocation,
    HttpCodec httpCodec, RealConnection connection, int index, Request request) {
  this.interceptors = interceptors;
  this.connection = connection;
  this.streamAllocation = streamAllocation;
  this.httpCodec = httpCodec;
  this.index = index;
  this.request = request;
}
 
开发者ID:weiwenqiang,项目名称:GitHub,代码行数:10,代码来源:RealInterceptorChain.java

示例14: proceed

import okhttp3.internal.connection.RealConnection; //导入依赖的package包/类
public Response proceed(Request request, StreamAllocation streamAllocation, HttpCodec httpCodec,
    RealConnection connection) throws IOException {
  if (index >= interceptors.size()) throw new AssertionError();

  calls++;

  // If we already have a stream, confirm that the incoming request will use it.
  if (this.httpCodec != null && !this.connection.supportsUrl(request.url())) {
    throw new IllegalStateException("network interceptor " + interceptors.get(index - 1)
        + " must retain the same host and port");
  }

  // If we already have a stream, confirm that this is the only call to chain.proceed().
  if (this.httpCodec != null && calls > 1) {
    throw new IllegalStateException("network interceptor " + interceptors.get(index - 1)
        + " must call proceed() exactly once");
  }

  // Call the next interceptor in the chain.
  RealInterceptorChain next = new RealInterceptorChain(
      interceptors, streamAllocation, httpCodec, connection, index + 1, request);
  Interceptor interceptor = interceptors.get(index);
  Response response = interceptor.intercept(next);

  // Confirm that the next interceptor made its required call to chain.proceed().
  if (httpCodec != null && index + 1 < interceptors.size() && next.calls != 1) {
    throw new IllegalStateException("network interceptor " + interceptor
        + " must call proceed() exactly once");
  }

  // Confirm that the intercepted response isn't null.
  if (response == null) {
    throw new NullPointerException("interceptor " + interceptor + " returned null");
  }

  return response;
}
 
开发者ID:weiwenqiang,项目名称:GitHub,代码行数:38,代码来源:RealInterceptorChain.java

示例15: connectionsEvictedWhenIdleLongEnough

import okhttp3.internal.connection.RealConnection; //导入依赖的package包/类
@Test public void connectionsEvictedWhenIdleLongEnough() throws Exception {
  ConnectionPool pool = new ConnectionPool(Integer.MAX_VALUE, 100L, TimeUnit.NANOSECONDS);
  pool.cleanupRunning = true; // Prevent the cleanup runnable from being started.

  RealConnection c1 = newConnection(pool, routeA1, 50L);

  // Running at time 50, the pool returns that nothing can be evicted until time 150.
  assertEquals(100L, pool.cleanup(50L));
  assertEquals(1, pool.connectionCount());
  assertFalse(c1.socket().isClosed());

  // Running at time 60, the pool returns that nothing can be evicted until time 150.
  assertEquals(90L, pool.cleanup(60L));
  assertEquals(1, pool.connectionCount());
  assertFalse(c1.socket().isClosed());

  // Running at time 149, the pool returns that nothing can be evicted until time 150.
  assertEquals(1L, pool.cleanup(149L));
  assertEquals(1, pool.connectionCount());
  assertFalse(c1.socket().isClosed());

  // Running at time 150, the pool evicts.
  assertEquals(0, pool.cleanup(150L));
  assertEquals(0, pool.connectionCount());
  assertTrue(c1.socket().isClosed());

  // Running again, the pool reports that no further runs are necessary.
  assertEquals(-1, pool.cleanup(150L));
  assertEquals(0, pool.connectionCount());
  assertTrue(c1.socket().isClosed());
}
 
开发者ID:weiwenqiang,项目名称:GitHub,代码行数:32,代码来源:ConnectionPoolTest.java


注:本文中的okhttp3.internal.connection.RealConnection类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。