forked from asynkron/protoactor-dotnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHostedGrpcNetRemote.cs
87 lines (79 loc) · 2.87 KB
/
HostedGrpcNetRemote.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
76
77
78
79
80
81
82
83
84
85
86
87
using System;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting.Server.Features;
using Microsoft.Extensions.Logging;
using Proto.Remote.Metrics;
namespace Proto.Remote.GrpcNet
{
public class HostedGrpcNetRemote : IRemote
{
private readonly object _lock = new();
private readonly GrpcNetRemoteConfig _config;
private readonly EndpointManager _endpointManager;
private readonly ILogger _logger;
public HostedGrpcNetRemote(
ActorSystem system,
GrpcNetRemoteConfig config,
EndpointManager endpointManager,
ILogger<HostedGrpcNetRemote> logger
)
{
System = system;
_config = config;
system.Metrics.Register(new RemoteMetrics(system.Metrics));
_endpointManager = endpointManager;
_logger = logger;
System.Extensions.Register(this);
System.Extensions.Register(config.Serialization);
}
public IServerAddressesFeature? ServerAddressesFeature { get; set; }
public RemoteConfigBase Config => _config;
public ActorSystem System { get; }
public bool Started { get; private set; }
public Task StartAsync()
{
lock (_lock)
{
if (Started)
return Task.CompletedTask;
var uri = ServerAddressesFeature?.Addresses.Select(address => new Uri(address)).FirstOrDefault();
var boundPort = uri?.Port ?? Config.Port;
var host = uri?.Host ?? Config.Host;
System.SetAddress(Config.AdvertisedHost ?? host,
Config.AdvertisedPort ?? boundPort
);
_endpointManager.Start();
_logger.LogInformation("Starting Proto.Actor server on {Host}:{Port} ({Address})", host, boundPort, System.Address);
Started = true;
return Task.CompletedTask;
}
}
public Task ShutdownAsync(bool graceful = true)
{
lock (_lock)
{
if (!Started)
return Task.CompletedTask;
try
{
_endpointManager.Stop();
_logger.LogInformation(
"Proto.Actor server stopped on {Address}. Graceful: {Graceful}",
System.Address, graceful
);
}
catch (Exception ex)
{
_logger.LogError(
ex, "Proto.Actor server stopped on {Address} with error: {Message}",
System.Address, ex.Message
);
throw;
}
Started = false;
return Task.CompletedTask;
}
}
}
}