class BulkheadImpl<R> implements Bulkhead<R> {
  private static final CompletableFuture<Void> NULL_FUTURE = CompletableFuture.completedFuture(null);
  private final BulkheadConfig<R> config;
  private final int maxPermits;

  // Mutable state
  private int permits;
  private final FutureLinkedList futures = new FutureLinkedList();

  public BulkheadImpl(BulkheadConfig<R> config) {
    this.config = config;
    maxPermits = config.getMaxConcurrency();
    permits = maxPermits;
  }

  @Override
  public BulkheadConfig<R> getConfig() {
    return config;
  }

  @Override
  public void acquirePermit() throws InterruptedException {
    try {
      acquirePermitAsync().get();
    } catch (CancellationException | ExecutionException ignore) {
      // Not possible since the future will always be completed with null
    }
  }

  @Override
  public synchronized boolean tryAcquirePermit() {
    if (permits > 0) {
      permits -= 1;
      return true;
    }
    return false;
  }

  @Override
  public boolean tryAcquirePermit(Duration maxWaitTime) throws InterruptedException {<FILL_FUNCTION_BODY>}

  /**
   * Returns a CompletableFuture that is completed when a permit is acquired. Externally completing this future will
   * remove the waiter from the bulkhead's internal queue.
   */
  synchronized CompletableFuture<Void> acquirePermitAsync() {
    if (permits > 0) {
      permits -= 1;
      return NULL_FUTURE;
    } else {
      return futures.add();
    }
  }

  @Override
  public synchronized void releasePermit() {
    if (permits < maxPermits) {
      permits += 1;
      CompletableFuture<Void> future = futures.pollFirst();
      if (future != null){
        permits -= 1;
        future.complete(null);
      }
    }
  }

  @Override
  public PolicyExecutor<R> toExecutor(int policyIndex) {
    return new BulkheadExecutor<>(this, policyIndex);
  }
}

    CompletableFuture<Void> future = acquirePermitAsync();
    if (future == NULL_FUTURE)
      return true;

    try {
      future.get(maxWaitTime.toNanos(), TimeUnit.NANOSECONDS);
      return true;
    } catch (CancellationException | ExecutionException | TimeoutException e) {
      return false;
    }
  
<no_super_class>