• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

ThreeMammals / Ocelot / 25319164242

04 May 2026 12:32PM UTC coverage: 93.298% (-0.3%) from 93.559%
25319164242

Pull #2300

github

web-flow
Merge 16e5748d3 into fe672ec02
Pull Request #2300: #941 Added SseDelegatingHandler

6501 of 6968 relevant lines covered (93.3%)

2806.98 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

97.92
src/Ocelot/Configuration/Repository/FileConfigurationPoller.cs
1
using Microsoft.Extensions.Hosting;
2
using Newtonsoft.Json;
3
using Ocelot.Configuration.Creator;
4
using Ocelot.Configuration.File;
5
using Ocelot.Logging;
6

7
namespace Ocelot.Configuration.Repository;
8

9
public class FileConfigurationPoller : IHostedService, IDisposable
10
{
11
    private readonly IOcelotLogger _logger;
12
    private readonly IFileConfigurationRepository _repo;
13
    private string _previousAsJson;
14
    private Timer _timer;
15
    private volatile bool _polling;
16
    private readonly IFileConfigurationPollerOptions _options;
17
    private readonly IInternalConfigurationRepository _internalConfigRepo;
18
    private readonly IInternalConfigurationCreator _internalConfigCreator;
19

20
    public FileConfigurationPoller(
11✔
21
        IOcelotLoggerFactory factory,
11✔
22
        IFileConfigurationRepository repo,
11✔
23
        IFileConfigurationPollerOptions options,
11✔
24
        IInternalConfigurationRepository internalConfigRepo,
11✔
25
        IInternalConfigurationCreator internalConfigCreator)
11✔
26
    {
27
        _internalConfigRepo = internalConfigRepo;
11✔
28
        _internalConfigCreator = internalConfigCreator;
11✔
29
        _options = options;
11✔
30
        _logger = factory.CreateLogger<FileConfigurationPoller>();
11✔
31
        _repo = repo;
11✔
32
        _previousAsJson = string.Empty;
11✔
33
    }
11✔
34

35
    private void OnTimer(object state)
36
    {
37
        if (_polling)
10✔
38
            return;
×
39

40
        _polling = true;
10✔
41
        PollAsync().GetAwaiter().GetResult(); // TODO This is not good, TimerCallback must be synchronous
10✔
42
        _polling = false;
10✔
43
    }
10✔
44

45
    public Task StartAsync(CancellationToken cancellationToken)
46
    {
47
        if (_timer is not null)
10✔
48
            return Task.CompletedTask;
1✔
49

50
        _logger.LogInformation(() => $"{nameof(FileConfigurationPoller)} is starting.");
9✔
51
        _timer = new(OnTimer, null, _options.Delay, _options.Delay); // TODO state could be CancellationToken?
9✔
52
        return Task.CompletedTask;
9✔
53
    }
54

55
    public Task StopAsync(CancellationToken cancellationToken)
56
    {
57
        if (_timer is null)
3✔
58
            return Task.CompletedTask;
1✔
59

60
        _logger.LogInformation(() => $"{nameof(FileConfigurationPoller)} is stopping.");
2✔
61
        _timer.Change(Timeout.Infinite, 0);
2✔
62
        return Task.CompletedTask;
2✔
63
    }
64

65
    private async Task PollAsync()
66
    {
67
        _logger.LogInformation(() => $"{nameof(PollAsync)}: Started polling");
10✔
68

69
        var fileConfig = await _repo.Get();
10✔
70
        if (fileConfig.IsError)
10✔
71
        {
72
            _logger.LogWarning(() => $"{nameof(PollAsync)}: Error getting file config, errors are {string.Join(',', fileConfig.Errors.Select(x => x.Message))}");
1✔
73
            return;
1✔
74
        }
75

76
        var asJson = ToJson(fileConfig.Data);
9✔
77
        if (asJson != _previousAsJson)
9✔
78
        {
79
            var config = await _internalConfigCreator.Create(fileConfig.Data);
7✔
80
            if (!config.IsError)
7✔
81
                _internalConfigRepo.AddOrReplace(config.Data);
6✔
82

83
            _previousAsJson = asJson;
7✔
84
        }
85

86
        _logger.LogInformation(() => $"{nameof(PollAsync)}: Finished polling");
9✔
87
    }
10✔
88

89
    /// <summary>
90
    /// We could do object comparison here but performance isnt really a problem. This might be an issue one day!.
91
    /// </summary>
92
    /// <returns>hash of the config.</returns>
93
    private static string ToJson(FileConfiguration config)
94
    {
95
        var currentHash = JsonConvert.SerializeObject(config);
9✔
96
        return currentHash;
9✔
97
    }
98

99
    public void Dispose()
100
    {
101
        _timer?.Dispose();
12✔
102
        _timer = null;
12✔
103
        GC.SuppressFinalize(this);
12✔
104
    }
12✔
105
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc