Fix bugs in branch windows (#47)

* fix: Fix the bug that LRUCache cannot work well when it is full

* fix: Avoid possible division by zero crash
This commit is contained in:
Sonullx Liu
2020-09-14 18:11:49 +08:00
committed by GitHub
parent 1938896cc3
commit 698c285cb3
2 changed files with 17 additions and 19 deletions
+7 -4
View File
@@ -467,11 +467,13 @@ static inline bool __is_alive_or_group_alive(const UpstreamAddress *ua)
const UpstreamAddress *Upstream::weighted_random_try_another() const
{
if (available_weight_ == 0)
int temp_weight = available_weight_;
if (temp_weight == 0)
return NULL;
const UpstreamAddress *ua = NULL;
int x = rand() % available_weight_;
int x = rand() % temp_weight;
int s = 0;
for (const auto *master : masters_)
@@ -604,9 +606,10 @@ const UpstreamAddress *Upstream::get(const ParsedURI& uri)
{
int x = 0;
int s = 0;
int temp_weight = total_weight_;
if (total_weight_ > 0)
x = rand() % total_weight_;
if (temp_weight > 0)
x = rand() % temp_weight;
for (idx = 0; idx < n; idx++)
{
+10 -15
View File
@@ -185,23 +185,18 @@ public:
e->ref = 1;
std::lock_guard<std::mutex> lock(mutex_);
if (max_size_ == 0 || size_ < max_size_)
size_++;
e->in_cache = true;
e->ref++;
list_append(&in_use_, e);
MapIterator it = cache_map_.find(key);
if (it != cache_map_.end())
{
size_++;
e->in_cache = true;
e->ref++;
list_append(&in_use_, e);
MapIterator it = cache_map_.find(key);
if (it != cache_map_.end())
{
erase_node(it->second);
it->second = e;
}
else
cache_map_[key] = e;
erase_node(it->second);
it->second = e;
}
else//do not cache
e->next = NULL;
else
cache_map_[key] = e;
if (max_size_ > 0)
{