Skip to content

Conversation

@KubaZ2
Copy link
Member

@KubaZ2 KubaZ2 commented Oct 29, 2025

Introduced an AutoStartStop option in GatewayClientOptions and ShardedGatewayClientOptions to control automatic start and stop behavior of gateway clients. Updated StartAsync and StopAsync methods in GatewayClientHostedService and ShardedGatewayClientHostedService to respect this option.

Introduced an `AutoStartStop` option in `GatewayClientOptions`
and `ShardedGatewayClientOptions` to control automatic start
and stop behavior of gateway clients. Updated `StartAsync`
and `StopAsync` methods in `GatewayClientHostedService` and
`ShardedGatewayClientHostedService` to respect this option.
@KubaZ2 KubaZ2 requested a review from Copilot October 29, 2025 21:08
@github-actions
Copy link

The documentation preview is available at https://preview.netcord.dev/207.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR adds an AutoStartStop configuration option to control whether the Gateway client automatically starts and stops with the hosted service lifecycle. When set to false, the gateway client will not automatically start when the application starts or close when the application stops.

  • Adds AutoStartStop nullable boolean property to both GatewayClientOptions and ShardedGatewayClientOptions
  • Implements early returns in both hosted services' StartAsync and StopAsync methods when AutoStartStop is set to false
  • Updates test code to demonstrate usage of the new configuration option

Reviewed Changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated no comments.

Show a summary per file
File Description
Tests/NetCord.Test.Hosting/Program.cs Demonstrates setting AutoStartStop = true in gateway configuration
Hosting/NetCord.Hosting/Gateway/GatewayClientOptions.cs Adds AutoStartStop nullable boolean property
Hosting/NetCord.Hosting/Gateway/ShardedGatewayClientOptions.cs Adds AutoStartStop nullable boolean property
Hosting/NetCord.Hosting/Gateway/GatewayClientHostedService.cs Implements conditional start/stop logic based on AutoStartStop option
Hosting/NetCord.Hosting/Gateway/ShardedGatewayClientHostedService.cs Implements conditional start/stop logic based on AutoStartStop option
Comments suppressed due to low confidence (2)

Hosting/NetCord.Hosting/Gateway/GatewayClientHostedService.cs:26

  • When AutoStartStop is false, the client and handlers are retrieved and registered unnecessarily before the early return. Move the GetRequiredService and handler registration logic after the AutoStartStop check to avoid unnecessary service resolution and handler registration when the client won't be started.
        var client = services.GetRequiredService<GatewayClient>();

        foreach (var handler in services.GetServices<IGatewayHandler>())
        {
            if (handler is IDelegateGatewayHandlerBase delegateHandler)
                RegisterDelegateHandler(client, delegateHandler);
            else
                RegisterClassHandler(client, handler);
        }

Hosting/NetCord.Hosting/Gateway/ShardedGatewayClientHostedService.cs:26

  • When AutoStartStop is false, the client and handlers are retrieved and registered unnecessarily before the early return. Move the GetRequiredService and handler registration logic after the AutoStartStop check to avoid unnecessary service resolution and handler registration when the client won't be started.
        var client = services.GetRequiredService<ShardedGatewayClient>();

        foreach (var handler in services.GetServices<IShardedGatewayHandler>())
        {
            if (handler is IDelegateShardedGatewayHandlerBase delegateHandler)
                RegisterDelegateShardedHandler(client, delegateHandler);
            else
                RegisterClassShardedHandler(client, handler);
        }

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@KubaZ2 KubaZ2 requested a review from Copilot October 29, 2025 22:21
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.

Comments suppressed due to low confidence (1)

Hosting/NetCord.Hosting/Gateway/ShardedGatewayClientHostedService.cs:1

  • The options are retrieved twice from the service provider (once in StartAsync and once in StopAsync). Consider caching the options value in a field during StartAsync or retrieving it once in the constructor to avoid repeated service resolution.
using Microsoft.Extensions.DependencyInjection;

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@KubaZ2 KubaZ2 merged commit 0e45faf into alpha Oct 29, 2025
7 checks passed
@KubaZ2 KubaZ2 deleted the feature/add-hosting-gateway-start-stop-config branch October 29, 2025 22:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants