pub fn load_rules_of_resource(res: &String, rules: Vec<Arc<Rule>>) -> Result<bool> {
    if res.len() == 0 {
        return Err(Error::msg("empty resource"));
    }
    let rules: HashSet<_> = rules.into_iter().collect();
    let mut global_rule_map = RULE_MAP.lock().unwrap();
    let mut global_controller_map = CONTROLLER_MAP.lock().unwrap();
    // clear resource rules
    if rules.len() == 0 {
        global_rule_map.remove(res);
        global_controller_map.remove(res);
        logging::info!("[Flow] clear resource level rules, resource {}", res);
        return Ok(true);
    }
    // load resource level rules
    if global_rule_map.get(res).unwrap_or(&HashSet::new()) == &rules {
        logging::info!("[Flow] Load resource level rules is the same with current resource level rules, so ignore load operation.");
        return Ok(false);
    }

    let mut valid_res_rules = HashSet::with_capacity(res.len());
    for rule in &rules {
        match rule.is_valid() {
            Ok(_) => {
                valid_res_rules.insert(Arc::clone(&rule));
            }
            Err(err) => logging::warn!(
                "[Flow load_rules_of_resource] Ignoring invalid flow rule {:?}, reason: {:?}",
                rule,
                err
            ),
        }
    }
    // the `res` related rules changes, have to update
    let start = utils::curr_time_nanos();
    let mut placeholder = Vec::new();
    let mut old_res_tcs = global_controller_map
        .get_mut(res)
        .unwrap_or(&mut placeholder);

    let valid_res_rules_string = format!("{:?}", &valid_res_rules);
    let new_res_tcs =
        build_resource_traffic_shaping_controller(res, &valid_res_rules, &mut old_res_tcs);

    if new_res_tcs.len() == 0 {
        global_controller_map.remove(res);
    } else {
        global_controller_map.insert(res.clone(), new_res_tcs);
    }

    global_rule_map.insert(res.clone(), rules);
    logging::debug!(
        "[Flow load_rules_of_resource] Time statistic(ns) for updating flow rule, timeCost: {}",
        utils::curr_time_nanos() - start
    );
    logging::info!(
        "[Flow] load resource level rules, resource: {}, valid_res_rules: {}",
        res,
        valid_res_rules_string
    );

    Ok(true)
}
/// `load_rules_of_resource` loads the given resource's flow rules to the rule manager, while all previous resource's rules will be replaced.
/// The first returned value indicates whether do real load operation, if the rules is the same with previous resource's rules, return false
// This func acquires locks on global `RULE_MAP` and `CONTROLLER_MAP`,
// please release your locks on them before calling this func
`load_rules_of_resource` loads the given resource's flow rules to the rule manager, while all previous resource's rules will be replaced.
The first returned value indicates whether do real load operation, if the rules is the same with previous resource's rules, return false
This func acquires locks on global `RULE_MAP` and `CONTROLLER_MAP`,
please release your locks on them before calling this func
` load_rules_of_resource ` loads the given resource ' s flow rules to the rule manager while all previous resource ' s rules will be replaced . The first returned value indicates whether do real load operation if the rules is the same with previous resource ' s rules return false This func acquires locks on global ` RULE_MAP ` and ` CONTROLLER_MAP ` please release your locks on them before calling this func
pub fn load_rules_of_resource(res: &String, rules: Vec<Arc<Rule>>) -> Result<bool> {
    if res.len() == 0 {
        return Err(Error::msg("empty resource"));
    }
    let rules: HashSet<_> = rules.into_iter().collect();
    let mut global_rule_map = RULE_MAP.lock().unwrap();
    let mut global_controller_map = CONTROLLER_MAP.lock().unwrap();
    // clear resource rules
    if rules.len() == 0 {
        global_rule_map.remove(res);
        global_controller_map.remove(res);
        logging::info!("[Flow] clear resource level rules, resource {}", res);
        return Ok(true);
    }
    // load resource level rules
    if global_rule_map.get(res).unwrap_or(&HashSet::new()) == &rules {
        logging::info!("[Flow] Load resource level rules is the same with current resource level rules, so ignore load operation.");
        return Ok(false);
    }
    let mut valid_res_rules = HashSet::with_capacity(res.len());
    for rule in &rules {
        match rule.is_valid() {
            Ok(_) => {
                valid_res_rules.insert(Arc::clone(&rule));
            }
            Err(err) => logging::warn!(
                "[Flow load_rules_of_resource] Ignoring invalid flow rule {:?}, reason: {:?}",
                rule,
                err
            ),
        }
    }
    // the `res` related rules changes, have to update
    let start = utils::curr_time_nanos();
    let mut placeholder = Vec::new();
    let mut old_res_tcs = global_controller_map
        .get_mut(res)
        .unwrap_or(&mut placeholder);
    let valid_res_rules_string = format!("{:?}", &valid_res_rules);
    let new_res_tcs =
        build_resource_traffic_shaping_controller(res, &valid_res_rules, &mut old_res_tcs);
    if new_res_tcs.len() == 0 {
        global_controller_map.remove(res);
    } else {
        global_controller_map.insert(res.clone(), new_res_tcs);
    }
    global_rule_map.insert(res.clone(), rules);
    logging::debug!(
        "[Flow load_rules_of_resource] Time statistic(ns) for updating flow rule, timeCost: {}",
        utils::curr_time_nanos() - start
    );
    logging::info!(
        "[Flow] load resource level rules, resource: {}, valid_res_rules: {}",
        res,
        valid_res_rules_string
    );
    Ok(true)
}
pub fn load_rules_of_resource ( res : & String , rules : Vec < Arc < Rule > > ) -> Result < bool > { if res . len ( ) == 0 { return Err ( Error :: msg ( " " ) ) ; } let rules : HashSet < _ > = rules . into_iter ( ) . collect ( ) ; let mut global_rule_map = RULE_MAP . lock ( ) . unwrap ( ) ; let mut global_controller_map = CONTROLLER_MAP . lock ( ) . unwrap ( ) ; // clear resource rules if rules . len ( ) == 0 { global_rule_map . remove ( res ) ; global_controller_map . remove ( res ) ; logging :: info ! ( " " res ) ; return Ok ( true ) ; } // load resource level rules if global_rule_map . get ( res ) . unwrap_or ( & HashSet :: new ( ) ) == & rules { logging :: info ! ( " " ) ; return Ok ( false ) ; } let mut valid_res_rules = HashSet :: with_capacity ( res . len ( ) ) ; for rule in & rules { match rule . is_valid ( ) { Ok ( _ ) => { valid_res_rules . insert ( Arc :: clone ( & rule ) ) ; } Err ( err ) => logging :: warn ! ( " " rule err ) , } } // the `res` related rules changes, have to update let start = utils :: curr_time_nanos ( ) ; let mut placeholder = Vec :: new ( ) ; let mut old_res_tcs = global_controller_map . get_mut ( res ) . unwrap_or ( & mut placeholder ) ; let valid_res_rules_string = format ! ( " " valid_res_rules ) ; let new_res_tcs = build_resource_traffic_shaping_controller ( res , & valid_res_rules , & mut old_res_tcs ) ; if new_res_tcs . len ( ) == 0 { global_controller_map . remove ( res ) ; } else { global_controller_map . insert ( res . clone ( ) , new_res_tcs ) ; } global_rule_map . insert ( res . clone ( ) , rules ) ; logging :: debug ! ( " " utils curr_time_nanos ( ) start ) ; logging :: info ! ( " " res valid_res_rules_string ) ; Ok ( true ) }
`load_rules_of_resource` loads the given resource's flow rules to the rule manager, while all previous resource's rules will be replaced.
` load_rules_of_resource ` loads the given resource ' s flow rules to the rule manager while all previous resource ' s rules will be replaced .
res
&String
rules
Vec<Arc<Rule>>
res
&String
rules
Vec<Arc<Rule>>