private Releasable acquirePermits()

in src/main/java/org/opensearch/search/asynchronous/context/permits/AsynchronousSearchContextPermits.java [58:86]


    private Releasable acquirePermits(int permits, TimeValue timeout, final String details) throws AsynchronousSearchContextClosedException,
            TimeoutException {
        RunOnce release = new RunOnce(() -> {});
        if (closed) {
            logger.debug("Trying to acquire permit for closed context [{}]", asynchronousSearchContextId);
            throw new AsynchronousSearchContextClosedException(asynchronousSearchContextId);
        }
        try {
            if (semaphore.tryAcquire(permits, timeout.getMillis(), TimeUnit.MILLISECONDS)) {
                this.lockDetails = details;
                release = new RunOnce(() -> {
                    logger.debug("Releasing permit(s) [{}] with reason [{}]", permits, lockDetails);
                    semaphore.release(permits);});
                if (closed) {
                    release.run();
                    logger.debug("Trying to acquire permit for closed context [{}]", asynchronousSearchContextId);
                    throw new AsynchronousSearchContextClosedException( asynchronousSearchContextId);
                }
                return release::run;
            } else {
                throw new TimeoutException("obtaining context lock" + asynchronousSearchContextId + "timed out after " +
                        timeout.getMillis() + "ms, previous lock details: [" + lockDetails + "] trying to lock for [" + details + "]");
            }
        } catch (InterruptedException e ) {
            Thread.currentThread().interrupt();
            release.run();
            throw new RuntimeException("thread interrupted while trying to obtain context lock", e);
        }
    }