Skip to content

Commit

Permalink
Fix CrashLoopBackOff in rust-sample
Browse files Browse the repository at this point in the history
  • Loading branch information
thara authored Jun 8, 2018
1 parent 0bdf438 commit 54c51e7
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 15 deletions.
1 change: 0 additions & 1 deletion examples/rust-simple/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,3 @@ version = "0.1.0"

[dependencies]
agones = { path = "../../sdks/rust" }
grpcio = "0.2.2"
27 changes: 21 additions & 6 deletions examples/rust-simple/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,25 @@ use std::time::Duration;
macro_rules! enclose {
( ($( $x:ident ),*) $y:expr ) => {
{
$(let $x = $x.clone();)*
$(let mut $x = $x.clone();)*
$y
}
};
}

fn main() {
println!("Rust Game Server has started!");
let _ = run();

::std::process::exit(match run() {
Ok(_) => {
println!("Rust Game Server finished.");
0
},
Err(msg) => {
println!("{}", msg);
1
}
});
}

fn run() -> Result<(), String>{
Expand All @@ -25,10 +35,15 @@ fn run() -> Result<(), String>{

let _t = thread::spawn(enclose!{(sdk) move || {
loop {
if sdk.health().is_err() {
println!("Health ping failed");
} else {
println!("Health ping sent");
match sdk.health() {
(s, Ok(_)) => {
println!("Health ping sent");
sdk = s;
},
(s, Err(e)) => {
println!("Health ping failed : {:?}", e);
sdk = s;
}
}
thread::sleep(Duration::from_secs(2));
}
Expand Down
7 changes: 7 additions & 0 deletions sdks/rust/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,11 @@ error_chain!{
foreign_links {
Grpc(::grpcio::Error);
}

errors {
HealthPingConnectionFailure(t: String) {
description("health ping connection failure"),
display("health ping connection failure: '{}'", t),
}
}
}
35 changes: 27 additions & 8 deletions sdks/rust/src/sdk.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::sync::Arc;
use std::sync::{Arc, Mutex};
use std::time::Duration;
use grpcio::{ChannelBuilder, EnvBuilder};
use grpcio;
use futures::{Future, Sink};

use errors::*;
use grpc::sdk as sdk;
Expand All @@ -12,6 +13,7 @@ const PORT: i32 = 59357;
/// SDK is an instance of the Agones SDK
pub struct Sdk {
client : Arc<sdk_grpc::SdkClient>,
health : Arc<Mutex<Option<grpcio::ClientCStreamSender<sdk::Empty>>>>,
}

impl Sdk {
Expand All @@ -21,12 +23,13 @@ impl Sdk {
/// Times out after 30 seconds.
pub fn new() -> Result<Sdk> {
let addr = format!("localhost:{}", PORT);
let env = Arc::new(EnvBuilder::new().build());
let ch = ChannelBuilder::new(env).keepalive_timeout(Duration::new(30, 0)).connect(&addr);
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);
let req = sdk::Empty::new();
let _ = cli.ready(&req).map(Box::new)?;
Ok(Sdk{client: Arc::new(cli)})
let (sender, _) = cli.health()?;
Ok(Sdk{client: Arc::new(cli), health: Arc::new(Mutex::new(Some(sender)))})
}

/// Marks the Game Server as ready to receive connections
Expand All @@ -44,16 +47,32 @@ impl Sdk {
}

/// Sends a ping to the health check to indicate that this server is healthy
pub fn health(&self) -> Result<()> {
let res = self.client.health().map(|_| ())?;
Ok(res)
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 h : grpcio::ClientCStreamSender<sdk::Empty> = h.unwrap().into();

let req = sdk::Empty::new();
match h.send((req, grpcio::WriteFlags::default())).wait() {
Ok(h) => {
self.health = Arc::new(Mutex::new(Some(h)));
(self, Ok(()))
},
Err(e) => {
(self, Err(ErrorKind::Grpc(e).into()))
},
}
}
}

impl Clone for Sdk {
fn clone(&self) -> Self {
Self {
client: Arc::clone(&self.client),
health: self.health.clone(),
}
}
}

0 comments on commit 54c51e7

Please sign in to comment.