package com.atlassian.stash.internal.concurrent;

import com.atlassian.bitbucket.util.PropertiesUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * Configures the thread pool executor service based on properties in {@code bitbucket.properties}. Simple expressions
 * are supported for these parameters, allowing for configuration relative to the number of available CPUs.
 * <p>
 * <strong>NOTE</strong> a minimum value of four is enforced for this property, setting the value any lower is useless
 * and the minimum will be used instead
 *
 * @since 3.6
 */
@Component("executorServiceConfiguration")
public class ExecutorServiceConfiguration {

    private static final int MIN_POOL_SIZE = 4;

    private final int corePoolSize;

    @Autowired
    public ExecutorServiceConfiguration(@Value("${executor.max.threads}") String coreThreads) {
        corePoolSize = Math.max(MIN_POOL_SIZE,
                PropertiesUtils.parseExpression(coreThreads, Runtime.getRuntime().availableProcessors()));
    }

    public int getCorePoolSize() {
        return corePoolSize;
    }

}
