Skip to content

Commit 1fad970

Browse files
committed
Add some failsafes to prevent the cache work loop from spinning
1 parent 0247315 commit 1fad970

File tree

1 file changed

+24
-12
lines changed

1 file changed

+24
-12
lines changed

src/bot/cache.rs

+24-12
Original file line numberDiff line numberDiff line change
@@ -187,13 +187,21 @@ impl ApiCache {
187187
}
188188
}
189189
// end of inner loop
190-
// the event processing is now done, so process a single guild
190+
// the event processing is now done, so process guilds until none are expired in a "work loop"
191191

192192
let mut now = SimpleTime::UNIX_EPOCH; // initialize it to some arbitrary default: we set it later.
193193
let mut work_remaining = true;
194+
let mut touched_guild_set =
195+
HashSet::with_hasher(ahash::RandomState::default());
194196
while work_remaining {
195197
// find the first guild that needs a refresh. This is a simple queue pop.
196198
if let Some(mut queue_entry) = queue.pop() {
199+
// if the queue is now empty no reason to go again
200+
work_remaining &= !queue.is_empty();
201+
202+
// record that we've touched this guild ID in the work loop
203+
touched_guild_set.insert(queue_entry.guild_id);
204+
197205
// grab the current time: we'll need it a couple of times and I want it to keep the same reading
198206
now = SimpleTime::now();
199207

@@ -338,23 +346,27 @@ impl ApiCache {
338346

339347
if guild_valid {
340348
// done loading the guild; time to put it back in the queue
341-
let previous_guild_id = queue_entry.guild_id;
342349
queue.push(queue_entry);
343-
let next_guild_id = queue
344-
.peek()
345-
.expect(
346-
"queue should not be empty immediately after push",
347-
)
348-
.guild_id;
349-
350-
// if the new head of the queue is different, then go again
351-
work_remaining = previous_guild_id != next_guild_id;
350+
351+
// check if we want to enter the work loop again
352+
if work_remaining {
353+
let next_guild_id = queue
354+
.peek()
355+
.expect(
356+
"queue should not be empty immediately after push",
357+
)
358+
.guild_id;
359+
360+
// if we haven't touched the next guild ID yet, then go again
361+
work_remaining &=
362+
!touched_guild_set.contains(&next_guild_id);
363+
}
352364
} else {
353365
// something about the guild was screwed up so we're just going to unregister it
354366
guild_set.remove(&queue_entry.guild_id);
355367
}
356368
}
357-
}
369+
} // end work loop
358370

359371
// update the sleep time
360372
if let Some(next_queue_entry) = queue.peek() {

0 commit comments

Comments
 (0)