-
Notifications
You must be signed in to change notification settings - Fork 822
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement block on connect with Rust+Node.js SDK #953
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,6 +24,18 @@ class AgonesSDK { | |
this.emitters = []; | ||
} | ||
|
||
async connect() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should add a test for this to maintain code coverage. Please let me know if you'd like me to add, e.g. separate PR? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure how we are going to make a unit test for this, when there is nothing to connect to, but I will have a look, see if I can work out a way. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Aha, got it - there is a mocking library you use to mock out the client. On it! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done! PTAL. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should also update the docs. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yep - on my todo list. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done!. PTAL. |
||
return new Promise((resolve, reject) => { | ||
this.client.waitForReady(Date.now() + 30000, (error) => { | ||
if (error) { | ||
reject(error); | ||
} else { | ||
resolve(); | ||
} | ||
}) | ||
}); | ||
} | ||
|
||
async close() { | ||
if (this.healthStream !== undefined) { | ||
this.healthStream.destroy(); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,15 +12,18 @@ | |
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
use futures::{Future, Sink, Stream}; | ||
use grpcio; | ||
use std::sync::{Arc, Mutex}; | ||
use std::thread::sleep; | ||
use std::time::Duration; | ||
|
||
use futures::{Future, Sink, Stream}; | ||
use grpcio; | ||
use grpcio::CallOption; | ||
use protobuf::Message; | ||
|
||
use errors::*; | ||
use grpc::sdk; | ||
use grpc::sdk_grpc; | ||
use protobuf::Message; | ||
use types::*; | ||
|
||
const PORT: i32 = 59357; | ||
|
@@ -34,7 +37,7 @@ pub struct Sdk { | |
impl Sdk { | ||
/// Starts a new SDK instance, and connects to localhost on port 59357. | ||
/// Blocks until connection and handshake are made. | ||
/// Times out after 30 seconds. | ||
/// Times out after ~30 seconds. | ||
pub fn new() -> Result<Sdk> { | ||
let addr = format!("localhost:{}", PORT); | ||
let env = Arc::new(grpcio::EnvBuilder::new().build()); | ||
|
@@ -43,7 +46,24 @@ impl Sdk { | |
.connect(&addr); | ||
let cli = sdk_grpc::SdkClient::new(ch); | ||
let req = sdk::Empty::new(); | ||
let _ = cli.ready(&req).map(Box::new)?; | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @thara would love your review of my Rust code. I'm a little.... rusty 🙄 🤣 🦀 |
||
// 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; | ||
} | ||
} | ||
} | ||
|
||
let (sender, _) = cli.health()?; | ||
Ok(Sdk { | ||
client: Arc::new(cli), | ||
|
@@ -58,15 +78,13 @@ impl Sdk { | |
Ok(res) | ||
} | ||
|
||
|
||
/// Allocate the Game Server | ||
/// 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(); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# Copyright 2019 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. | ||
|
||
/target/ | ||
Cargo.lock |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@aLekSer wdyt of this approach?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, this one much better