in server/src/main/java/com/epam/aidial/core/server/controller/DeploymentPostController.java [93:138]
private Future<?> handleDeployment(String deploymentId, String deploymentApi) {
return DeploymentController.selectDeployment(context, deploymentId, false, true)
.map(dep -> {
if (dep.getEndpoint() == null) {
throw new HttpException(HttpStatus.SERVICE_UNAVAILABLE, "");
}
Features features = dep.getFeatures();
boolean isPerRequestKey = context.getApiKeyData().getPerRequestKey() != null;
if (features != null && Boolean.FALSE.equals(features.getAccessibleByPerRequestKey()) && isPerRequestKey) {
throw new PermissionDeniedException(String.format("Deployment %s is not accessible by %s", deploymentId, context.getApiKeyData().getSourceDeployment()));
}
context.setTraceOperation("Send request to %s deployment".formatted(dep.getName()));
context.setDeployment(dep);
return dep;
})
.compose(dep -> {
if (dep instanceof Model && !context.hasNextInterceptor()) {
return proxy.getRateLimiter().limit(context, dep);
} else {
return Future.succeededFuture(RateLimitResult.SUCCESS);
}
})
.compose(rateLimitResult -> {
Future<?> future;
if (rateLimitResult.status() == HttpStatus.OK) {
if (context.hasNextInterceptor()) {
context.setInitialDeployment(deploymentId);
context.setInitialDeploymentApi(deploymentApi);
context.setInterceptors(context.getDeployment().getInterceptors());
future = handleInterceptor(0);
} else {
future = handleRateLimitSuccess();
}
} else {
handleRateLimitHit(deploymentId, rateLimitResult);
future = Future.succeededFuture();
}
return future;
})
.otherwise(error -> {
handleRequestError(deploymentId, error);
return null;
});
}