Skip to content

Commit

Permalink
Refactor some code in tag flow control module
Browse files Browse the repository at this point in the history
  • Loading branch information
chenneal committed May 10, 2024
1 parent 97d5734 commit a8d0b43
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 11 deletions.
2 changes: 1 addition & 1 deletion sentinel-core/common/entry_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class EntryContext {

const std::string& name() const { return name_; };
const std::string& tag() const { return tag_; };
Stat::NodeSharedPtr tag_node() { return tag_node_; }

const Slot::TokenResultSharedPtr& last_token_result() const {
return last_token_result_;
Expand All @@ -26,7 +27,6 @@ class EntryContext {
void set_last_token_result(const Slot::TokenResultSharedPtr& r) {
last_token_result_ = r;
}
Stat::NodeSharedPtr get_tag_node() { return tag_node_; }
void set_tag_node(Stat::NodeSharedPtr& node) { tag_node_ = node; }

private:
Expand Down
4 changes: 2 additions & 2 deletions sentinel-core/flow/flow_rule_checker.cc
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ Stat::NodeSharedPtr FlowRuleChecker::selectNodeByRequesterAndStrategy(
std::string tag = entry->context()->tag();
std::string limit_origin = rule.limit_origin();
FlowRelationStrategy strategy = rule.strategy();
Stat::NodeSharedPtr tag_node = entry->context()->get_tag_node();
Stat::NodeSharedPtr tag_node = entry->context()->tag_node();

if ((tag == limit_origin) && IsValidTag(tag)) {
if (strategy == FlowRelationStrategy::kDirect) {
Expand All @@ -63,7 +63,7 @@ Stat::NodeSharedPtr FlowRuleChecker::selectNodeByRequesterAndStrategy(
}
return SelectNodeByRelStrategy(rule, entry, node);
} else if ((limit_origin == Constants::kLimitOriginOther) &&
m.IsTagInFlowRuleList(rule.resource(), tag)) {
m.IsTagNotInFlowRuleList(rule.resource(), tag)) {
if (strategy == FlowRelationStrategy::kDirect) {
// When rule contains other tag, which means all request except this tag
// should follow this rule.
Expand Down
6 changes: 3 additions & 3 deletions sentinel-core/flow/flow_rule_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -119,15 +119,15 @@ std::shared_ptr<TrafficShapingController> FlowRuleManager::GenerateController(
return CreateDefaultController(rule);
}

bool FlowRuleManager::IsTagInFlowRuleList(const std::string& resource_name,
const std::string& tag) {
bool FlowRuleManager::IsTagNotInFlowRuleList(const std::string& resource_name,
const std::string& tag) {
if (tag.empty()) {
return false;
}
absl::ReaderMutexLock lck(&update_mtx_);
auto got = rule_map_.find(resource_name);
if (got == rule_map_.end()) {
return false;
return true;
}

for (const auto rule : got->second) {
Expand Down
4 changes: 2 additions & 2 deletions sentinel-core/flow/flow_rule_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ class FlowRuleManager {

std::shared_ptr<TrafficShapingController> GetTrafficControllerFor(
const FlowRule& rule) const;
bool IsTagInFlowRuleList(const std::string& resource_name,
const std::string& tag);
bool IsTagNotInFlowRuleList(const std::string& resource_name,
const std::string& tag);

private:
FlowRuleManager();
Expand Down
6 changes: 3 additions & 3 deletions sentinel-core/slot/statistic_slot.cc
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ TokenResultSharedPtr StatisticSlot::OnPass(
count);
}
if (entry != nullptr) {
this->RecordPassFor(entry->context()->get_tag_node(), count);
this->RecordPassFor(entry->context()->tag_node(), count);
}
auto metric = ParamFlowSlot::GetParamMetric(resource->name());
if (metric) {
Expand All @@ -69,7 +69,7 @@ TokenResultSharedPtr StatisticSlot::OnBlock(
prev_result->blocked_reason().value_or("unexpected_blocked"));

this->RecordBlockFor(node, count);
this->RecordBlockFor(entry->context()->get_tag_node(), count);
this->RecordBlockFor(entry->context()->tag_node(), count);
if (resource->entry_type() == EntryType::IN) {
this->RecordBlockFor(
Stat::ResourceNodeStorage::GetInstance().GetEntryNode(), count);
Expand Down Expand Up @@ -116,7 +116,7 @@ void StatisticSlot::Exit(const EntrySharedPtr& entry, int count,
entry->set_rt(rt);

this->RecordCompleteFor(node, rt, entry->error(), count);
this->RecordCompleteFor(entry->context()->get_tag_node(), rt, entry->error(),
this->RecordCompleteFor(entry->context()->tag_node(), rt, entry->error(),
count);
if (entry->resource()->entry_type() == EntryType::IN) {
this->RecordCompleteFor(
Expand Down

0 comments on commit a8d0b43

Please sign in to comment.