public Object bulkhead()

in safeguard-impl/src/main/java/org/apache/safeguard/impl/bulkhead/BulkheadInterceptor.java [62:101]


    public Object bulkhead(final InvocationContext context) throws Exception {
        final Map<Key, Model> models = cache.getModels();
        final Key key = new Key(context, cache.getUnwrappedCache().getUnwrappedCache());
        Model model = models.get(key);
        if (model == null) {
            model = cache.create(context);
            final Model existing = models.putIfAbsent(key, model);
            if (existing != null) {
                model = existing;
            } else {
                cache.postCreate(model, context);
            }
        }
        if (model.disabled) {
            return context.proceed();
        }
        if (model.useThreads) {
            final Map<String, Object> data = context.getContextData();
            final Object id = data.get(IdGeneratorInterceptor.class.getName());
            data.put(BulkheadInterceptor.class.getName() + ".model_" + id, model);
            data.put(BulkheadInterceptor.class.getName() + "_" + id, model.pool);
            data.put(Asynchronous.class.getName() + ".skip_" + id, Boolean.TRUE);
            return around(context);
        } else {
            if (!model.semaphore.tryAcquire()) {
                model.callsRejected.inc();
                throw new BulkheadException("No more permission available");
            }
            model.callsAccepted.inc();
            model.concurrentCalls.incrementAndGet();
            final long start = System.nanoTime();
            try {
                return context.proceed();
            } finally {
                model.executionDuration.update(System.nanoTime() - start);
                model.semaphore.release();
                model.concurrentCalls.decrementAndGet();
            }
        }
    }