This repository has been archived by the owner on Nov 3, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathGrpcBaseServer.cs
114 lines (101 loc) · 3.56 KB
/
GrpcBaseServer.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
using System;
using System.Collections.Generic;
using System.IO;
using Grpc.Core;
using Grpc.Core.Interceptors;
using Improbable.OnlineServices.Base.Server.Interceptors;
using PrometheusServerInterceptor = NetGrpcPrometheus.ServerInterceptor;
using Improbable.OnlineServices.Base.Server.Logging;
namespace Improbable.OnlineServices.Base.Server
{
public class GrpcBaseServer : IDisposable
{
private static readonly ILog Logger = LogProvider.GetCurrentClassLogger();
private readonly Grpc.Core.Server _server;
private readonly List<Interceptor> _interceptorChain = new List<Interceptor>();
public static GrpcBaseServer Build(CommandLineArgs args)
{
Logger.Info(args.ToString);
return new GrpcBaseServer(
args.HostName, args.SslCertChainPath, args.SslPrivateKeyPath,
args.GrpcServicePort, args.MetricsPort, args.DisablePrometheus
);
}
private GrpcBaseServer(
string host, string sslCertChainPath, string sslPrivateKeyPath,
int port, int metricsPort, bool disablePrometheusInterceptor
)
{
var credentials = ServerCredentials.Insecure;
if (string.IsNullOrEmpty(sslCertChainPath) || string.IsNullOrEmpty(sslPrivateKeyPath))
{
Logger.Warn($"SSL certificate chain and/or private key paths were not provided. " +
"Server will start in insecure non-TLS mode.");
}
else
{
credentials = new SslServerCredentials(new[]
{
new KeyCertificatePair(File.ReadAllText(sslCertChainPath), File.ReadAllText(sslPrivateKeyPath))
});
}
// Build a server
_server = new Grpc.Core.Server
{
Ports = { new ServerPort(host, port, credentials) }
};
if (!disablePrometheusInterceptor)
{
try
{
_interceptorChain.Add(new PrometheusServerInterceptor("*", metricsPort, true, true));
}
catch (Exception e)
{
Console.WriteLine("Exception creating prometheus interceptor: " + e);
}
}
}
public GrpcBaseServer AddInterceptor(Interceptor interceptor)
{
_interceptorChain.Add(interceptor);
return this;
}
public void AddService(ServerServiceDefinition serviceDefinition)
{
_server.Services.Add(_addInterceptors(serviceDefinition));
}
public void Start()
{
Logger.Info("Starting Server...");
_server.Start();
_server.ShutdownTask.Wait();
}
public void Shutdown()
{
Logger.Info("Shutdown server...");
_server.ShutdownAsync().Wait();
Logger.Info("Shutdown cleanly.");
}
private ServerServiceDefinition _addInterceptors(ServerServiceDefinition service)
{
foreach (var interceptor in _interceptorChain)
{
service = service.Intercept(interceptor);
}
return service
.Intercept(new LoggingInterceptor());
}
public void Dispose()
{
try
{
Shutdown();
}
catch (Exception ex)
{
Logger.Warn("Exception while shutting down: " + ex);
}
}
}
}