/
sdk.rs
360 lines (325 loc) · 12.2 KB
/
sdk.rs
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
// Copyright 2018 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.
use std::env;
use std::sync::{Arc, Mutex};
use std::thread::sleep;
use std::time::Duration;
use futures::future::TryFutureExt;
use futures::prelude::*;
use futures::FutureExt;
use futures01::future::Future;
use grpcio;
use protobuf::Message;
use super::alpha::*;
use super::errors::*;
use super::grpc::sdk;
use super::grpc::sdk_grpc;
use super::types::*;
/// SDK is an instance of the Agones SDK
pub struct Sdk {
client: Arc<sdk_grpc::SdkClient>,
health: Arc<Mutex<Option<grpcio::ClientCStreamSender<sdk::Empty>>>>,
health_receiver: Arc<Mutex<Option<grpcio::ClientCStreamReceiver<sdk::Empty>>>>,
alpha: Arc<Alpha>,
}
impl Sdk {
/// Starts a new SDK instance, and connects to localhost on port 9357.
/// Blocks until connection and handshake are made.
/// Times out after ~30 seconds.
pub fn new() -> Result<Sdk> {
let port = env::var("AGONES_SDK_GRPC_PORT").unwrap_or("9357".to_string());
let addr = format!("localhost:{}", port);
let env = Arc::new(grpcio::EnvBuilder::new().build());
let ch = grpcio::ChannelBuilder::new(env)
.keepalive_timeout(Duration::new(30, 0))
.connect(&addr);
let cli = sdk_grpc::SdkClient::new(ch.clone());
let alpha = Alpha::new(ch);
let req = sdk::Empty::new();
// Unfortunately there isn't a native way to block until connected
// so we had to roll our own.
let mut counter = 0;
loop {
counter += 1;
match cli.get_game_server(&req) {
Ok(_) => break,
Err(e) => {
if counter > 30 {
return Err(ErrorKind::Grpc(e).into());
}
sleep(Duration::from_secs(1));
continue;
}
}
}
// Keep both sender and receiver as RPC is canceled when sender or receiver is dropped
let (sender, receiver) = cli.health()?;
Ok(Sdk {
client: Arc::new(cli),
health: Arc::new(Mutex::new(Some(sender))),
health_receiver: Arc::new(Mutex::new(Some(receiver))),
alpha: Arc::new(alpha),
})
}
/// Alpha returns the Alpha SDK
pub fn alpha(&self) -> &Arc<Alpha> {
&self.alpha
}
/// Marks the Game Server as ready to receive connections
pub fn ready(&self) -> Result<()> {
let req = sdk::Empty::default_instance();
let res = self.client.ready(req).map(|_| ())?;
Ok(res)
}
/// Allocate the Game Server
pub fn allocate(&self) -> Result<()> {
let req = sdk::Empty::default_instance();
let res = self.client.allocate(req).map(|_| ())?;
Ok(res)
}
/// Marks the Game Server as ready to shutdown
pub fn shutdown(&self) -> Result<()> {
let req = sdk::Empty::default_instance();
let res = self.client.shutdown(req).map(|_| ())?;
Ok(res)
}
/// Sends a ping to the health check to indicate that this server is healthy
pub fn health(mut self) -> (Self, Result<()>) {
// Avoid `cannot move out of borrowed content` compile error for self.health
let h = self.health.lock().unwrap().take();
if h.is_none() {
return (
self,
Err(ErrorKind::HealthPingConnectionFailure(
"failed to hold client stream for health ping".to_string(),
)
.into()),
);
}
let mut h = h.unwrap();
let req = sdk::Empty::new();
let result = h
.send((req, grpcio::WriteFlags::default()))
.boxed_local()
.compat()
.wait();
match result {
Ok(_) => {
self.health = Arc::new(Mutex::new(Some(h)));
(self, Ok(()))
}
Err(e) => (self, Err(ErrorKind::Grpc(e).into())),
}
}
/// Set a Label value on the backing GameServer record that is stored in Kubernetes
pub fn set_label<S>(&self, key: S, value: S) -> Result<()>
where
S: Into<String>,
{
let mut kv = sdk::KeyValue::new();
kv.set_key(key.into());
kv.set_value(value.into());
let res = self.client.set_label(&kv).map(|_| ())?;
Ok(res)
}
/// Set a Annotation value on the backing Gameserver record that is stored in Kubernetes
pub fn set_annotation<S>(&self, key: S, value: S) -> Result<()>
where
S: Into<String>,
{
let mut kv = sdk::KeyValue::new();
kv.set_key(key.into());
kv.set_value(value.into());
let res = self.client.set_annotation(&kv).map(|_| ())?;
Ok(res)
}
/// Returns most of the backing GameServer configuration and Status
pub fn get_gameserver(&self) -> Result<GameServer> {
let req = sdk::Empty::new();
let res = self
.client
.get_game_server(&req)
.map(|e| GameServer::from_message(e))?;
Ok(res)
}
/// Reserve marks the Game Server as Reserved for a given duration, at which point
/// it will return the GameServer to a Ready state.
/// Do note, the smallest unit available in the time.Duration argument is a second.
pub fn reserve(&self, duration: Duration) -> Result<()> {
let mut d = sdk::Duration::new();
d.set_seconds(duration.as_secs() as i64);
let res = self.client.reserve(&d).map(|_| ())?;
Ok(res)
}
/// Watch the backing GameServer configuration on updated
pub fn watch_gameserver<F>(&self, mut watcher: F) -> Result<()>
where
F: FnMut(GameServer) -> (),
{
let req = sdk::Empty::new();
let mut receiver = self.client.watch_game_server(&req)?;
loop {
let res = receiver.try_next().boxed_local().compat().wait();
match res {
Ok(res) => match res {
Some(res) => watcher(GameServer::from_message(res)),
None => break,
},
Err(e) => {
return Err(e.into());
}
}
}
Ok(())
}
/// Starts a new SDK instance, and connects to localhost on port 9357.
/// Blocks until connection and handshake are made.
/// Times out after ~30 seconds.
pub async fn new_async() -> Result<Sdk> {
let port = env::var("AGONES_SDK_GRPC_PORT").unwrap_or("9357".to_string());
let addr = format!("localhost:{}", port);
let env = Arc::new(grpcio::EnvBuilder::new().build());
let ch = grpcio::ChannelBuilder::new(env)
.keepalive_timeout(Duration::new(30, 0))
.connect(&addr);
let cli = sdk_grpc::SdkClient::new(ch.clone());
let alpha = Alpha::new(ch);
let req = sdk::Empty::new();
// Unfortunately there isn't a native way to block until connected
// so we had to roll our own.
let mut counter = 0;
loop {
counter += 1;
match cli.get_game_server_async(&req)?.await {
Ok(_) => break,
Err(e) => {
if counter > 30 {
return Err(ErrorKind::Grpc(e).into());
}
sleep(Duration::from_secs(1));
continue;
}
}
}
// Keep both sender and receiver as RPC is canceled when sender or receiver is dropped
let (sender, receiver) = cli.health()?;
Ok(Sdk {
client: Arc::new(cli),
health: Arc::new(Mutex::new(Some(sender))),
health_receiver: Arc::new(Mutex::new(Some(receiver))),
alpha: Arc::new(alpha),
})
}
/// Marks the Game Server as ready to receive connections
pub async fn ready_async(&self) -> Result<()> {
let req = sdk::Empty::default_instance();
let res = self.client.ready_async(req)?.await.map(|_| ())?;
Ok(res)
}
/// Allocate the Game Server
pub async fn allocate_async(&self) -> Result<()> {
let req = sdk::Empty::default_instance();
let res = self.client.allocate_async(req)?.await.map(|_| ())?;
Ok(res)
}
/// Marks the Game Server as ready to shutdown
pub async fn shutdown_async(&self) -> Result<()> {
let req = sdk::Empty::default_instance();
let res = self.client.shutdown_async(req)?.await.map(|_| ())?;
Ok(res)
}
/// Sends a ping to the health check to indicate that this server is healthy
pub async fn health_async(&mut self) -> Result<()> {
// Avoid `cannot move out of borrowed content` compile error for self.health
let h = self.health.lock().unwrap().take();
if h.is_none() {
return Err(ErrorKind::HealthPingConnectionFailure(
"failed to hold client stream for health ping".to_string(),
)
.into());
}
let mut h = h.unwrap();
let req = sdk::Empty::new();
match h.send((req, grpcio::WriteFlags::default())).await {
Ok(_) => {
self.health = Arc::new(Mutex::new(Some(h)));
Ok(())
}
Err(e) => Err(ErrorKind::Grpc(e).into()),
}
}
/// Set a Label value on the backing GameServer record that is stored in Kubernetes with the prefix 'agones.dev/sdk-'
pub async fn set_label_async<S>(&self, key: S, value: S) -> Result<()>
where
S: Into<String>,
{
let mut kv = sdk::KeyValue::new();
kv.set_key(key.into());
kv.set_value(value.into());
let res = self.client.set_label_async(&kv)?.await.map(|_| ())?;
Ok(res)
}
/// Set a Annotation value on the backing Gameserver record that is stored in Kubernetes with the prefix 'agones.dev/sdk-'
pub async fn set_annotation_async<S>(&self, key: S, value: S) -> Result<()>
where
S: Into<String>,
{
let mut kv = sdk::KeyValue::new();
kv.set_key(key.into());
kv.set_value(value.into());
let res = self.client.set_annotation_async(&kv)?.await.map(|_| ())?;
Ok(res)
}
/// Returns most of the backing GameServer configuration and Status
pub async fn get_gameserver_async(&self) -> Result<GameServer> {
let req = sdk::Empty::new();
let res = self
.client
.get_game_server_async(&req)?
.await
.map(|e| GameServer::from_message(e))?;
Ok(res)
}
/// Reserve marks the Game Server as Reserved for a given duration, at which point
/// it will return the GameServer to a Ready state.
/// Do note, the smallest unit available in the time.Duration argument is a second.
pub async fn reserve_async(&self, duration: Duration) -> Result<()> {
let mut d = sdk::Duration::new();
d.set_seconds(duration.as_secs() as i64);
let res = self.client.reserve_async(&d)?.await.map(|_| ())?;
Ok(res)
}
/// Watch the backing GameServer configuration on updated
pub async fn watch_gameserver_async<F>(&self, mut watcher: F) -> Result<()>
where
F: FnMut(GameServer) -> (),
{
let req = sdk::Empty::new();
let mut receiver = self.client.watch_game_server(&req)?;
while let Some(e) = receiver.try_next().await? {
watcher(GameServer::from_message(e));
}
Ok(())
}
}
impl Clone for Sdk {
fn clone(&self) -> Self {
Self {
client: Arc::clone(&self.client),
health: self.health.clone(),
health_receiver: self.health_receiver.clone(),
alpha: Arc::clone(&self.alpha),
}
}
}