Skip to content

Commit

Permalink
Added logic to retry when registration to OriginMapStore fails
Browse files Browse the repository at this point in the history
  • Loading branch information
getroot committed Sep 24, 2024
1 parent adca439 commit 900589b
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
45 changes: 44 additions & 1 deletion src/projects/modules/origin_map_client/origin_map_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ OriginMapClient::OriginMapClient(const ov::String &redis_host, const ov::String

_update_timer.Push(
[this](void *paramter) -> ov::DelayQueueAction {
RetryRegister();
NofifyStreamsAlive();
return ov::DelayQueueAction::Repeat;
},
Expand All @@ -47,6 +48,38 @@ bool OriginMapClient::NofifyStreamsAlive()
return true;
}

bool OriginMapClient::RetryRegister()
{
std::unique_lock<std::mutex> lock(_origin_map_mutex);
if (_origin_map_candidates.size() == 0)
{
return true;
}

auto origin_map_candidates = _origin_map_candidates;
lock.unlock();

std::vector<ov::String> keys_to_remove;
for (auto &[key, value] : origin_map_candidates)
{
if (Register(key, value) == true)
{
keys_to_remove.push_back(key);
}
}

if (keys_to_remove.size() > 0)
{
std::lock_guard<std::mutex> lock(_origin_map_mutex);
for (auto &key : keys_to_remove)
{
_origin_map_candidates.erase(key);
}
}

return true;
}

bool OriginMapClient::Register(const ov::String &app_stream_name, const ov::String &origin_host)
{
if (ConnectRedis() == false)
Expand Down Expand Up @@ -78,8 +111,13 @@ bool OriginMapClient::Register(const ov::String &app_stream_name, const ov::Stri
}
else
{
logte("<%s> stream is already registered with different origin host.", app_stream_name.CStr());
logte("<%s> stream is already registered with different origin host (%s)", app_stream_name.CStr(), reply->str);
freeReplyObject(reply);
lock.unlock();

std::lock_guard<std::mutex> origin_map_lock(_origin_map_mutex);
_origin_map_candidates[app_stream_name] = origin_host;

return false;
}
}
Expand All @@ -99,6 +137,11 @@ bool OriginMapClient::Register(const ov::String &app_stream_name, const ov::Stri
else if (reply->type == REDIS_REPLY_NIL)
{
logte("<%s> stream is already registered.", app_stream_name.CStr());
freeReplyObject(reply);
lock.unlock();

std::lock_guard<std::mutex> origin_map_lock(_origin_map_mutex);
_origin_map_candidates[app_stream_name] = origin_host;
return false;
}

Expand Down
2 changes: 2 additions & 0 deletions src/projects/modules/origin_map_client/origin_map_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class OriginMapClient
bool ConnectRedis();

bool NofifyStreamsAlive();
bool RetryRegister();

ov::String _redis_ip;
uint16_t _redis_port;
Expand All @@ -46,6 +47,7 @@ class OriginMapClient
ov::DelayQueue _update_timer{"OMapC"};

std::map<ov::String, ov::String> _origin_map;
std::map<ov::String, ov::String> _origin_map_candidates;
std::mutex _origin_map_mutex;

redisContext *_redis_context = nullptr;
Expand Down

0 comments on commit 900589b

Please sign in to comment.