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

ThreeMammals / Ocelot / 18845436754

27 Oct 2025 02:57PM UTC coverage: 91.759% (+0.3%) from 91.479%
18845436754

Pull #2331

github

web-flow
Merge 3cd09ae3c into f758ba7b1
Pull Request #2331: #585 #2330 Caching global configuration

6380 of 6953 relevant lines covered (91.76%)

6313.44 hits per line

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

94.87
src/Ocelot.Provider.Consul/ConsulFileConfigurationRepository.cs
1
using Microsoft.Extensions.Options;
2
using Newtonsoft.Json;
3
using Ocelot.Cache;
4
using Ocelot.Configuration;
5
using Ocelot.Configuration.File;
6
using Ocelot.Configuration.Repository;
7
using Ocelot.Logging;
8
using Ocelot.Provider.Consul.Interfaces;
9
using Ocelot.Responses;
10
using System.Text;
11

12
namespace Ocelot.Provider.Consul;
13

14
public class ConsulFileConfigurationRepository : IFileConfigurationRepository
15
{
16
    private readonly IOcelotCache<FileConfiguration> _cache;
17
    private readonly string _configurationKey;
18
    private readonly IConsulClient _consul;
19
    private readonly IOcelotLogger _logger;
20

21
    public ConsulFileConfigurationRepository(
7✔
22
        IOptions<FileConfiguration> fileConfiguration,
7✔
23
        IOcelotCache<FileConfiguration> cache,
7✔
24
        IConsulClientFactory factory,
7✔
25
        IOcelotLoggerFactory loggerFactory)
7✔
26
    {
27
        _logger = loggerFactory.CreateLogger<ConsulFileConfigurationRepository>();
7✔
28
        _cache = cache;
7✔
29

30
        var provider = fileConfiguration.Value.GlobalConfiguration.ServiceDiscoveryProvider;
7✔
31
        _configurationKey = string.IsNullOrWhiteSpace(provider.ConfigurationKey)
7✔
32
            ? nameof(InternalConfiguration)
7✔
33
            : provider.ConfigurationKey;
7✔
34

35
        var config = new ConsulRegistryConfiguration(provider.Scheme, provider.Host,
7✔
36
            provider.Port, _configurationKey, provider.Token);
7✔
37
        _consul = factory.Get(config);
7✔
38
    }
7✔
39

40
    public async Task<Response<FileConfiguration>> Get()
41
    {
42
        var config = _cache.Get(_configurationKey, _configurationKey);
5✔
43
        if (config != null)
5✔
44
        {
45
            return new OkResponse<FileConfiguration>(config);
1✔
46
        }
47

48
        var queryResult = await _consul.KV.Get(_configurationKey);
4✔
49
        if (queryResult.Response == null)
4✔
50
        {
51
            return new OkResponse<FileConfiguration>(null);
1✔
52
        }
53

54
        var bytes = queryResult.Response.Value;
3✔
55
        var json = Encoding.UTF8.GetString(bytes);
3✔
56
        var consulConfig = JsonConvert.DeserializeObject<FileConfiguration>(json);
3✔
57

58
        return new OkResponse<FileConfiguration>(consulConfig);
3✔
59
    }
5✔
60

61
    public async Task<Response> Set(FileConfiguration ocelotConfiguration)
62
    {
63
        var json = JsonConvert.SerializeObject(ocelotConfiguration, Formatting.Indented);
1✔
64
        var bytes = Encoding.UTF8.GetBytes(json);
1✔
65
        var kvPair = new KVPair(_configurationKey)
1✔
66
        {
1✔
67
            Value = bytes,
1✔
68
        };
1✔
69

70
        var result = await _consul.KV.Put(kvPair);
1✔
71
        if (result.Response)
1✔
72
        {
73
            _cache.AddOrUpdate(_configurationKey, ocelotConfiguration, _configurationKey, TimeSpan.FromSeconds(3));
1✔
74
            return new OkResponse();
1✔
75
        }
76

77
        return new ErrorResponse(new UnableToSetConfigInConsulError(
×
78
            $"Unable to set {nameof(FileConfiguration)} in {nameof(Consul)}, response status code from {nameof(Consul)} was {result.StatusCode}"));
×
79
    }
1✔
80
}
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