/
aprogrammer
/
reverse-proxy-ms-yarp
Обзор
Документация
Войти
/
aprogrammer
/
reverse-proxy-ms-yarp
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
src/ReverseProxy/Configuration/InMemoryConfigProvider.cs
107 строк
4 KB
Thomas Carlier
Typos (#2547)
23 июл 2024, 20:30
Не верифицирован
23 июл 2024, 20:30
cb0cdec
Код
Авторство
О чём код?
// Copyright (c) Microsoft Corporation. // Licensed under the MIT License. using System; using System.Collections.Generic; using System.Threading; using Microsoft.Extensions.Primitives; namespace Yarp.ReverseProxy.Configuration; /// <summary> /// Provides an implementation of IProxyConfigProvider to support config being generated by code. /// </summary> public sealed class InMemoryConfigProvider : IProxyConfigProvider { // Marked as volatile so that updates are atomic private volatile InMemoryConfig _config; /// <summary> /// Creates a new instance. /// </summary> public InMemoryConfigProvider(IReadOnlyList<RouteConfig> routes, IReadOnlyList<ClusterConfig> clusters) : this(routes, clusters, Guid.NewGuid().ToString()) { } /// <summary> /// Creates a new instance, specifying a revision id of the configuration. /// </summary> public InMemoryConfigProvider(IReadOnlyList<RouteConfig> routes, IReadOnlyList<ClusterConfig> clusters, string revisionId) { _config = new InMemoryConfig(routes, clusters, revisionId); } /// <summary> /// Implementation of the IProxyConfigProvider.GetConfig method to supply the current snapshot of configuration /// </summary> /// <returns>An immutable snapshot of the current configuration state</returns> public IProxyConfig GetConfig() => _config; /// <summary> /// Swaps the config state with a new snapshot of the configuration, then signals that the old one is outdated. /// </summary> public void Update(IReadOnlyList<RouteConfig> routes, IReadOnlyList<ClusterConfig> clusters) { var newConfig = new InMemoryConfig(routes, clusters); UpdateInternal(newConfig); } /// <summary> /// Swaps the config state with a new snapshot of the configuration, then signals that the old one is outdated. /// </summary> public void Update(IReadOnlyList<RouteConfig> routes, IReadOnlyList<ClusterConfig> clusters, string revisionId) { var newConfig = new InMemoryConfig(routes, clusters, revisionId); UpdateInternal(newConfig); } private void UpdateInternal(InMemoryConfig newConfig) { var oldConfig = Interlocked.Exchange(ref _config, newConfig); oldConfig.SignalChange(); } /// <summary> /// Implementation of IProxyConfig which is a snapshot of the current config state. The data for this class should be immutable. /// </summary> private class InMemoryConfig : IProxyConfig { // Used to implement the change token for the state private readonly CancellationTokenSource _cts = new CancellationTokenSource(); public InMemoryConfig(IReadOnlyList<RouteConfig> routes, IReadOnlyList<ClusterConfig> clusters) : this(routes, clusters, Guid.NewGuid().ToString()) { } public InMemoryConfig(IReadOnlyList<RouteConfig> routes, IReadOnlyList<ClusterConfig> clusters, string revisionId) { RevisionId = revisionId ?? throw new ArgumentNullException(nameof(revisionId)); Routes = routes; Clusters = clusters; ChangeToken = new CancellationChangeToken(_cts.Token); } /// <inheritdoc/> public string RevisionId { get; } /// <summary> /// A snapshot of the list of routes for the proxy /// </summary> public IReadOnlyList<RouteConfig> Routes { get; } /// <summary> /// A snapshot of the list of Clusters which are collections of interchangeable destination endpoints /// </summary> public IReadOnlyList<ClusterConfig> Clusters { get; } /// <summary> /// Fired to indicate the proxy state has changed, and that this snapshot is now stale /// </summary> public IChangeToken ChangeToken { get; } internal void SignalChange() { _cts.Cancel(); } } }