Skip to content

Commit 84f0a43

Browse files
douggishTimHess
authored andcommitted
Fix issues with refresh polling
- Fixes issue where application crashes when FailFast is enabled and an exception is thrown during refresh polling. - Fixes issue where refresh polling is not disabled when Enabled is set to false. Addresses #1217 and #1218
1 parent 9d7ccdd commit 84f0a43

1 file changed

Lines changed: 30 additions & 6 deletions

File tree

src/Configuration/src/ConfigServerBase/ConfigServerConfigurationProvider.cs

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -140,9 +140,15 @@ internal void Initialize(ConfigServerClientSettings settings, IConfiguration con
140140
}
141141

142142
_settings = settings;
143+
144+
if (httpClient != null)
145+
{
146+
_httpClient = httpClient;
147+
}
148+
143149
OnSettingsChanged();
144150

145-
_httpClient = httpClient ?? GetHttpClient(_settings);
151+
_httpClient ??= GetHttpClient(_settings);
146152
}
147153

148154
private void OnSettingsChanged()
@@ -154,17 +160,35 @@ private void OnSettingsChanged()
154160
_configuration.GetReloadToken().RegisterChangeCallback(_ => OnSettingsChanged(), null);
155161
}
156162

157-
if (_settings.PollingInterval == TimeSpan.Zero)
163+
if (_settings.PollingInterval == TimeSpan.Zero || !_settings.Enabled)
158164
{
159165
_refreshTimer?.Dispose();
160166
}
161-
else if (_refreshTimer == null)
167+
else if (_settings.Enabled)
168+
{
169+
if (_refreshTimer == null)
170+
{
171+
_refreshTimer = new Timer(_ => DoPolledLoad(), null, TimeSpan.Zero, _settings.PollingInterval);
172+
}
173+
else if (existingPollingInterval != _settings.PollingInterval)
174+
{
175+
_refreshTimer.Change(TimeSpan.Zero, _settings.PollingInterval);
176+
}
177+
}
178+
}
179+
180+
/// <remarks>
181+
/// DoPolledLoad is called by a Timer callback, so must catch all exceptions
182+
/// </remarks>
183+
private void DoPolledLoad()
184+
{
185+
try
162186
{
163-
_refreshTimer = new Timer(_ => DoLoad(), null, TimeSpan.Zero, _settings.PollingInterval);
187+
DoLoad();
164188
}
165-
else if (existingPollingInterval != _settings.PollingInterval)
189+
catch (Exception e)
166190
{
167-
_refreshTimer.Change(TimeSpan.Zero, _settings.PollingInterval);
191+
_logger.LogWarning("Could not reload configuration during polling" + e);
168192
}
169193
}
170194

0 commit comments

Comments
 (0)