long cleanup(long now){
  int inUseConnectionCount=0;
  int idleConnectionCount=0;
  RealConnection longestIdleConnection=null;
  long longestIdleDurationNs=Long.MIN_VALUE;
synchronized (this) {
    for (Iterator<RealConnection> i=connections.iterator(); i.hasNext(); ) {
      RealConnection connection=i.next();
      if (pruneAndGetAllocationCount(connection,now) > 0) {
        inUseConnectionCount++;
        continue;
      }
      idleConnectionCount++;
      long idleDurationNs=now - connection.idleAtNanos;
      if (idleDurationNs > longestIdleDurationNs) {
        longestIdleDurationNs=idleDurationNs;
        longestIdleConnection=connection;
      }
    }
    if (longestIdleDurationNs >= this.keepAliveDurationNs || idleConnectionCount > this.maxIdleConnections) {
      connections.remove(longestIdleConnection);
    }
 else     if (idleConnectionCount > 0) {
      return keepAliveDurationNs - longestIdleDurationNs;
    }
 else     if (inUseConnectionCount > 0) {
      return keepAliveDurationNs;
    }
 else {
      return -1;
    }
  }
  Util.closeQuietly(longestIdleConnection.getSocket());
  return 0;
}

long cleanup ( long now ) { int inUseConnectionCount = 0 ; int idleConnectionCount = 0 ; RealConnection longestIdleConnection = null ; long longestIdleDurationNs = Long . MIN_VALUE ; synchronized ( this ) { for ( Iterator < RealConnection > i = connections . iterator ( ) ; i . hasNext ( ) ; ) { RealConnection connection = i . next ( ) ; if ( pruneAndGetAllocationCount ( connection , now ) > 0 ) { inUseConnectionCount ++ ; continue ; } idleConnectionCount ++ ; long idleDurationNs = now - connection . idleAtNanos ; if ( idleDurationNs > longestIdleDurationNs ) { longestIdleDurationNs = idleDurationNs ; longestIdleConnection = connection ; } } if ( longestIdleDurationNs >= this . keepAliveDurationNs || idleConnectionCount > this . maxIdleConnections ) { connections . remove ( longestIdleConnection ) ; } else if ( idleConnectionCount > 0 ) { return keepAliveDurationNs - longestIdleDurationNs ; } else if ( inUseConnectionCount > 0 ) { return keepAliveDurationNs ; } else { return - 1 ; } } Util . closeQuietly ( longestIdleConnection . getSocket ( ) ) ; return 0 ; }
performs maintenance on this pool , evicting the connection that has been idle the longest if either it has exceeded the keep alive limit or the idle connections limit . < p > returns the duration in nanos to sleep until the next scheduled call to this method . returns - 1 if no further cleanups are required .
