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

Add a dedicated gRPC client builder. #3981

Closed
ikhoon opened this issue Dec 8, 2021 · 0 comments · Fixed by #3999
Closed

Add a dedicated gRPC client builder. #3981

ikhoon opened this issue Dec 8, 2021 · 0 comments · Fixed by #3999

Comments

@ikhoon
Copy link
Contributor

ikhoon commented Dec 8, 2021

If users want to set gRPC specific options, they have to use GrpcClientOptions with ClientBuilder.
For example:

MyStub client = 
    Clients.builder("myGrpcServer")
           .maxResponseLength(MAX_MESSAGE_SIZE)
           .option(GrpcClientOptions.GRPC_JSON_MARSHALLER_FACTORY.newValue(serviceDescriptor -> {
                return GrpcJsonMarshaller.builder()
                                         .jsonMarshallerCustomizer(builder -> {
                                             builder.preservingProtoFieldNames(true);
                                         })
                                         .build(serviceDescriptor);
           }))
           .option(GrpcClientOptions.INTERCEPTORS.newValue(List.of(myInterceptor1, myInterceptor2))
           .build(MyStub.class);

However, the GrpcClientOptions style has two disadvantages.

  1. GrpcClientOptions is not strongly related to ClientBuilder. So users should check Javadoc or reference documentation.
    This could be a learning curve for beginners.
  2. The API looks verbose. 🙈

It would be useful to provide a dedicated gRPC client builder so that users can fluently/easily build their gRPC clients with various options.

GrpcClients.builder(...)
           .maxResponseLength(MAX_MESSAGE_SIZE)
           .decorator(myDecorators)
           .jsonMarshallerFactory(descriptor -> {
                ...
            })
           .intercept(myInterceptors)
           .build(MyStub.class); 

Implementation note:

We can reuse ClientBuilder and Clients to implement GrpcClients and GrpcClientBuilder.

ikhoon added a commit to ikhoon/armeria that referenced this issue Dec 23, 2021
Motivation:

It would be useful to provide a dedicated gRPC client builder so that
users can fluently/easily build their gRPC clients with various options.
See line#3981 for details.

Modifications:

- Add `GrpcClients` and `GrpcClientBuilder` that provide various factory
  and builder methods to coveniently create gRPC client stub.
  - `gproto` will be used the default protocol if a `SerializationFormat` is not specified,
  - `GrpcClientOptions` is no longer needed to configure a gRPC client.
- Add `UnwrappableChannel` to unwrap an intercepted `Channel`.
- Migate code where the legacy gRPC client factory and builder is used.

Result:

You can now fluently build a gRPC client with various options using
`GrpcClientBuilder`.
```java
GrpcClients.builder(...)
           .decorator(myDecorators)
           .maxResponseMessageLength(MAX_MESSAGE_SIZE)
           .jsonMarshallerFactory(descriptor -> {
               ...
           })
           .intercept(myInterceptors)
           .build(MyStub.class);
```
trustin pushed a commit that referenced this issue Jan 13, 2022
Motivation:

It would be useful to provide a dedicated gRPC client builder so that
users can fluently/easily build their gRPC clients with various options.
See #3981 for details.

Modifications:

- Add `GrpcClients` and `GrpcClientBuilder` that provide various factory
  and builder methods to conveniently create gRPC client stub.
  - `gproto` will be used as the default protocol if a `SerializationFormat` is not specified,
  - `GrpcClientOptions` is no longer needed to configure a gRPC client.
- Add `UnwrappableChannel` to unwrap an intercepted `Channel`.
- Migrate code where the legacy gRPC client factory and builder is used.

Result:

- You can now fluently build a gRPC client with various options using
  `GrpcClientBuilder`.
  ```java
  GrpcClients.builder(...)
             .decorator(myDecorators)
             .maxResponseMessageLength(MAX_MESSAGE_SIZE)
             .jsonMarshallerFactory(descriptor -> {
                 ...
             })
             .intercept(myInterceptors)
             .build(MyStub.class);
  ```
- Closes #3981

Co-authored-by: minux <songmw725@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant