Code Monkey home page Code Monkey logo

Comments (5)

ysimonson avatar ysimonson commented on June 30, 2024 1

Yes, that did it, thank you @overvenus! If anyone else gets stuck on this, this is what I ended up with:

fn transaction(&self, ctx: grpcio::RpcContext, stream: grpcio::RequestStream<request::TransactionRequest>, mut sink: grpcio::DuplexSink<response::TransactionResponse>) {
    let datastore = self.datastore.clone();
    let trans = datastore.transaction().unwrap();

    for result in stream.wait() {
        let response = match result {
            Ok(request) => build_response(&trans, &request).unwrap_or_else(|err| build_error_response(&err)),
            Err(err) => build_error_response(&err)
        };

        sink = sink.send(response).wait().unwrap();
    }
}

(With the usual caveat that unwrap is only used here to make the example more terse.)

from grpc-rs.

siddontang avatar siddontang commented on June 30, 2024

seem you can use future wait to act as a sync call.

from grpc-rs.

ysimonson avatar ysimonson commented on June 30, 2024

Here's the best I could chalk up with that in mind:

fn transaction(&self, ctx: grpcio::RpcContext, stream: grpcio::RequestStream<request::TransactionRequest>, sink: grpcio::DuplexSink<response::TransactionResponse>) {
    let datastore = self.datastore.clone();
    let trans = datastore.transaction().unwrap();

    for result in stream.wait() {
        match result {
            Ok(request) => {   
                let response = match build_response(&trans, request) {
                    Ok(response) => response,
                    Err(err) => build_error_response(&err)
                };

                sink.send((response, grpcio::WriteFlags::default()));
            }
            Err(err) => {
                let response = build_error_response(&err);
                sink.send((response, grpcio::WriteFlags::default()));
            }
        }
    }
}

This doesn't work because grpcio::DuplexSink::send consumes self. Also, I'm guessing grpcio doesn't setup a thread per request, so wouldn't this bottleneck request anyways?

(For context, datastore.transaction() wraps a postgresql transaction, which is not Sync.)

from grpc-rs.

siddontang avatar siddontang commented on June 30, 2024

/cc @BusyJay

from grpc-rs.

overvenus avatar overvenus commented on June 30, 2024

Hi @ysimonson

Is there a way to use grpc-rs without futures, i.e. just synchronously?

Yes, just as @siddontang s suggestion.

I ask because I'm dealing with !Sync types, and it's not clear to me whether it's possible to use them with grpcio::RpcContext::spawn.

Yes, it can, e.g.,

diff --git a/tests/cases/alarm.rs b/tests/cases/alarm.rs
index 53d8009..01ed67e 100644
--- a/tests/cases/alarm.rs
+++ b/tests/cases/alarm.rs
@@ -27,6 +27,7 @@ struct GreeterService {
 
 impl Greeter for GreeterService {
     fn say_hello(&self, ctx: RpcContext, mut req: HelloRequest, sink: UnarySink<HelloReply>) {
+        let mut c = ::std::cell::Cell::new(1); // <- It's `!Sync`.
         let (tx, rx) = oneshot::channel();
         let tx_lock = self.tx.clone();
         let name = req.take_name();
@@ -38,6 +39,7 @@ impl Greeter for GreeterService {
             .and_then(move |(greet, _)| {
                 let mut resp = HelloReply::new();
                 resp.set_message(format!("{} {}", greet, name));
+                *c.get_mut() += 1;
                 sink.success(resp)
                     .map_err(|e| panic!("failed to reply {:?}", e))
             });

You can apply this diff and cargo test --all.

This doesn't work because grpcio::DuplexSink::send consumes self.

It does consumes self, but also returns Self. I think you can:

let sink = sink.send(...).wait().unwrap();

from grpc-rs.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.