Skip to content
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

Default service generator? #1105

Open
victorbnl opened this issue Jul 19, 2024 · 3 comments
Open

Default service generator? #1105

victorbnl opened this issue Jul 19, 2024 · 3 comments

Comments

@victorbnl
Copy link

service SearchService {
  rpc Search(SearchRequest) returns (SearchResponse);
}

It would be nice to have a default service generator that, for the previous service, generates the following trait.

trait SearchService {
  fn search(search_request: SearchRequest) -> SearchResponse;
}

That way users could use it as follows.

struct MySearchService;

impl SearchService for MySearchService {
  fn search(search_request: SearchRequest) -> SearchResponse {
    println!(search_request);
    SearchResponse { ... }
  }
}

fn main() {
  let request: &[u8]; // An encoded call to the Search method of SearchService
  prost::Service::from(MySearchService).decode_and_handle(request);
}

This generation process does not seem to depend on the way it is implemented in the project. Is it planned and not implemented because there hasn’t been any one to do so, or do you think it shouldn’t be implemented?

@caspermeijn
Copy link
Collaborator

I don't understand what you are asking.

If you are looking for a complete service implementation, then you should look into gRPC and tonic.

Else, you need to explain more of what you are trying to do, primarily because prost::Service::from(MySearchService).decode_and_handle(request); seems to be completely made up, and you don't explain what you expect of it.

@victorbnl
Copy link
Author

What I wanted was to make an RPC interface with prost but manage transport myself. So I expected the, indeed made-up example of prost::Service::from(MySearchService).decode_and_handle(request); to create an object with a method to decode a request and call the corresponding method on the trait. Basically like when decoding a message, but it decodes a request instead, and instead of returning an object it calls the appropriate trait method and returns the response the trait method returns. So one can manage transport themselves, and when receiving a request on the stream, they just have to call the decode_and_handle with the received bytes and write the bytes it returns. Prost handles the logic of decoding and encoding, and provides a nice RPC interface.

@caspermeijn
Copy link
Collaborator

As I understand, the transport needs to provide the method name to be called. There is wire specification for the service itself, just for the request and response objects. There is no protobuf specification for what you are looking for.

The C++ implementation did have something like you are describing, but that seems to be deprecated. A transport-specific implementation is deemed more efficient.

If you are willing to contribute an implementation of your proposal, I am open to discussing that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants