From 868264d0567292126ad076da5a9fde51642818db Mon Sep 17 00:00:00 2001 From: Daniel Cazzulino Date: Fri, 2 Jan 2026 02:00:03 -0300 Subject: [PATCH] Make client disposable so it clears Grpc clients --- src/xAI/GrokClient.cs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/xAI/GrokClient.cs b/src/xAI/GrokClient.cs index 3a9a4c5..45e6e4c 100644 --- a/src/xAI/GrokClient.cs +++ b/src/xAI/GrokClient.cs @@ -7,9 +7,9 @@ namespace xAI; /// Client for interacting with the Grok service. /// The API key used for authentication. /// The options used to configure the client. -public class GrokClient(string apiKey, GrokClientOptions options) +public sealed class GrokClient(string apiKey, GrokClientOptions options) : IDisposable { - static ConcurrentDictionary<(Uri, string), GrpcChannel> channels = []; + static readonly ConcurrentDictionary<(Uri, string), GrpcChannel> channels = []; /// Initializes a new instance of the class with default options. public GrokClient(string apiKey) : this(apiKey, new GrokClientOptions()) { } @@ -36,6 +36,9 @@ public class GrokClient(string apiKey, GrokClientOptions options) return GrpcChannel.ForAddress(Endpoint, options); }); + /// Clears the cached list of gRPC channels in the client. + public void Dispose() => channels.Clear(); + class AuthenticationHeaderHandler(string apiKey) : DelegatingHandler { protected override Task SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)