Skip to content

Commit

Permalink
Avoid environment lookups and value conversions on hot paths
Browse files Browse the repository at this point in the history
  • Loading branch information
ppalaga committed Nov 18, 2020
1 parent 450cce9 commit 8ad2734
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public class DaemonClientConnection implements Closeable {
private final Thread receiver;
private final AtomicBoolean running = new AtomicBoolean(true);
private final AtomicReference<Exception> exception = new AtomicReference<>();
private final long maxKeepAliveMs;
private final DaemonParameters parameters;

public DaemonClientConnection(DaemonConnection connection, DaemonInfo daemon,
Expand All @@ -64,6 +65,7 @@ public DaemonClientConnection(DaemonConnection connection, DaemonInfo daemon,
this.receiver = new Thread(this::doReceive);
this.receiver.start();
this.parameters = parameters;
this.maxKeepAliveMs = parameters.keepAlive().toMillis() * parameters.maxLostKeepAlive();
}

public void dispatch(Message message) throws DaemonException.ConnectException {
Expand Down Expand Up @@ -94,7 +96,6 @@ public void dispatch(Message message) throws DaemonException.ConnectException {
}

public List<Message> receive() throws ConnectException, StaleAddressException {
long maxKeepAliveMs = parameters.keepAlive().toMillis() * parameters.maxLostKeepAlive();
while (true) {
try {
final Message m = queue.poll(maxKeepAliveMs, TimeUnit.MILLISECONDS);
Expand Down
6 changes: 4 additions & 2 deletions daemon/src/main/java/org/mvndaemon/mvnd/daemon/Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ public class Server implements AutoCloseable, Runnable {
private final Lock stateLock = new ReentrantLock();
private final Condition condition = stateLock.newCondition();
private final DaemonMemoryStatus memoryStatus;
private final long keepAliveMs;

public Server() throws IOException {
// When spawning a new process, the child process is create within
Expand All @@ -107,6 +108,8 @@ public Server() throws IOException {
}
this.uid = Environment.MVND_UID.asString();
this.noDaemon = Environment.MVND_NO_DAEMON.asBoolean();
this.keepAliveMs = Environment.MVND_KEEP_ALIVE.asDuration().toMillis();

try {
cli = new DaemonMavenCli();
registry = new DaemonRegistry(Environment.MVND_REGISTRY.asPath());
Expand Down Expand Up @@ -424,7 +427,6 @@ private void cancelNow() {
private void handle(DaemonConnection connection, BuildRequest buildRequest) {
updateState(Busy);
try {
Duration keepAlive = Environment.MVND_KEEP_ALIVE.asDuration();

LOGGER.info("Executing request");

Expand All @@ -440,7 +442,7 @@ private void handle(DaemonConnection connection, BuildRequest buildRequest) {
while (true) {
Message m;
if (flushed) {
m = sendQueue.poll(keepAlive.toMillis(), TimeUnit.MILLISECONDS);
m = sendQueue.poll(keepAliveMs, TimeUnit.MILLISECONDS);
if (m == null) {
m = Message.KEEP_ALIVE_SINGLETON;
}
Expand Down

0 comments on commit 8ad2734

Please sign in to comment.