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 pathProgram.cs
97 lines (88 loc) · 4.78 KB
/
Program.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
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using CommandLine;
using Grpc.Core;
using Improbable.OnlineServices.Base.Server;
using Improbable.OnlineServices.Base.Server.Interceptors;
using Improbable.OnlineServices.Common;
using Improbable.OnlineServices.Common.Analytics;
using Improbable.OnlineServices.Common.Analytics.ExceptionHandlers;
using Improbable.OnlineServices.Common.Interceptors;
using Improbable.OnlineServices.Proto.Invite;
using Improbable.OnlineServices.Proto.Party;
using Improbable.SpatialOS.Platform.Common;
using Improbable.SpatialOS.PlayerAuth.V2Alpha1;
using MemoryStore;
using MemoryStore.Redis;
using Mono.Unix;
using Mono.Unix.Native;
using Serilog;
using Serilog.Formatting.Compact;
using PartyDataModel = Improbable.OnlineServices.DataModel.Party.Party;
namespace Party
{
public class Program
{
private const string SpatialRefreshTokenEnvironmentVariable = "SPATIAL_REFRESH_TOKEN";
public static void Main(string[] args)
{
// See https://support.microsoft.com/en-gb/help/821268/contention-poor-performance-and-deadlocks-when-you-make-calls-to-web-s
// Experimentation shows we need the ThreadPool to always spin up threads for good performance under load
ThreadPool.GetMaxThreads(out var workerThreads, out var ioThreads);
ThreadPool.SetMinThreads(workerThreads, ioThreads);
Parser.Default.ParseArguments<PartyServerCommandLineArgs>(args)
.WithParsed(parsedArgs =>
{
parsedArgs.Validate();
var spatialRefreshToken = Secrets.GetEnvSecret(SpatialRefreshTokenEnvironmentVariable);
PartyDataModel.Defaults.MinMembers = (uint) parsedArgs.DefaultMinMembers;
PartyDataModel.Defaults.MaxMembers = (uint) parsedArgs.DefaultMaxMembers;
Log.Logger = new LoggerConfiguration()
.WriteTo.Console(new RenderedCompactJsonFormatter())
.Enrich.FromLogContext()
.CreateLogger();
IAnalyticsSender analyticsSender = new AnalyticsSenderBuilder("gateway_party")
.WithCommandLineArgs(parsedArgs)
.With(new LogExceptionStrategy(Log.Logger))
.Build();
using (var server = GrpcBaseServer.Build(parsedArgs))
using (var memoryStoreManager = new RedisClientManager(parsedArgs.RedisConnectionString))
{
Log.Information($"Successfully connected to Redis at {parsedArgs.RedisConnectionString}");
server.AddInterceptor(new PlayerIdentityTokenValidatingInterceptor(
PlayerAuthServiceClient.Create(credentials: new PlatformRefreshTokenCredential(spatialRefreshToken)),
memoryStoreManager.GetRawClient(Database.CACHE)
))
.AddInterceptor(new ExceptionMappingInterceptor(new Dictionary<Type, StatusCode>
{
{typeof(EntryNotFoundException), StatusCode.NotFound},
{typeof(EntryAlreadyExistsException), StatusCode.AlreadyExists},
{typeof(TransactionAbortedException), StatusCode.Unavailable}
}));
server.AddService(
PartyService.BindService(new PartyServiceImpl(memoryStoreManager, analyticsSender)));
server.AddService(
InviteService.BindService(new InviteServiceImpl(memoryStoreManager, analyticsSender)));
var serverTask = Task.Run(() => server.Start());
var signalTask = Task.Run(() => UnixSignal.WaitAny(new[] { new UnixSignal(Signum.SIGINT), new UnixSignal(Signum.SIGTERM) }));
Task.WaitAny(serverTask, signalTask);
if (signalTask.IsCompleted)
{
Log.Information($"Received UNIX signal {signalTask.Result}");
Log.Information("Server shutting down...");
server.Shutdown();
serverTask.Wait();
Log.Information("Server stopped cleanly");
}
else
{
/* The server task has completed; we can just exit. */
Log.Information("The Party server has stopped itself or encountered an unhandled exception.");
}
}
});
}
}
}