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

feat: support rewrite config after parse_rule_config invoked #401

Merged
merged 1 commit into from
Jun 28, 2023

Conversation

jizhuozhi
Copy link
Contributor

@jizhuozhi jizhuozhi commented Jun 28, 2023

Ⅰ. Describe what this PR did

Support rewrite config after parse_rule_config invoked.

The configuration we usually get just describes what we want to do, but at runtime we have many efficient data structures to allow us to complete these tasks more efficiently. For example, there is a coverage relationship between CIDRs, so we only need to keep the one with the largest coverage interval. In addition, for string matching, Radix Trie is far more efficient than list matching, and this needs to be rewritten after the configuration is parsed.

There is no other way for extensions to do the rewriting, because both global_config and rule_config belong to rule_matcher, and they are not public (and cannot be public). So we need a mapping function to do the rewriting. fn(config: &PluginConfig) -> PluginConfig is not &mut intentional, I always think that pure functions can simplify the user's mind.

There is an example that how to rewrite config

#[derive(Default, Debug, Deserialize)]
struct DemoConfig {
    words: Vec<String>,
    trie: Option<Trie> // fields need rewrite should be `Option` because they are not actual configuraion
}

struct DemoRoot { /* rule_matcher and other fields are ignored */ }

impl RootContext for DemoRoot {
    fn on_configure(&mut self, _plugin_configuration_size: usize) -> bool {
        /* codes with invoking parse_rule_config... */

        rule_matcher.rewrite_config(|config| {
            let trie = covert(config.words);
            DemoConfig {
                words: vec![],    // there is no need to still hold words
                trie: Some(trie)
            }
        });

        true
    }

Ⅱ. Does this pull request fix one issue?

Ⅲ. Why don't you add test cases (unit test/integration test)?

Ⅳ. Describe how to verify it

Ⅴ. Special notes for reviews

Copy link
Collaborator

@johnlanni johnlanni left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@johnlanni johnlanni merged commit fc05a3b into alibaba:main Jun 28, 2023
3 checks passed
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

Successfully merging this pull request may close these issues.

2 participants