You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@dubbo.apache.org by hu...@apache.org on 2019/02/21 08:30:36 UTC

[incubator-dubbo] branch master updated: Cache CompiledScript #390 (#3524)

This is an automated email from the ASF dual-hosted git repository.

huxing pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-dubbo.git


The following commit(s) were added to refs/heads/master by this push:
     new 44348f4  Cache CompiledScript #390 (#3524)
44348f4 is described below

commit 44348f4a8e434250f5802c8920e01a716001a7b6
Author: huazhongming <cr...@gmail.com>
AuthorDate: Thu Feb 21 16:30:31 2019 +0800

    Cache CompiledScript #390 (#3524)
---
 .../rpc/cluster/router/script/ScriptRouter.java    | 22 +++++++++++++++++-----
 1 file changed, 17 insertions(+), 5 deletions(-)

diff --git a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/router/script/ScriptRouter.java b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/router/script/ScriptRouter.java
index 74ef98b..b7f3587 100644
--- a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/router/script/ScriptRouter.java
+++ b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/router/script/ScriptRouter.java
@@ -54,12 +54,23 @@ public class ScriptRouter extends AbstractRouter {
 
     private final String rule;
 
+    private CompiledScript function;
+
     public ScriptRouter(URL url) {
         this.url = url;
         this.priority = url.getParameter(Constants.PRIORITY_KEY, 0);
-        
+
         engine = getEngine(url);
         rule = getRule(url);
+        try {
+            Compilable compilable = (Compilable) engine;
+            function = compilable.compile(rule);
+        } catch (ScriptException e) {
+            logger.error("route error, rule has been ignored. rule: " + rule +
+                    ", url: " + RpcContext.getContext().getUrl(), e);
+        }
+
+
     }
 
     /**
@@ -72,13 +83,13 @@ public class ScriptRouter extends AbstractRouter {
         }
         return vRule;
     }
-    
+
     /**
      * create ScriptEngine instance by type from url parameters, then cache it
      */
     private ScriptEngine getEngine(URL url) {
         String type = url.getParameter(Constants.TYPE_KEY, Constants.DEFAULT_SCRIPT_TYPE_KEY);
-        
+
         return engines.computeIfAbsent(type, t -> {
             ScriptEngine scriptEngine = new ScriptEngineManager().getEngineByName(type);
             if (scriptEngine == null) {
@@ -92,8 +103,9 @@ public class ScriptRouter extends AbstractRouter {
     public <T> List<Invoker<T>> route(List<Invoker<T>> invokers, URL url, Invocation invocation) throws RpcException {
         try {
             Bindings bindings = createBindings(invokers, invocation);
-            Compilable compilable = (Compilable) engine;
-            CompiledScript function = compilable.compile(rule);
+            if (function == null) {
+                return invokers;
+            }
             return getRoutedInvokers(function.eval(bindings));
         } catch (ScriptException e) {
             logger.error("route error, rule has been ignored. rule: " + rule + ", method:" +