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
pub mod gameserver;
use std::{env, time::Duration};
use tonic::transport::Channel;
use crate::proto::api::{self, sdk_client::SdkClient};
#[cfg(feature = "player-tracking")]
use crate::proto::alpha::{self, sdk_client::SdkClient as AlphaClient};
pub use gameserver::GameServer;
use crate::errors::Result;
#[inline]
fn empty() -> api::Empty {
api::Empty {}
}
/// SDK is an instance of the Agones SDK
#[derive(Clone)]
pub struct Sdk {
client: SdkClient<Channel>,
#[cfg(feature = "player-tracking")]
alpha: AlphaClient<Channel>,
}
impl Sdk {
/// Starts a new SDK instance, and connects to localhost on the `port` specified
/// or else falls back to the `AGONES_SDK_GRPC_PORT` environment variable,
/// or defaults to 9357.
///
/// The `connect_timeout` applies to the time it takes to perform the initial
/// connection as well as the handshake with the agones sidecar.
///
/// # Errors
///
/// - The port specified in `AGONES_SDK_GRPC_PORT` can't be parsed as a `u16`.
/// - A connection cannot be established with an Agones SDK server
/// - The handshake takes longer than the specified `handshake_timeout` duration
pub async fn connect(
port: Option<u16>,
connect_timeout: Option<Duration>,
keep_alive: Option<Duration>,
) -> Result<(Self, GameServer)> {
let addr: http::Uri = format!(
"http://localhost:{}",
match port {
Some(port) => port,
None => {
match env::var("AGONES_SDK_GRPC_PORT") {
Ok(val) => val.parse().map_err(crate::Error::ParseInteger)?,
Err(_) => 9357,
}
}
}
)
.parse()?;
let builder = tonic::transport::channel::Channel::builder(addr)
.keep_alive_timeout(keep_alive.unwrap_or(Duration::from_secs(30)));
let (client, game_server, _channel) =
tokio::time::timeout(connect_timeout.unwrap_or(Duration::from_secs(30)), async {
let mut connect_interval = tokio::time::interval(Duration::from_millis(1));
let channel = loop {
connect_interval.tick().await;
// It would be nice to differentiate between transient errors
// (eg, agones sidecar is still initializing) and hard errors
// but tonic's error doesn't really allow good introspection
// so we just retry until we succeed or timeout
if let Ok(channel) = builder.connect().await {
break channel;
}
};
let mut client = SdkClient::new(channel.clone());
let game_server: GameServer = loop {
if let Ok(game_server) = client.get_game_server(empty()).await {
break game_server.into_inner().try_into()?;
}
connect_interval.tick().await;
};
Result::Ok((client, game_server, channel))
})
.await??;
#[cfg(feature = "player-tracking")]
let alpha = AlphaClient::new(_channel);
Ok((
Self {
client,
#[cfg(feature = "player-tracking")]
alpha,
},
game_server,
))
}
/// Marks the Game Server as ready to receive connections
#[inline]
pub async fn mark_ready(&mut self) -> Result<()> {
Ok(self.client.ready(empty()).await.map(|_| ())?)
}
/// Allocate the Game Server
#[inline]
pub async fn allocate(&mut self) -> Result<()> {
Ok(self.client.allocate(empty()).await.map(|_| ())?)
}
/// Marks the Game Server as ready to shutdown
#[inline]
pub async fn shutdown(&mut self) -> Result<()> {
Ok(self.client.shutdown(empty()).await.map(|_| ())?)
}
/// Returns a [`tokio::sync::mpsc::Sender`](https://docs.rs/tokio/1.10.0/tokio/sync/mpsc/struct.Sender.html)
/// that will emit a health check every time a message is sent on the channel.
pub fn health_check(&self) -> tokio::sync::mpsc::Sender<()> {
let mut health_client = self.clone();
let (tx, mut rx) = tokio::sync::mpsc::channel(10);
tokio::task::spawn(async move {
let health_stream = async_stream::stream! {
while rx.recv().await.is_some() {
yield empty();
}
};
let _ = health_client.client.health(health_stream).await;
});
tx
}
/// Set a [Label](https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/)
/// value on the backing Game Server record that is stored in Kubernetes
#[inline]
pub async fn set_label(
&mut self,
key: impl Into<String>,
value: impl Into<String>,
) -> Result<()> {
Ok(self
.client
.set_label(api::KeyValue {
key: key.into(),
value: value.into(),
})
.await
.map(|_| ())?)
}
/// Set an [Annotation](https://kubernetes.io/docs/concepts/overview/working-with-objects/annotations/)
/// value on the backing Game Server record that is stored in Kubernetes
#[inline]
pub async fn set_annotation(
&mut self,
key: impl Into<String>,
value: impl Into<String>,
) -> Result<()> {
Ok(self
.client
.set_annotation(api::KeyValue {
key: key.into(),
value: value.into(),
})
.await
.map(|_| ())?)
}
/// Returns most of the backing Game Server configuration and Status
#[inline]
pub async fn get_gameserver(&mut self) -> Result<GameServer> {
self.client
.get_game_server(empty())
.await
.map_err(crate::Error::from)
.and_then(|res| res.into_inner().try_into())
}
/// Reserve marks the Game Server as Reserved for a given duration, at which
/// point it will return the Game Server to a Ready state.
///
/// Note that the smallest reserve duration is 1 second and is limited to
/// second resolution.
#[inline]
pub async fn reserve(&mut self, duration: Duration) -> Result<()> {
Ok(self
.client
.reserve(api::Duration {
seconds: std::cmp::max(duration.as_secs() as i64, 1),
})
.await
.map(|_| ())?)
}
/// Watch the backing Game Server configuration on updated
pub async fn watch_gameserver(
&mut self,
) -> Result<impl futures_util::Stream<Item = Result<GameServer>>> {
use futures_util::stream::StreamExt;
Ok(self.client.watch_game_server(empty()).await.map(|stream| {
stream.into_inner().map(|res| {
res.map_err(crate::Error::from)
.and_then(|ogs| ogs.try_into())
})
})?)
}
}
#[cfg(feature = "player-tracking")]
impl Sdk {
/// This returns the last player capacity that was set through the SDK.
/// If the player capacity is set from outside the SDK, use
/// [`Sdk::get_gameserver`] instead.
#[inline]
pub async fn get_player_capacity(&mut self) -> Result<u64> {
Ok(self
.alpha
.get_player_capacity(alpha::Empty {})
.await
.map(|c| c.into_inner().count as u64)?)
}
/// This changes the player capacity to a new value.
#[inline]
pub async fn set_player_capacity(&mut self, count: u64) -> Result<()> {
Ok(self
.alpha
.set_player_capacity(alpha::Count {
count: count as i64,
})
.await
.map(|_| ())?)
}
/// This function increases the SDK’s stored player count by one, and appends
/// this player id to `GameServer.status.players.ids`.
///
/// Returns true and adds the player id to the list of player ids if it
/// was not already present.
#[inline]
pub async fn player_connect(&mut self, id: impl Into<String>) -> Result<bool> {
Ok(self
.alpha
.player_connect(alpha::PlayerId {
player_id: id.into(),
})
.await
.map(|b| b.into_inner().bool)?)
}
/// This function decreases the SDK’s stored player count by one, and removes
/// the player id from `GameServer.status.players.ids`.
///
/// Will return true and remove the supplied player id from the list of
/// connected player ids if the player id exists within the list.
#[inline]
pub async fn player_disconnect(&mut self, id: impl Into<String>) -> Result<bool> {
Ok(self
.alpha
.player_disconnect(alpha::PlayerId {
player_id: id.into(),
})
.await
.map(|b| b.into_inner().bool)?)
}
/// Returns the current player count.
#[inline]
pub async fn get_player_count(&mut self) -> Result<u64> {
Ok(self
.alpha
.get_player_count(alpha::Empty {})
.await
.map(|c| c.into_inner().count as u64)?)
}
/// Returns whether the player id is currently connected to the Game Server.
/// This is always accurate, even if the value hasn’t been updated to the
/// Game Server status yet.
#[inline]
pub async fn is_player_connected(&mut self, id: impl Into<String>) -> Result<bool> {
Ok(self
.alpha
.is_player_connected(alpha::PlayerId {
player_id: id.into(),
})
.await
.map(|b| b.into_inner().bool)?)
}
/// Returns the list of the currently connected player ids.
/// This is always accurate, even if the value has not been updated to the
/// Game Server status yet.
#[inline]
pub async fn get_connected_players(&mut self) -> Result<Vec<String>> {
Ok(self
.alpha
.get_connected_players(alpha::Empty {})
.await
.map(|pl| pl.into_inner().list)?)
}
}