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

ThreeMammals / Ocelot / 16444211173

22 Jul 2025 12:25PM UTC coverage: 87.672% (-0.3%) from 87.958%
16444211173

Pull #2300

github

web-flow
Merge 8b98a65a3 into c721273e2
Pull Request #2300: #941 Added SseDelegatingHandler

5718 of 6522 relevant lines covered (87.67%)

7509.01 hits per line

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

87.23
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 bool _polling;
16
    private readonly IFileConfigurationPollerOptions _options;
17
    private readonly IInternalConfigurationRepository _internalConfigRepo;
18
    private readonly IInternalConfigurationCreator _internalConfigCreator;
19

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

35
    public Task StartAsync(CancellationToken cancellationToken)
36
    {
37
        _logger.LogInformation($"{nameof(FileConfigurationPoller)} is starting.");
4✔
38

39
        _timer = new Timer(async x =>
4✔
40
        {
4✔
41
            if (_polling)
3✔
42
            {
4✔
43
                return;
×
44
            }
4✔
45

4✔
46
            _polling = true;
3✔
47
            await Poll();
3✔
48
            _polling = false;
3✔
49
        }, null, _options.Delay, _options.Delay);
7✔
50

51
        return Task.CompletedTask;
4✔
52
    }
53

54
    public Task StopAsync(CancellationToken cancellationToken)
55
    {
56
        _logger.LogInformation($"{nameof(FileConfigurationPoller)} is stopping.");
×
57

58
        _timer?.Change(Timeout.Infinite, 0);
×
59

60
        return Task.CompletedTask;
×
61
    }
62

63
    private async Task Poll()
64
    {
65
        _logger.LogInformation("Started polling");
3✔
66

67
        var fileConfig = await _repo.Get();
3✔
68

69
        if (fileConfig.IsError)
3✔
70
        {
71
            _logger.LogWarning(() => $"error geting file config, errors are {string.Join(',', fileConfig.Errors.Select(x => x.Message))}");
×
72
            return;
×
73
        }
74

75
        var asJson = ToJson(fileConfig.Data);
3✔
76

77
        if (!fileConfig.IsError && asJson != _previousAsJson)
3✔
78
        {
79
            var config = await _internalConfigCreator.Create(fileConfig.Data);
3✔
80

81
            if (!config.IsError)
3✔
82
            {
83
                _internalConfigRepo.AddOrReplace(config.Data);
3✔
84
            }
85

86
            _previousAsJson = asJson;
3✔
87
        }
88

89
        _logger.LogInformation("Finished polling");
3✔
90
    }
3✔
91

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

102
    public void Dispose()
103
    {
104
        _timer?.Dispose();
6✔
105
        _timer = null;
6✔
106
    }
6✔
107
}
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