blob: 64f703bd2232e2eb5575c8a32bcecb1905286344 [file] [log] [blame]
// Copyright (C) 2014 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.gerrit.pgm.util;
import com.google.common.flogger.FluentLogger;
import com.google.gerrit.server.config.GerritServerConfig;
import com.google.gerrit.server.config.ThreadSettingsConfig;
import com.google.gerrit.server.schema.DataSourceType;
import com.google.inject.Injector;
import com.google.inject.Key;
import org.eclipse.jgit.lib.Config;
// TODO(dborowitz): Not necessary once we switch to NoteDb.
/** Utility to limit threads used by a batch program. */
public class ThreadLimiter {
private static final FluentLogger logger = FluentLogger.forEnclosingClass();
public static int limitThreads(Injector dbInjector, int threads) {
return limitThreads(
dbInjector.getInstance(Key.get(Config.class, GerritServerConfig.class)),
dbInjector.getInstance(DataSourceType.class),
dbInjector.getInstance(ThreadSettingsConfig.class),
threads);
}
private static int limitThreads(
Config cfg, DataSourceType dst, ThreadSettingsConfig threadSettingsConfig, int threads) {
boolean usePool = cfg.getBoolean("database", "connectionpool", dst.usePool());
int poolLimit = threadSettingsConfig.getDatabasePoolLimit();
if (usePool && threads > poolLimit) {
logger.atWarning().log("Limiting program to %d threads due to database.poolLimit", poolLimit);
return poolLimit;
}
return threads;
}
private ThreadLimiter() {}
}