1212import net .bytebuddy .asm .Advice ;
1313import net .bytebuddy .description .type .TypeDescription ;
1414import net .bytebuddy .matcher .ElementMatcher ;
15+ import org .apache .pekko .http .javadsl .server .RouteResult ;
16+ import org .apache .pekko .http .scaladsl .server .RequestContext ;
17+ import scala .PartialFunction ;
18+ import scala .Unit ;
19+ import scala .concurrent .Future ;
20+ import scala .util .Try ;
1521
1622public class RouteConcatenationInstrumentation implements TypeInstrumentation {
1723 @ Override
@@ -27,20 +33,39 @@ public void transform(TypeTransformer transformer) {
2733 named ("$anonfun$$tilde$2" ), this .getClass ().getName () + "$Apply2Advice" );
2834 }
2935
36+ public static class OnExitFinalizer implements PartialFunction <Try <RouteResult >, Unit > {
37+ @ Override
38+ public boolean isDefinedAt (Try <RouteResult > x ) {
39+ return true ;
40+ }
41+
42+ @ Override
43+ public Unit apply (Try <RouteResult > v1 ) {
44+ System .err .println ("===tilde$1 onExit===" );
45+ PekkoRouteHolder .restore ();
46+ return null ;
47+ }
48+ }
49+
3050 @ SuppressWarnings ("unused" )
3151 public static class ApplyAdvice {
3252
3353 @ Advice .OnMethodEnter (suppress = Throwable .class )
3454 public static void onEnter () {
35- // when routing dsl uses concat(path(...) {...}, path(...) {...}) we'll restore the currently
36- // matched route after each matcher so that match attempts that failed wouldn't get recorded
55+ // when routing dsl uses concat(path(...) {...}, path(...) {...}) we'll restore
56+ // the currently
57+ // matched route after each matcher so that match attempts that failed wouldn't
58+ // get recorded
3759 // in the route
60+ System .err .println ("===tilde$1 onEnter===" );
3861 PekkoRouteHolder .save ();
3962 }
4063
4164 @ Advice .OnMethodExit (onThrowable = Throwable .class , suppress = Throwable .class )
42- public static void onExit () {
43- PekkoRouteHolder .restore ();
65+ public static void onExit (
66+ @ Advice .Argument (value = 2 ) RequestContext ctx ,
67+ @ Advice .Return (readOnly = false ) Future <RouteResult > fut ) {
68+ fut = fut .andThen (new OnExitFinalizer (), ctx .executionContext ());
4469 }
4570 }
4671
@@ -49,6 +74,7 @@ public static class Apply2Advice {
4974
5075 @ Advice .OnMethodEnter (suppress = Throwable .class )
5176 public static void onEnter () {
77+ System .err .println ("===tilde$2 onEnter===" );
5278 PekkoRouteHolder .reset ();
5379 }
5480 }
0 commit comments