private async Task ReauthenticateConnectionsAsync()

in src/AzureCacheOptionsProviderWithToken.cs [201:260]


    private async Task ReauthenticateConnectionsAsync()
    {
        var connectionTasks = new List<Task>(_cacheConnections.Count);
        foreach (var connection in _cacheConnections)
        {
            connectionTasks.Add(Task.Run(async () =>
            {
                try
                {
                    if (connection.Value.TokenExpiry >= _tokenExpiry)
                    {
                        // This connection has already been authenticated with the current token
                        return;
                    }

                    if (!connection.Value.ConnectionMultiplexerReference.TryGetTarget(out var connectionMultiplexer))
                    {
                        // The IConnectionMultiplexer reference is no longer valid
                        _cacheConnections.TryRemove(connection.Key, out _);
                        return;
                    }

                    var servers = connectionMultiplexer.GetServers();
                    var serverTasks = new List<Task>(servers.Length);
                    var allSucceeded = true;
                    foreach (var server in servers)
                    {
                        serverTasks.Add(Task.Run(async () =>
                        {
                            try
                            {
                                // NOTE that this will only re-authenticate interactive connections. Subscription connections (where the SUBSCRIBE command has been run) cannot be re-authenticated. 
                                // When a subscription connection's token expires, the server will close it and StackExchange.Redis will immediately restore the connection using the current token.
                                // This may result in brief gaps where subscription connections are not available and published messages aren't received by clients that are in the process of recovering their connection. 
                                await server.ExecuteAsync("AUTH", User!, Password!).ConfigureAwait(false);
                                ConnectionReauthenticated?.Invoke(this, server.EndPoint.ToString()!);
                            }
                            catch (Exception ex)
                            {
                                // No need to retry. When the connection is restored it will be authenticated with the AzureCacheOptionsProviderWithToken.Password which has been updated to the new token
                                ConnectionReauthenticationFailed?.Invoke(this, new(ex, server.EndPoint.ToString()!));
                                allSucceeded = false;
                            }
                        }));
                    }
                    await Task.WhenAll(serverTasks).ConfigureAwait(false);

                    if (allSucceeded)
                    {
                        connection.Value.TokenExpiry = _tokenExpiry;
                    }
                }
                catch (Exception ex)
                {
                    ConnectionReauthenticationFailed?.Invoke(this, new(ex, "UNKNOWN"));
                }
            }));
        }
        await Task.WhenAll(connectionTasks).ConfigureAwait(false);
    }