-
Notifications
You must be signed in to change notification settings - Fork 2k
/
ConsulGatewayListProvider.cs
75 lines (66 loc) · 2.48 KB
/
ConsulGatewayListProvider.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using Consul;
using Orleans.Messaging;
using Orleans.Runtime.Configuration;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Orleans.Configuration;
namespace Orleans.Runtime.Membership
{
public class ConsulGatewayListProvider : IGatewayListProvider
{
private ConsulClient consulClient;
private string clusterId;
private ILogger logger;
private readonly ConsulClusteringClientOptions options;
private readonly TimeSpan maxStaleness;
private readonly string kvRootFolder;
public ConsulGatewayListProvider(
ILogger<ConsulGatewayListProvider> logger,
IOptions<ConsulClusteringClientOptions> options,
IOptions<GatewayOptions> gatewayOptions,
IOptions<ClusterOptions> clusterOptions)
{
this.logger = logger;
this.clusterId = clusterOptions.Value.ClusterId;
this.maxStaleness = gatewayOptions.Value.GatewayListRefreshPeriod;
this.options = options.Value;
this.kvRootFolder = options.Value.KvRootFolder;
}
public TimeSpan MaxStaleness
{
get { return this.maxStaleness; }
}
public bool IsUpdatable
{
get { return true; }
}
public Task InitializeGatewayListProvider()
{
consulClient =
new ConsulClient(config =>
{
config.Address = options.Address;
config.Token = options.AclClientToken;
});
return Task.CompletedTask;
}
public async Task<IList<Uri>> GetGateways()
{
var membershipTableData = await ConsulBasedMembershipTable.ReadAll(this.consulClient, this.clusterId, this.kvRootFolder, this.logger, null);
if (membershipTableData == null) return new List<Uri>();
return membershipTableData.Members.Select(e => e.Item1).
Where(m => m.Status == SiloStatus.Active && m.ProxyPort != 0).
Select(m =>
{
var endpoint = new IPEndPoint(m.SiloAddress.Endpoint.Address, m.ProxyPort);
var gatewayAddress = SiloAddress.New(endpoint, m.SiloAddress.Generation);
return gatewayAddress.ToGatewayUri();
}).ToList();
}
}
}