Skip to content

Commit

Permalink
clean maps when we can to free up bucket leakage
Browse files Browse the repository at this point in the history
  • Loading branch information
braindigitalis committed Jun 21, 2024
1 parent 99eea9f commit 478f717
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 2 deletions.
8 changes: 8 additions & 0 deletions modules/trivia/cmd_start.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,9 @@ void command_start_t::call(const in_cmd &cmd, std::stringstream &tokens, guild_s
creator->SimpleEmbed(settings, ":octagonal_sign:", _("DASH_STOP", settings), j->first, _("STOPPING", settings));
log_game_end(cmd.guild_id, j->first);
creator->states.erase(j);
if (creator->states.size() == 0) {
creator->states = {};
}
break;
} else {
creator->EmbedWithFields(cmd.interaction_token, cmd.command_id, settings, _("NOWAY", settings), {
Expand All @@ -150,6 +153,9 @@ void command_start_t::call(const in_cmd &cmd, std::stringstream &tokens, guild_s
creator->SimpleEmbed(settings, ":octagonal_sign:", _("DASH_STOP", settings), j->first, _("STOPPING", settings));
log_game_end(cmd.guild_id, j->first);
creator->states.erase(j);
if (creator->states.size() == 0) {
creator->states = {};
}
break;
} else {
number_of_games++;
Expand Down Expand Up @@ -260,6 +266,8 @@ void command_start_t::call(const in_cmd &cmd, std::stringstream &tokens, guild_s
creator->GetBot()->core->log(dpp::ll_debug, fmt::format("Streak for channel id {} not carried over, as it is over 10 minutes old", cmd.channel_id));
}
}
auto streaks = creator->last_channel_streaks;
creator->last_channel_streaks = streaks;
}

{
Expand Down
18 changes: 16 additions & 2 deletions modules/trivia/commands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -451,13 +451,27 @@ void TriviaModule::thinking(bool ephemeral, const dpp::interaction_create_t& eve
bool TriviaModule::has_rl_warn(dpp::snowflake channel_id) {
std::shared_lock cmd_lock(cmdmutex);
auto check = last_rl_warning.find(channel_id);
return check != last_rl_warning.end() && time(NULL) < check->second;
bool r = check != last_rl_warning.end() && time(NULL) < check->second;
if (check != limits.end() && time(NULL) >= check->second) {
last_rl_warning.erase(check);
}
if (last_rl_warning.size() == 0) {
last_rl_warning = {};
}
return r;
}

bool TriviaModule::has_limit(dpp::snowflake channel_id) {
std::shared_lock cmd_lock(cmdmutex);
auto check = limits.find(channel_id);
return check != limits.end() && time(NULL) < check->second;
bool r = check != limits.end() && time(NULL) < check->second;
if (check != limits.end() && time(NULL) >= check->second) {
limits.erase(check);
}
if (limits.size() == 0) {
limits = {};
}
return r;
}

bool TriviaModule::set_rl_warn(dpp::snowflake channel_id) {
Expand Down
7 changes: 7 additions & 0 deletions modules/trivia/trivia.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,8 @@ bool TriviaModule::OnGuildDelete(const dpp::guild_delete_t &gd)
{
std::unique_lock locker(settingcache_mutex);
settings_cache.erase(gd.deleted.id);
auto s = settings_cache;
settings_cache = s;
}
db::backgroundquery("UPDATE trivia_guild_cache SET kicked = 1 WHERE snowflake_id = ?", {gd.deleted.id});
bot->core->log(dpp::ll_info, fmt::format("Kicked from guild id {}", gd.deleted.id));
Expand Down Expand Up @@ -373,6 +375,8 @@ void TriviaModule::eraseCache(dpp::snowflake guild_id)
auto i = settings_cache.find(guild_id);
if (i != settings_cache.end()) {
settings_cache.erase(i);
auto s = settings_cache;
settings_cache = s;
}
}

Expand Down Expand Up @@ -527,6 +531,9 @@ void TriviaModule::Tick()
for (auto e : expired) {
bot->core->log(dpp::ll_debug, fmt::format("Terminating state id {}", e));
states.erase(e);
if (states.size() == 0) {
states = {};
}
}
}
catch (const std::exception &e) {
Expand Down
2 changes: 2 additions & 0 deletions modules/trivia/webrequest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,8 @@ std::string web_request(const std::string &_host, const std::string &_path, cons
if (i != module->webhooks.end()) {
module->webhooks.erase(i);
}
auto wh = module->webhooks;
module->webhooks = wh;
}
db::backgroundquery("DELETE FROM channel_webhooks WHERE channel_id = ?", {channel_id});
}
Expand Down

0 comments on commit 478f717

Please sign in to comment.