private void getSessionInternal()

in httpcore5/src/main/java/org/apache/hc/core5/reactor/AbstractIOSessionPool.java [167:249]


    private void getSessionInternal(
            final PoolEntry poolEntry,
            final boolean requestNew,
            final T namedEndpoint,
            final Timeout connectTimeout,
            final FutureCallback<IOSession> callback) {
        poolEntry.lock.lock();
        try {
            if (poolEntry.session != null && requestNew) {
                closeSession(poolEntry.session, CloseMode.GRACEFUL);
                poolEntry.session = null;
            }
            if (poolEntry.session != null && !poolEntry.session.isOpen()) {
                poolEntry.session = null;
            }
            if (poolEntry.session != null) {
                callback.completed(poolEntry.session);
            } else {
                poolEntry.requestQueue.add(callback);
                if (poolEntry.sessionFuture != null && poolEntry.completed) {
                    poolEntry.sessionFuture = null;
                }
                if (poolEntry.sessionFuture == null) {
                    poolEntry.completed = false;
                    poolEntry.sessionFuture = connectSession(
                            namedEndpoint,
                            connectTimeout,
                            new FutureCallback<IOSession>() {

                                @Override
                                public void completed(final IOSession result) {
                                    poolEntry.lock.lock();
                                    try {
                                        poolEntry.completed = true;
                                        if (poolEntry.session == null) {
                                            poolEntry.session = result;
                                        } else {
                                            closeSession(result,CloseMode.GRACEFUL);
                                        }
                                        for (;;) {
                                            final FutureCallback<IOSession> callback = poolEntry.requestQueue.poll();
                                            if (callback != null) {
                                                callback.completed(result);
                                            } else {
                                                break;
                                            }
                                        }
                                    } finally {
                                        poolEntry.lock.unlock();
                                    }
                                }

                                @Override
                                public void failed(final Exception ex) {
                                    poolEntry.lock.lock();
                                    try {
                                        poolEntry.completed = true;
                                        poolEntry.session = null;
                                        for (;;) {
                                            final FutureCallback<IOSession> callback = poolEntry.requestQueue.poll();
                                            if (callback != null) {
                                                callback.failed(ex);
                                            } else {
                                                break;
                                            }
                                        }
                                    } finally {
                                        poolEntry.lock.unlock();
                                    }
                                }

                                @Override
                                public void cancelled() {
                                    failed(new ConnectionClosedException("Connection request cancelled"));
                                }

                            });
                }
            }
        } finally {
            poolEntry.lock.unlock();
        }
    }