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

ThreeMammals / Ocelot / 21076334793

16 Jan 2026 06:14PM UTC coverage: 93.469% (-0.03%) from 93.503%
21076334793

Pull #2125

github

web-flow
Merge df7350da3 into 9fc4e78d3
Pull Request #2125: Remove the `Newtonsoft.Json` package and migrate to `System.Text.Json`

6569 of 7028 relevant lines covered (93.47%)

4285.1 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 Ocelot.Configuration.Creator;
3
using Ocelot.Configuration.File;
4
using Ocelot.Infrastructure;
5
using Ocelot.Logging;
6
using System.Text.Json;
7

8
namespace Ocelot.Configuration.Repository;
9

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

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

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

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

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

52
        return Task.CompletedTask;
4✔
53
    }
54

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

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

61
        return Task.CompletedTask;
×
62
    }
63

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

68
        var fileConfig = await _repo.Get();
4✔
69

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

76
        var asJson = ToJson(fileConfig.Data);
4✔
77

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

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

87
            _previousAsJson = asJson;
3✔
88
        }
89

90
        _logger.LogInformation("Finished polling");
4✔
91
    }
4✔
92

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

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