Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean up ShardManager/Queuer/Runner #2653

Merged
merged 1 commit into from
Dec 9, 2023

Conversation

GnomedDev
Copy link
Member

Just some basic cleanups I noticed.

@github-actions github-actions bot added client Related to the `client` module. gateway Related to the `gateway` module. model Related to the `model` module. labels Dec 5, 2023
@GnomedDev GnomedDev force-pushed the cleanup-shard-stuff branch from 97a9f68 to c0a9436 Compare December 5, 2023 10:19
Copy link
Collaborator

@mkrasnitski mkrasnitski left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since ShardManager::{restart, restart_shard} are public functions, relying on the user to make sure that ShardInfo::total is correct seems brittle. Maybe ShardManager::shard_total could be kept and changed to type Arc<u32>? It's only set on initialization and never changed, and therefore could be kept immutable this way.

src/gateway/bridge/shard_queuer.rs Outdated Show resolved Hide resolved
src/gateway/bridge/shard_messenger.rs Show resolved Hide resolved
src/gateway/bridge/mod.rs Outdated Show resolved Hide resolved
src/gateway/bridge/shard_manager.rs Outdated Show resolved Hide resolved
src/gateway/bridge/shard_manager.rs Show resolved Hide resolved
src/model/gateway.rs Outdated Show resolved Hide resolved
@GnomedDev GnomedDev force-pushed the cleanup-shard-stuff branch from c0a9436 to ec6d247 Compare December 6, 2023 10:33
Copy link
Collaborator

@mkrasnitski mkrasnitski left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, so rather than an Arc<u32>, I figured the best way to avoid breaking the signatures of restart and restart_shard is to have shard_total be a OnceLock<u32> that gets filled inside ShardManager::initialize and is fetched inside ShardManager::boot.

src/gateway/bridge/shard_manager.rs Show resolved Hide resolved
src/gateway/bridge/shard_manager.rs Show resolved Hide resolved
src/gateway/bridge/shard_manager.rs Outdated Show resolved Hide resolved
src/gateway/bridge/shard_manager.rs Outdated Show resolved Hide resolved
src/gateway/bridge/shard_manager.rs Outdated Show resolved Hide resolved
src/gateway/bridge/shard_manager.rs Outdated Show resolved Hide resolved
src/gateway/bridge/shard_manager.rs Outdated Show resolved Hide resolved
src/gateway/bridge/shard_manager.rs Outdated Show resolved Hide resolved
src/gateway/bridge/shard_runner.rs Outdated Show resolved Hide resolved
@arqunis arqunis added enhancement An improvement to Serenity. breaking change The public API is changed, resulting in miscompilations or unexpected new behaviour for users labels Dec 8, 2023
@GnomedDev GnomedDev force-pushed the cleanup-shard-stuff branch from ec6d247 to 1ace66e Compare December 8, 2023 12:00
@github-actions github-actions bot added cache Related to the `cache`-feature. utils Related to the `utils` module. examples Related to Serenity's examples. labels Dec 8, 2023
@GnomedDev GnomedDev force-pushed the cleanup-shard-stuff branch from 1ace66e to db344cb Compare December 8, 2023 12:10
@GnomedDev
Copy link
Member Author

Right then, fixed your concerns by making ShardQueuer just store the total shard count and stop using ShardInfo everywhere redundantly.

@GnomedDev GnomedDev force-pushed the cleanup-shard-stuff branch from db344cb to 1d7a2e1 Compare December 8, 2023 12:17
@mkrasnitski
Copy link
Collaborator

Changing to u16 is fine, but making shard_total a NonZeroU16 I think is a bit awkward, since it infects the public API for Client.

@GnomedDev
Copy link
Member Author

It infecting the API for Client is somewhat intentional, it is an invariant of the API that was previously not encoded and is now in the type system.

@mkrasnitski
Copy link
Collaborator

It infecting the API for Client is somewhat intentional, it is an invariant of the API that was previously not encoded and is now in the type system.

That's true, but do you think the panic could be moved inside the function by converting from a u16? The affected functions are called early on in the life of the program, and aren't called as often as start_autosharded (I'm just assuming here).

@GnomedDev
Copy link
Member Author

If I wanted mistakes in my code to become problems at runtime, instead of compile time, I would be writing in python. I can make it panic if 0 inside the serenity code but I very much recommend against. Id::new panicking is fine enough because it's done so often and isn't likely to have a person give it 0, but this is a one time thing for each program, it just looks like more of a migration because we have 50 bots in the repo in the form of doc examples and actual examples.

Copy link
Collaborator

@mkrasnitski mkrasnitski left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Initial brief review of the new changes. The shard total is being incorrectly initialized IMO. The intention is that it should be either specified manually, or default to the value provided by the gateway. Right now it can't be manually overridden.

One way to fix this is to introduce a new ShardQueuerMessage variant that can override the value of ShardQueuer::shard_total. Then, Client::start_autosharded won't have to fire a duplicate request to the gateway.

I don't like that ShardManagerOptions::shard_total exists solely to get the value into the ShardQueuer, but I guess changing to using a OnceLock would mean that Client::start_autosharded would be the place that initializes it, meaning a duplicate gateway request.

As for the NonZeroU16 thing, all I'll say is that in the string NonZeroU16::new(1).expect("1 != 0"), out of the 35 total characters, only a single one is relevant. And, I'm sure there are plenty of other values serenity receives from the API that can never be 0 which aren't tracked as non-zero. Ultimately, the tradeoff of noisiness here is pretty stark to me, and not worth it.

src/cache/mod.rs Outdated Show resolved Hide resolved
src/gateway/bridge/shard_manager.rs Show resolved Hide resolved
src/gateway/bridge/shard_queuer.rs Outdated Show resolved Hide resolved
src/gateway/bridge/shard_queuer.rs Outdated Show resolved Hide resolved
@GnomedDev GnomedDev force-pushed the cleanup-shard-stuff branch from 1d7a2e1 to b4b1294 Compare December 9, 2023 13:18
@github-actions github-actions bot removed the examples Related to Serenity's examples. label Dec 9, 2023
@GnomedDev GnomedDev force-pushed the cleanup-shard-stuff branch from b4b1294 to 902582a Compare December 9, 2023 13:22
@GnomedDev
Copy link
Member Author

Followed through with all your suggestions.

Copy link
Collaborator

@mkrasnitski mkrasnitski left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for all your patience and work on this PR.

src/utils/mod.rs Show resolved Hide resolved
@arqunis arqunis merged commit f3e537b into serenity-rs:next Dec 9, 2023
22 checks passed
@GnomedDev GnomedDev deleted the cleanup-shard-stuff branch December 9, 2023 21:00
GnomedDev added a commit to GnomedDev/serenity that referenced this pull request Mar 13, 2024
GnomedDev added a commit to GnomedDev/serenity that referenced this pull request Mar 19, 2024
GnomedDev added a commit to GnomedDev/serenity that referenced this pull request Mar 19, 2024
GnomedDev added a commit to GnomedDev/serenity that referenced this pull request Mar 31, 2024
GnomedDev added a commit to GnomedDev/serenity that referenced this pull request Apr 1, 2024
GnomedDev added a commit to GnomedDev/serenity that referenced this pull request May 14, 2024
GnomedDev added a commit to GnomedDev/serenity that referenced this pull request Jun 9, 2024
GnomedDev added a commit to GnomedDev/serenity that referenced this pull request Jun 22, 2024
mkrasnitski pushed a commit to mkrasnitski/serenity that referenced this pull request Jul 29, 2024
mkrasnitski pushed a commit to mkrasnitski/serenity that referenced this pull request Jul 30, 2024
GnomedDev added a commit to GnomedDev/serenity that referenced this pull request Oct 7, 2024
GnomedDev added a commit to GnomedDev/serenity that referenced this pull request Oct 20, 2024
GnomedDev added a commit to GnomedDev/serenity that referenced this pull request Nov 11, 2024
GnomedDev added a commit to GnomedDev/serenity that referenced this pull request Nov 15, 2024
mkrasnitski pushed a commit to mkrasnitski/serenity that referenced this pull request Dec 8, 2024
arqunis pushed a commit to arqunis/serenity that referenced this pull request Jan 16, 2025
mkrasnitski pushed a commit to mkrasnitski/serenity that referenced this pull request Feb 1, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking change The public API is changed, resulting in miscompilations or unexpected new behaviour for users cache Related to the `cache`-feature. client Related to the `client` module. enhancement An improvement to Serenity. gateway Related to the `gateway` module. model Related to the `model` module. utils Related to the `utils` module.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants