public void IChangeToken_CancellationToken_ExchangeTokenBeforeOnRelaod() { // - 回调本该执行一次, 但是这里调用了两次, 说明同一个token被绑定上了两次而不是在每一个新token上绑定 - var token = new ConfigurationReloadToken(); int times = 0; ChangeToken.OnChange(() => token, () => { token = new ConfigurationReloadToken(); times++; }); token.OnReload(); // -> 2次 Assert.Equal(2, times); token.OnReload(); // -> 0次 Assert.Equal(2, times); // - 正确的调用一次的方式 - var t = new ConfigurationReloadToken(); var count = 0; ChangeToken.OnChange(() => t, () => { count++; }); Interlocked.Exchange(ref t, new ConfigurationReloadToken()).OnReload(); Assert.Equal(1, count); Interlocked.Exchange(ref t, new ConfigurationReloadToken()).OnReload(); Assert.Equal(2, count); }
public void IChangeToken_ConfigurationReloadToken() { IChangeToken token = new ConfigurationReloadToken(); Assert.False(token.HasChanged); Assert.True(token.ActiveChangeCallbacks); }
public CloudFoundryLoggerSettings(IConfiguration configuration) { _changeToken = new ConfigurationReloadToken(); _settings = new ConfigurationConsoleLoggerSettings(configuration); _settings.ChangeToken.RegisterChangeCallback(OnConfigurationReload, null); DynamicSwitches = new Dictionary <string, LogLevel>(); }
public IConsoleLoggerSettings Reload() { _settings = _settings.Reload(); _changeToken = new ConfigurationReloadToken(); _settings.ChangeToken.RegisterChangeCallback(OnConfigurationReload, null); return(this); }
protected void OnReload() { ConfigurationReloadToken previousToken = Interlocked.Exchange(ref _reloadToken, new ConfigurationReloadToken()); previousToken.OnReload(); }
private async Task PollForChanges( string key, Action <ConsulWatchExceptionContext> onException, CancellationToken cancellationToken) { while (!cancellationToken.IsCancellationRequested) { try { if (await HasValueChanged(key, cancellationToken).ConfigureAwait(false)) { ConfigurationReloadToken previousToken = Interlocked.Exchange( ref _reloadToken, new ConfigurationReloadToken()); previousToken.OnReload(); return; } } catch (Exception exception) { var exceptionContext = new ConsulWatchExceptionContext(cancellationToken, exception); onException?.Invoke(exceptionContext); } } }
private async Task PollForChanges( string key, Func <ConsulWatchExceptionContext, TimeSpan> onException, CancellationToken cancellationToken) { var consecutiveFailureCount = 0; while (!cancellationToken.IsCancellationRequested) { try { if (await HasValueChanged(key, cancellationToken).ConfigureAwait(false)) { ConfigurationReloadToken previousToken = Interlocked.Exchange( ref _reloadToken, new ConfigurationReloadToken()); previousToken.OnReload(); return; } consecutiveFailureCount = 0; } catch (Exception exception) { TimeSpan wait = onException?.Invoke( new ConsulWatchExceptionContext(cancellationToken, exception, ++consecutiveFailureCount)) ?? TimeSpan.FromSeconds(5); await Task.Delay(wait, cancellationToken); } } }
private void HandleConfigurationChange(IConfiguration configuration) { Set(configuration); configuration.GetReloadToken().RegisterChangeCallback(_ => { HandleConfigurationChange(configuration); }, null); _changeToken.OnReload(); _changeToken = new ConfigurationReloadToken(); }
public HttpConfigProvider(HttpConfigParameters httpConfigParameters, IHttpJsonHelperService httpJsonHelperService, ICacheProvider cacheProvider) { _configurationReloadToken = new ConfigurationReloadToken(); _httpConfigParameters = httpConfigParameters; _httpJsonHelperService = httpJsonHelperService; _cacheProvider = cacheProvider; }
public PluginConfigurationProvider(PluginConfigurationSource source) { _source = source ?? throw new ArgumentNullException(nameof(source)); _reloadToken = new ConfigurationReloadToken(); _providers = new ConcurrentDictionary <Zongsoft.Plugins.Plugin, CompositeConfigurationProvider>(); _pluginTree = Zongsoft.Plugins.PluginTree.Get(source.Options); _pluginTree.Loader.PluginLoaded += PluginLoader_PluginLoaded; _pluginTree.Loader.PluginUnloaded += PluginLoader_PluginUnloaded; }
public IConfigurationProvider Build(IConfigurationBuilder builder) { if (ReloadOnChange) { _reloadToken = new ConfigurationReloadToken(); } // todo: resolve services var dbContextFactory = new DefaultDbContextFactory(); return(new DbConfigurationProvider(this, dbContextFactory)); }
public void IChangeToken_ConfigurationReloadToken_OnChange() { var isChangeCallbackInvoke = false; IChangeToken token = new ConfigurationReloadToken(); token.RegisterChangeCallback(o => isChangeCallbackInvoke = true, null); Assert.False(token.HasChanged); Assert.False(isChangeCallbackInvoke); ((ConfigurationReloadToken)token).OnReload(); Assert.True(token.HasChanged); Assert.True(isChangeCallbackInvoke); }
private async Task RefreshForChanges(string key, CancellationToken cancellationToken) { while (!cancellationToken.IsCancellationRequested) { if (await this.IsValueChanged(key, cancellationToken).ConfigureAwait(false)) { ConfigurationReloadToken previousToken = Interlocked.Exchange(ref this.reloadToken, new ConfigurationReloadToken()); previousToken.OnReload(); return; } } }
/// <summary> /// Initializes a new instance of the <see cref="MongoDbConfigurationProvider"/> class. /// </summary> /// <param name="options">The options.</param> public MongoDbConfigurationProvider(MongoDbConfigOptions options) { _connectionString = options.ConnectionString; _database = options.DatabaseName; _collectionToUse = options.CollectionName; _readMode = options.ReadOption; _keysToRead = options.KeysToRead; _itemsCollection = new Dictionary <string, string>(StringComparer.OrdinalIgnoreCase); _runInQueryMode = options.QueryInFilteredMode; _keyToMatch = options.KeyToQuery; _valueToMatch = options.ValueToMatch; _token = new ConfigurationReloadToken(); }
public ConsulConfigurationProvider(bool reloadOnChange) { Console.WriteLine($"Initialize of {nameof(ConsulConfigurationProvider)}"); if (!reloadOnChange) { return; } var changeToken = new ConfigurationReloadToken(); ChangeToken.OnChange(() => { Console.WriteLine("Register change token consumer"); Task.Run(() => { var queryResult1 = QueryResult(); Console.WriteLine("Listening to changes."); var queryResult2 = QueryResult(queryResult1.LastIndex); Console.WriteLine("Value changed!"); var shouldBeTrue = StructuralComparisons.StructuralEqualityComparer.Equals( queryResult1.Response.Value, queryResult2.Response.Value); Console.WriteLine($"Is previous and current value equal? {shouldBeTrue}."); var previousToken = Interlocked.Exchange(ref changeToken, new ConfigurationReloadToken()); Console.WriteLine("[START] Call previous token to reload."); previousToken.OnReload(); Console.WriteLine("[END] Call previous token to reload."); }); return(changeToken); }, () => { Console.WriteLine("Callback consumer raised."); ExecuteLoad(reload: true); Console.WriteLine("Trigger tree reloading."); OnReload(); }); }
public DatabaseConfigurationProvider(Action <DbContextOptionsBuilder> OptionsBuilderAction) { var OptionsBuilder = new DbContextOptionsBuilder <DatabaseContext>(); OptionsBuilderAction(OptionsBuilder); DatabaseContext = new DatabaseContext(OptionsBuilder.Options); Configurations = new Dictionary <string, string>(); ConfigurationReloadToken = new ConfigurationReloadToken(); //TODO Implement ConfigurationReloadToken }
public Reload() { _firstChangeToken = new ConfigurationReloadToken(); _consulConfigClientMock .SetupSequence(ccc => ccc.Watch(It.IsAny <Action <ConsulWatchExceptionContext> >())) .Returns(_firstChangeToken) .Returns(new ConfigurationReloadToken()); _consulConfigSourceMock.Setup(ccs => ccs.OnWatchException).Returns(context => { }); _consulConfigSourceMock.Setup(ccs => ccs.Parser).Returns(_configParserMock.Object); _consulConfigSourceMock.Setup(ccs => ccs.ReloadOnChange).Returns(true); _consulConfigProvider = new ConsulConfigurationProvider( _consulConfigSourceMock.Object, _consulConfigClientMock.Object); }
public Reload() { _source = new ConsulConfigurationSource("Test", default(CancellationToken)) { Parser = _configParserMock.Object, ReloadOnChange = true }; _firstChangeToken = new ConfigurationReloadToken(); _consulConfigClientMock .SetupSequence(ccc => ccc.Watch("Test", It.IsAny <Action <ConsulWatchExceptionContext> >(), default(CancellationToken))) .Returns(_firstChangeToken) .Returns(new ConfigurationReloadToken()); _consulConfigProvider = new ConsulConfigurationProvider( _source, _consulConfigClientMock.Object); }
public ModelConfigurationProvider(ModelConfigurationSource <TModel> source) { _source = source ?? throw new ArgumentNullException(nameof(source)); _reloadToken = new ConfigurationReloadToken(); this.Data = new Dictionary <string, TModel>(StringComparer.OrdinalIgnoreCase); if (source.Models != null) { foreach (var model in source.Models) { if (model.TryGetValue(source.Mapping.Key, out var key) && key != null) { this.Data.Add(key.ToString(), model); } } } }
public void IChangeToken_ConfigurationReloadToken_CallCancleAgain() { var token = new ConfigurationReloadToken(); var isCallbackInvoked = false; token.RegisterChangeCallback(o => isCallbackInvoked = true, null); token.OnReload(); Wait(); Assert.True(isCallbackInvoked); Assert.True(token.HasChanged); isCallbackInvoked = false; var isCallbackInvokedAgain = false; token.RegisterChangeCallback(o => isCallbackInvokedAgain = true, null); Assert.True(token.HasChanged); Wait(); token.OnReload(); // 还是会调用第二次 Assert.True(isCallbackInvokedAgain); // 新注册的回调还是会调用的 Assert.True(isCallbackInvokedAgain); }
private void RaiseChanged() { ConfigurationReloadToken previousToken = Interlocked.Exchange(ref _changeToken, new ConfigurationReloadToken()); previousToken.OnReload(); }
public LanguageServerLoggingManager() { _changeToken = new(); }
public CascadeOptionsSetup(IOptionsMonitor <SourceOptions> sourceMonitor) { _registration = sourceMonitor.OnChange(RaiseChange); _changeToken = new ConfigurationReloadToken(); _sourceMonitor = sourceMonitor; }