Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

服务中能维护几百个AviatorEvaluatorInstance吗? #623

Closed
missfmaster opened this issue May 9, 2024 · 2 comments
Closed

服务中能维护几百个AviatorEvaluatorInstance吗? #623

missfmaster opened this issue May 9, 2024 · 2 comments

Comments

@missfmaster
Copy link

最近遇到个使用场景:

有几百个产品对象,每个产品有自己的常量、方法、计算公式,且产品之间的常量、方法是不共享的。

思考了一下,思路如下:

  1. 每个产品维护一个AviatorEvaluatorInstance,但是可能会维护几百个AviatorEvaluatorInstance,里边有太多基础公用库,太浪费内存;
  2. 使用模块,但是每次都require、使用还要加前缀,太麻烦;
  3. 想办法让AviatorEvaluatorInstance能够做到隔离;

最终按照思路3去处理的,且修改了下源码,虽然暂时实现了功能,但是后期升级成本变高了,还不知是否存在隐患。
源码修改点:

1. AviatorEvaluatorInstance去掉final,使其能被继承;
2. AviatorEvaluatorInstance构造方法添加public;

新编写的"AviatorEvaluatorInstance"继承自原有的AviatorEvaluatorInstance,且做如下修改:

1. 添加自己的Map<String, Object> ownFuncMap,用于存放自己的方法;
2. 修改getFunction相关方法,使其可以查找ownFuncMap;
3. 新AviatorEvaluatorInstance的构造器改成如下,以实现基础库的共享:
        public AviatorEvaluatorInstanceX(final AviatorEvaluatorInstance instance) {
            this.aviatorClassLoader = instance.getAviatorClassLoader();
            this.traceOutputStream = instance.getTraceOutputStream();
            this.functionMissing = instance.getFunctionMissing();
            this.cachedExpressionByDefault = instance.isCachedExpressionByDefault();
            this.options = instance.getOriginalOptions();
            this.funcMap = instance.getFuncMap();
            this.moduleCache = instance.getModuleCache();
            this.functionLoaders = instance.getFunctionLoaders();
            this.envProcessor = instance.getEnvProcessor();
        }

所以,请问下,以后会考虑支持上边的使用场景吗?
就是可以让AviatorEvaluatorInstance构造时传入一个parent,且AviatorEvaluatorInstance中的基础库和parent共享,而不是再去load自己的。

@killme2008
Copy link
Owner

parent 共享的方式是个好主意,我想想 API 设计

@missfmaster
Copy link
Author

期待。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants