Skip to content

Commit

Permalink
vertx: decrease amount of tokens created
Browse files Browse the repository at this point in the history
  • Loading branch information
meiao committed Dec 16, 2024
1 parent 925da72 commit 8baea0d
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,15 @@ public static void linkAndExpireToken(Listener listener) {
}
}

public static void expireToken(Listener listener) {
if (listener != null) {
final Token token = tokenMap.remove(listener);
if (token != null) {
token.expire();
}
}
}

public static void processResponse(Segment segment, HttpClientResponseImpl resp, String host, int port,
String scheme) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@
package io.vertx.core.impl.future;

import com.newrelic.api.agent.Trace;
import com.newrelic.api.agent.weaver.MatchType;
import com.newrelic.api.agent.weaver.Weave;
import com.newrelic.api.agent.weaver.Weaver;
import com.nr.vertx.instrumentation.VertxCoreUtil;

import java.util.ArrayList;

@Weave(originalName = "io.vertx.core.impl.future.FutureImpl")
public abstract class FutureImpl_Instrumentation {

Expand All @@ -21,12 +24,26 @@ public abstract class FutureImpl_Instrumentation {

@Trace(async = true, excludeFromTransactionTrace = true)
public void addListener(Listener listener) {
boolean newListenerArray = false;
if (isComplete()) {
VertxCoreUtil.linkAndExpireToken(listener);
} else {
VertxCoreUtil.storeToken(listener);
if (this.listener == null) {
// storing a token for the first listener, which will be set in callOriginal
VertxCoreUtil.storeToken(listener);
} else if (!(this.listener instanceof ListenerArray)) {
// when a 2nd listener is added, it will convert this.listener to a ListenerArray
// so we expire the previous token, and after the ListenerArray is created, we store a token for that
VertxCoreUtil.expireToken(this.listener);
newListenerArray = true;
}
}

Weaver.callOriginal();

if (newListenerArray) {
VertxCoreUtil.storeToken(this.listener);
}
}

@Trace(async = true, excludeFromTransactionTrace = true)
Expand All @@ -40,4 +57,8 @@ public boolean tryFail(Throwable cause) {
VertxCoreUtil.linkAndExpireToken(this.listener);
return Weaver.callOriginal();
}
}

@Weave(type = MatchType.ExactClass, originalName = "io.vertx.core.impl.future.FutureImpl$ListenerArray")
private abstract static class ListenerArray<T> extends ArrayList<Listener<T>> implements Listener<T> {
}
}

0 comments on commit 8baea0d

Please sign in to comment.