Skip to content

Commit

Permalink
Internal feature toggle to disable virtual threads (#521)
Browse files Browse the repository at this point in the history
* toggle to swap between virtual and physical threads

* creating system property
  • Loading branch information
mageddo authored Jul 31, 2024
1 parent 0e538ed commit e66a6fc
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package com.mageddo.dnsproxyserver.solver;

import com.mageddo.net.IpAddr;
import com.mageddo.utils.Executors;

import java.util.List;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.function.Function;

public class RemoteResolvers implements AutoCloseable {
Expand All @@ -23,7 +23,7 @@ public static RemoteResolvers of(List<IpAddr> servers, final Function<IpAddr, Re
.stream()
.map(resolverProvider)
.toList();
return new RemoteResolvers(resolvers, Executors.newVirtualThreadPerTaskExecutor());
return new RemoteResolvers(resolvers, Executors.newThreadExecutor());
}

public List<Resolver> resolvers() {
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/mageddo/dnsserver/TCPServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.mageddo.commons.concurrent.ThreadPool;
import com.mageddo.commons.io.IoUtils;
import com.mageddo.dnsproxyserver.utils.Ips;
import com.mageddo.utils.Executors;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.MDC;

Expand All @@ -16,7 +17,6 @@
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

Expand All @@ -36,7 +36,7 @@ public class TCPServer implements AutoCloseable {

@Inject
public TCPServer() {
this.serverThreadPool = Executors.newVirtualThreadPerTaskExecutor();
this.serverThreadPool = Executors.newThreadExecutor();
this.clients = new LinkedHashSet<>();
}

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/mageddo/dnsserver/UDPServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

import com.mageddo.commons.io.IoUtils;
import com.mageddo.dns.utils.Messages;
import com.mageddo.utils.Executors;
import lombok.extern.slf4j.Slf4j;
import org.xbill.DNS.Message;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketAddress;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@Slf4j
public class UDPServer {
Expand All @@ -24,7 +24,7 @@ public class UDPServer {
public UDPServer(SocketAddress address, RequestHandler requestHandler) {
this.address = address;
this.requestHandler = requestHandler;
this.pool = Executors.newVirtualThreadPerTaskExecutor();
this.pool = Executors.newThreadExecutor();
}

public void start() {
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/mageddo/net/NetExecutorWatchdog.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@
import com.mageddo.commons.circuitbreaker.CircuitCheckException;
import com.mageddo.commons.concurrent.Threads;
import com.mageddo.dnsproxyserver.utils.InetAddresses;
import com.mageddo.utils.Executors;
import lombok.extern.slf4j.Slf4j;

import java.net.InetSocketAddress;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

@Slf4j
public class NetExecutorWatchdog implements AutoCloseable {

public static final int FPS_120 = 1000 / 120;

private final ExecutorService threadPool = Executors.newVirtualThreadPerTaskExecutor();
private final ExecutorService threadPool = Executors.newThreadExecutor();

/**
* Will ping the #pingAddr while waiting the future to be done, which occurs first will return,
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/com/mageddo/utils/Executors.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.mageddo.utils;

import com.mageddo.commons.concurrent.ThreadPool;

import java.util.concurrent.ExecutorService;

public class Executors {
public static ExecutorService newThreadExecutor() {
if (Boolean.getBoolean("mg.physical_threads.active")) {
return ThreadPool.newFixed(50);
}
return java.util.concurrent.Executors.newVirtualThreadPerTaskExecutor();
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.mageddo.dnsproxyserver.solver;

import com.mageddo.dnsproxyserver.solver.remote.application.CircuitBreakerNonResilientService;
import com.mageddo.utils.Executors;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
Expand All @@ -16,7 +17,6 @@
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
Expand Down Expand Up @@ -45,7 +45,7 @@ class SolverRemoteTest {

@BeforeEach
void beforeEach (){
doReturn(Executors.newVirtualThreadPerTaskExecutor())
doReturn(Executors.newThreadExecutor())
.when(this.resolvers)
.getExecutor();
}
Expand Down
23 changes: 23 additions & 0 deletions src/test/java/com/mageddo/utils/ExecutorsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.mageddo.utils;

import lombok.SneakyThrows;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertTrue;

class ExecutorsTest {

@Test
@SneakyThrows
void virtualThreadsActiveByDefault() {
// arrange
try (final var executor = Executors.newThreadExecutor()) {
// act
final var isVirtual = executor.submit(() -> Thread.currentThread().isVirtual()).get();

// assert
assertTrue(isVirtual);
}

}
}

0 comments on commit e66a6fc

Please sign in to comment.