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

consider support async matcher function #342

Open
wasdee opened this issue Apr 1, 2024 · 2 comments
Open

consider support async matcher function #342

wasdee opened this issue Apr 1, 2024 · 2 comments
Assignees
Labels
enhancement New feature or request

Comments

@wasdee
Copy link

wasdee commented Apr 1, 2024

Action

I want author to provide some directions about interface design if he agreed. so I or others can have confident in creating PR.

Rationale

AsyncManagementEnforcer.add_function accept only sync funciton. there will throw an error. when AsyncManagementEnforcer.enforce get called.

python stack embraces a lot of async this is an inconvenient to implentment sync match function only for using it with casbin

The Error

        explain_index = -1
        if not 0 == policy_len:
            for i, pvals in enumerate(self.model["p"][ptype].policy):
                if len(p_tokens) != len(pvals):
                    raise RuntimeError("invalid policy size")
                p_parameters = dict(zip(p_tokens, pvals))
                parameters = dict(r_parameters, **p_parameters)
                if exp_has_eval:
                    rule_names = util.get_eval_value(exp_string)
                    rules = [util.escape_assertion(p_parameters[rule_name]) for rule_name in rule_names]
                    exp_with_rule = util.replace_eval(exp_string, rules)
                    expression = self._get_expression(exp_with_rule, functions)
                result = expression.eval(parameters)
                if isinstance(result, bool):
                    if not result:
                        policy_effects.add(Effector.INDETERMINATE)
                        continue
                elif isinstance(result, float):
                    if 0 == result:
                        policy_effects.add(Effector.INDETERMINATE)
                        continue
                else:
>                   raise RuntimeError("matcher result should be bool, int or float")
E                   RuntimeError: matcher result should be bool, int or float
 
.venv/lib/python3.11/site-packages/casbin/core_enforcer.py:469: RuntimeError
=================================================================================== short test summary info ====================================================================================
FAILED tests/test_casbin/test_plan_enforcement.py::test_plan_enforcement - RuntimeError: matcher result should be bool, int or float

Suggestion

  1. add_function could support both sync and async dynamically
  2. create await enforce.enforce() async function
    1. might need a separate class or new function name ``
  3. use async.gather() for perf await enforce.batch_enforce()
@casbin-bot casbin-bot added the enhancement New feature or request label Apr 1, 2024
@casbin-bot
Copy link
Member

@techoner @Nekotoxin

@leeqvip
Copy link
Member

leeqvip commented Apr 1, 2024

@wasdee Do it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
Development

No branches or pull requests

4 participants