-
Notifications
You must be signed in to change notification settings - Fork 820
/
AgonesSDK.cs
309 lines (287 loc) · 9.94 KB
/
AgonesSDK.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
// Copyright 2020 Google LLC All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
using System;
using System.Threading;
using System.Threading.Tasks;
using Grpc.Core;
using Agones.Dev.Sdk;
[assembly: System.Runtime.CompilerServices.InternalsVisibleTo("Agones.Test")]
namespace Agones
{
public sealed class AgonesSDK : IDisposable
{
public string Host { get; } = Environment.GetEnvironmentVariable("AGONES_SDK_GRPC_HOST") ?? "localhost";
public int Port { get; } = Convert.ToInt32((Environment.GetEnvironmentVariable("AGONES_SDK_GRPC_PORT") ?? "9357"));
/// <summary>
/// The timeout for gRPC calls.
/// </summary>
public double RequestTimeout { get; set; }
/// <summary>
/// Interval between Health pings in seconds.
/// </summary>
public int HealthInterval { get; set; }
/// <summary>
/// Set to true when you want health pings to occur periodically.
/// </summary>
public bool HealthEnabled { get; set; }
internal SDK.SDKClient client;
internal Channel channel;
internal IClientStreamWriter<Empty> healthStream;
internal CancellationTokenSource cts = new CancellationTokenSource();
internal CancellationToken ctoken => cts.Token;
internal volatile bool isWatchingGameServer = false;
internal AsyncServerStreamingCall<GameServer> watchStreamingCall;
/// <summary>
/// Fired everytime the GameServer k8s data is updated.
/// A more efficient way to emulate WatchGameServer behavior
/// without starting a new task for every subscrbtion request.
/// </summary>
private event Action<GameServer> GameServerUpdated;
internal Delegate[] GameServerUpdatedCallbacks => GameServerUpdated.GetInvocationList();
public AgonesSDK(int healthInterval = 5, bool healthEnabled = true, double requestTimeout = 15)
{
this.HealthInterval = healthInterval;
this.HealthEnabled = healthEnabled;
this.RequestTimeout = requestTimeout;
this.channel = new Channel(Host, Port, ChannelCredentials.Insecure);
this.client = new SDK.SDKClient(channel);
this.healthStream = client.Health().RequestStream;
}
/// <summary>
/// Connect the underlying gRPC channel.
/// </summary>
/// <returns>True if successful</returns>
public async Task<bool> ConnectAsync()
{
await channel.ConnectAsync(DateTime.UtcNow.AddSeconds(30));
if (channel.State != ChannelState.Ready){
Console.Error.WriteLine($"Could not connect to the sidecar at {Host}:{Port}. Exited with connection state: {channel.State}");
return false;
}
return true;
}
/// <summary>
/// Tells Agones that the Game Server is ready to take player connections
/// </summary>
/// <returns>gRPC Status of the request</returns>
public async Task<Status> ReadyAsync()
{
try
{
await client.ReadyAsync(new Empty(), deadline: DateTime.UtcNow.AddSeconds(RequestTimeout), cancellationToken: ctoken);
return new Status(StatusCode.OK, "Ready request successful.");
}
catch (RpcException ex)
{
Console.Error.WriteLine(ex.Message);
return ex.Status;
}
}
/// <summary>
/// Marks the game server as Allocated.
/// </summary>
/// <returns>gRPC Status of the request</returns>
public async Task<Status> AllocateAsync()
{
try
{
await client.AllocateAsync(new Empty(), deadline: DateTime.UtcNow.AddSeconds(RequestTimeout), cancellationToken: ctoken);
return new Status(StatusCode.OK, "Allocate request successful.");
}
catch (RpcException ex)
{
Console.Error.WriteLine(ex.Message);
return ex.Status;
}
}
/// <summary>
/// Reserve(seconds) will move the GameServer into the Reserved state for the specified number of seconds (0 is forever),
/// and then it will be moved back to Ready state. While in Reserved state,
/// the GameServer will not be deleted on scale down or Fleet update, and also it could not be Allocated using GameServerAllocation.
/// </summary>
/// <param name="seconds">Amount of seconds to reserve.</param>
/// <returns>gRPC Status of the request</returns>
public async Task<Status> ReserveAsync(long seconds)
{
try
{
await client.ReserveAsync(new Duration() {
Seconds = seconds
}, deadline: DateTime.UtcNow.AddSeconds(RequestTimeout), cancellationToken: ctoken);
return new Status(StatusCode.OK, $"Reserve({seconds}) request successful.");
}
catch (RpcException ex)
{
Console.Error.WriteLine(ex.Message);
return ex.Status;
}
}
/// <summary>
/// This returns most of the backing GameServer configuration and Status.
/// This can be useful for instances where you may want to know Health check configuration, or the IP and Port the GameServer is currently allocated to.
/// </summary>
/// <returns>A GameServer object containing this GameServer's configuration data</returns>
public async Task<GameServer> GetGameServerAsync()
{
try
{
return await client.GetGameServerAsync(new Empty(), deadline: DateTime.UtcNow.AddSeconds(RequestTimeout), cancellationToken: ctoken);
}
catch (RpcException ex)
{
Console.Error.WriteLine(ex.Message);
//Should I rethrow the exception?
return null;
}
}
/// <summary>
/// Starts watching the GameServer updates in the background in it's own task.
/// On update, it fires the GameServerUpdate event.
/// </summary>
private async Task BeginInternalWatch()
{
//Begin WatchGameServer in the background for the provided callback.
try
{
using (watchStreamingCall = client.WatchGameServer(new Empty(), cancellationToken: ctoken))
{
var reader = watchStreamingCall.ResponseStream;
while (await reader.MoveNext(ctoken))
{
GameServerUpdated?.Invoke(reader.Current);
}
}
}
catch (RpcException ex)
{
Console.Error.WriteLine(ex.Message);
throw ex;
}
}
/// <summary>
/// This executes the passed in callback with the current GameServer details whenever the underlying GameServer configuration is updated.
/// This can be useful to track GameServer > Status > State changes, metadata changes, such as labels and annotations, and more.
/// </summary>
/// <param name="callback">The action to be called when the underlying GameServer metadata changes.</param>
public void WatchGameServer(Action<GameServer> callback)
{
GameServerUpdated += callback;
if (!isWatchingGameServer)
{
isWatchingGameServer = true;
//Ignoring this warning as design is intentional.
#pragma warning disable 4014
BeginInternalWatch().ContinueWith((t)=> { this.Dispose(); }, TaskContinuationOptions.OnlyOnFaulted);
#pragma warning restore 4014
}
}
/// <summary>
/// Cancels all running tasks & tells Agones to shut down the currently running game server.
/// </summary>
/// <returns>gRPC Status of the request</returns>
public async Task<Status> ShutDownAsync()
{
try
{
await client.ShutdownAsync(new Empty(), deadline: DateTime.UtcNow.AddSeconds(RequestTimeout));
return new Status(StatusCode.OK, "Shutdown request successful.");
}
catch (RpcException ex)
{
Console.Error.WriteLine(ex.Message);
return ex.Status;
}
}
/// <summary>
/// Set a Label value on the backing GameServer record that is stored in Kubernetes.
/// </summary>
/// <param name="key">Label key</param>
/// <param name="value">Label value</param>
/// <returns>gRPC Status of the request</returns>
public async Task<Status> SetLabelAsync(string key, string value)
{
try
{
await client.SetLabelAsync(new KeyValue()
{
Key = key,
Value = value
}, deadline: DateTime.UtcNow.AddSeconds(RequestTimeout), cancellationToken: ctoken);
return new Status(StatusCode.OK, $"SetLabel {key}:{value} request successful.");
}
catch(RpcException ex)
{
Console.Error.WriteLine(ex.Message);
return ex.Status;
}
}
/// <summary>
/// Set a Annotation value on the backing Gameserver record that is stored in Kubernetes.
/// </summary>
/// <param name="key">Annotation key</param>
/// <param name="value">Annotation value</param>
/// <returns>gRPC Status of the request</returns>
public async Task<Status> SetAnnotationAsync(string key, string value)
{
try
{
await client.SetAnnotationAsync(new KeyValue()
{
Key = key,
Value = value
}, deadline: DateTime.UtcNow.AddSeconds(RequestTimeout), cancellationToken: ctoken);
return new Status(StatusCode.OK, $"SetAnnotation {key}:{value} request successful.");
}
catch (RpcException ex)
{
Console.Error.WriteLine(ex.Message);
return ex.Status;
}
}
/// <summary>
/// Sends a single ping to designate that the Game Server is alive and healthy.
/// </summary>
/// <returns>gRPC Status of the request</returns>
internal async Task<Status> HealthAsync()
{
try
{
await healthStream.WriteAsync(new Empty());
return new Status(StatusCode.OK, "Health ping successful.");
}
catch (RpcException ex)
{
Console.Error.WriteLine(ex.Message);
return ex.Status;
}
}
private async Task HealthCheckLoop()
{
while(!cts.IsCancellationRequested)
{
if(HealthEnabled)
await HealthAsync();
await Task.Delay(HealthInterval * 1000);
}
}
public void Dispose()
{
cts.Cancel();
}
~AgonesSDK()
{
this.Dispose();
}
}
}