Skip to content
Snippets Groups Projects
Commit 3fd23830 authored by akwizgran's avatar akwizgran
Browse files

Use an unbounded executor for DB tasks, which may depend on each other.

parent f076a65e
No related branches found
No related tags found
No related merge requests found
package net.sf.briar.db;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import java.sql.Connection;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Executor;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import net.sf.briar.api.clock.Clock;
import net.sf.briar.api.clock.SystemClock;
......@@ -9,7 +14,6 @@ import net.sf.briar.api.db.DatabaseComponent;
import net.sf.briar.api.db.DatabaseConfig;
import net.sf.briar.api.db.DatabaseExecutor;
import net.sf.briar.api.lifecycle.ShutdownManager;
import net.sf.briar.util.BoundedExecutor;
import com.google.inject.AbstractModule;
import com.google.inject.Provides;
......@@ -17,27 +21,23 @@ import com.google.inject.Singleton;
public class DatabaseModule extends AbstractModule {
// FIXME: Determine suitable values for these constants empirically
/**
* The maximum number of database tasks that can be queued for execution
* before submitting another task will block.
*/
private static final int MAX_QUEUED_DB_TASKS = 1000;
/** The minimum number of database threads to keep in the pool. */
private static final int MIN_DB_THREADS = 1;
/** The maximum number of database threads. */
private static final int MAX_DB_THREADS = 10;
/** The time in milliseconds to keep unused database threads alive. */
private static final int DB_KEEPALIVE = 60 * 1000;
@Override
protected void configure() {
bind(DatabaseCleaner.class).to(DatabaseCleanerImpl.class);
// The executor is bounded, so tasks must be independent and short-lived
// Database tasks may depend on each other, so use an unbounded queue
BlockingQueue<Runnable> queue = new LinkedBlockingQueue<Runnable>();
bind(Executor.class).annotatedWith(DatabaseExecutor.class).toInstance(
new BoundedExecutor(MAX_QUEUED_DB_TASKS, MIN_DB_THREADS,
MAX_DB_THREADS));
new ThreadPoolExecutor(MIN_DB_THREADS, MAX_DB_THREADS,
DB_KEEPALIVE, MILLISECONDS, queue));
}
@Provides
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment