Run delayed velocity tasks under our scheduler too

This commit is contained in:
games647
2024-05-07 20:31:55 +02:00
parent 7b6d2062cf
commit bb78043d64
9 changed files with 45 additions and 61 deletions

View File

@@ -23,47 +23,31 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package com.github.games647.fastlogin.core;
package com.github.games647.fastlogin.core.scheduler;
import org.slf4j.Logger;
import java.time.Duration;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;
/**
* This limits the number of threads that are used at maximum. Thread creation can be very heavy for the CPU and
* context switching between threads too. However, we need many threads for blocking HTTP and database calls.
* Nevertheless, this number can be further limited, because the number of actually working database threads
* is limited by the size of our database pool. The goal is to separate concerns into processing and blocking only
* threads.
*/
public class AsyncScheduler {
public abstract class AbstractAsyncScheduler {
private final Logger logger;
protected final Logger logger;
protected final Executor processingPool;
protected final AtomicInteger currentlyRunning = new AtomicInteger();
private final Executor asyncPool;
private final AtomicInteger currentlyRunning = new AtomicInteger();
public AsyncScheduler(Logger logger, Executor processingPool) {
public AbstractAsyncScheduler(Logger logger, Executor processingPool) {
this.logger = logger;
logger.info("Using optimized green threads with Java 21");
this.asyncPool = Executors.newVirtualThreadPerTaskExecutor();
this.processingPool = processingPool;
}
public CompletableFuture<Void> runAsync(Runnable task) {
return CompletableFuture
.runAsync(() -> process(task), asyncPool)
.exceptionally(error -> {
logger.warn("Error occurred on thread pool", error);
return null;
});
}
public abstract CompletableFuture<Void> runAsync(Runnable task);
private void process(Runnable task) {
public abstract CompletableFuture<Void> runAsyncDelayed(Runnable task, Duration delay);
protected void process(Runnable task) {
currentlyRunning.incrementAndGet();
try {
task.run();

View File

@@ -23,13 +23,13 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package com.github.games647.fastlogin.core;
package com.github.games647.fastlogin.core.scheduler;
import org.slf4j.Logger;
import java.time.Duration;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;
import java.util.concurrent.atomic.AtomicInteger;
/**
* This limits the number of threads that are used at maximum. Thread creation can be very heavy for the CPU and
@@ -38,21 +38,14 @@ import java.util.concurrent.atomic.AtomicInteger;
* is limited by the size of our database pool. The goal is to separate concerns into processing and blocking only
* threads.
*/
public class AsyncScheduler {
private final Logger logger;
private final Executor processingPool;
private final AtomicInteger currentlyRunning = new AtomicInteger();
public class AsyncScheduler extends AbstractAsyncScheduler {
public AsyncScheduler(Logger logger, Executor processingPool) {
this.logger = logger;
super(logger, processingPool);
logger.info("Using legacy scheduler");
this.processingPool = processingPool;
}
@Override
public CompletableFuture<Void> runAsync(Runnable task) {
return CompletableFuture.runAsync(() -> process(task), processingPool).exceptionally(error -> {
logger.warn("Error occurred on thread pool", error);
@@ -60,12 +53,22 @@ public class AsyncScheduler {
});
}
private void process(Runnable task) {
currentlyRunning.incrementAndGet();
try {
task.run();
} finally {
currentlyRunning.getAndDecrement();
}
@Override
public CompletableFuture<Void> runAsyncDelayed(Runnable task, Duration delay) {
return CompletableFuture.runAsync(() -> {
currentlyRunning.incrementAndGet();
try {
Thread.sleep(delay.toMillis());
process(task);
} catch (InterruptedException interruptedException) {
// restore interrupt flag
Thread.currentThread().interrupt();
} finally {
currentlyRunning.getAndDecrement();
}
}, processingPool).exceptionally(error -> {
logger.warn("Error occurred on thread pool", error);
return null;
});
}
}

View File

@@ -25,8 +25,8 @@
*/
package com.github.games647.fastlogin.core.shared;
import com.github.games647.fastlogin.core.AsyncScheduler;
import com.github.games647.fastlogin.core.hooks.bedrock.BedrockService;
import com.github.games647.fastlogin.core.scheduler.AsyncScheduler;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import org.slf4j.Logger;

View File

@@ -25,6 +25,7 @@
*/
package com.github.games647.fastlogin.core;
import com.github.games647.fastlogin.core.scheduler.AsyncScheduler;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.DisabledForJreRange;