Skip to content

Commit

Permalink
Provide custom grpc/http client for DaprClientBuilder Signed-off-by: …
Browse files Browse the repository at this point in the history
…Subash Gamage <[email protected]>
  • Loading branch information
subashfmr committed Feb 23, 2022
1 parent 4c077e1 commit 1d88c76
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 3 deletions.
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,27 @@ This SDK provides a basic serialization for request/response objects but also fo
}
```

#### How to provide custom grpc/http client for DaprClient

When building dapr client, following api will allow you to provide your own managed grpc channel within the DaprClient. This allows users to define/maintain their own thread pools and tune grpc parameters.
Similarly you can override http client while building dapr client and tune http client parameters.

* Override custom grpc managed channel for DaprClient

```java
DaprClientBuilder daprClientBuilder = new DaprClientBuilder();
ManagedChannelBuilder managedChannelBuilder = Grpc.newChannelBuilder("localhost:8000", TlsChannelCredentials.create())
.executor(Executors.newFixedThreadPool(20));
DaprClient client = daprClientBuilder.withManagedGrpcChannel(managedChannelBuilder.build()).build();
```

* Override custom okhttpclient for DaprClient

```java
DaprClientBuilder daprClientBuilder = new DaprClientBuilder();
DaprClient client = daprClientBuilder.withOkHttpClient(new OkHttpClient.Builder().readTimeout(5, TimeUnit.SECONDS)).build();
```


#### Debug Java application or Dapr's Java SDK
Expand Down
24 changes: 22 additions & 2 deletions sdk/src/main/java/io/dapr/client/DaprClientBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import io.dapr.v1.DaprGrpc;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import okhttp3.OkHttpClient;

import java.io.Closeable;

Expand All @@ -43,6 +44,11 @@ public class DaprClientBuilder {
*/
private final DaprHttpBuilder daprHttpBuilder;

/**
* Managed gRPC channel
*/
private ManagedChannel channel;

/**
* Serializer used for request and response objects in DaprClient.
*/
Expand Down Expand Up @@ -103,6 +109,16 @@ public DaprClientBuilder withStateSerializer(DaprObjectSerializer stateSerialize
return this;
}

public DaprClientBuilder withManagedGrpcChannel(ManagedChannel channel){
this.channel = channel;
return this;
}

public DaprClientBuilder withOkHttpClient(OkHttpClient.Builder builder){
this.daprHttpBuilder.withOkHttpClientBuilder(builder);
return this;
}

/**
* Build an instance of the Client based on the provided setup.
*
Expand Down Expand Up @@ -157,8 +173,12 @@ private DaprClient buildDaprClientGrpc() {
if (port <= 0) {
throw new IllegalArgumentException("Invalid port.");
}
ManagedChannel channel = ManagedChannelBuilder.forAddress(
Properties.SIDECAR_IP.get(), port).usePlaintext().build();

if(channel == null) {
channel = ManagedChannelBuilder.forAddress(
Properties.SIDECAR_IP.get(), port).usePlaintext().build();
}

Closeable closeableChannel = () -> {
if (channel != null && !channel.isShutdown()) {
channel.shutdown();
Expand Down
15 changes: 14 additions & 1 deletion sdk/src/main/java/io/dapr/client/DaprHttpBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,20 @@ public class DaprHttpBuilder {
*/
private static final Object LOCK = new Object();

/**
* OKHttpClient builder
*/
private OkHttpClient.Builder builder;

public DaprHttpBuilder() {
builder = new OkHttpClient.Builder();
}

public DaprHttpBuilder withOkHttpClientBuilder(OkHttpClient.Builder builder){
this.builder = builder;
return this;
}

/**
* Build an instance of the Http client based on the provided setup.
*
Expand All @@ -52,7 +66,6 @@ private DaprHttp buildDaprHttp() {
if (OK_HTTP_CLIENT == null) {
synchronized (LOCK) {
if (OK_HTTP_CLIENT == null) {
OkHttpClient.Builder builder = new OkHttpClient.Builder();
Duration readTimeout = Duration.ofSeconds(Properties.HTTP_CLIENT_READ_TIMEOUT_SECONDS.get());
builder.readTimeout(readTimeout);
OK_HTTP_CLIENT = builder.build();
Expand Down
24 changes: 24 additions & 0 deletions sdk/src/test/java/io/dapr/client/DaprClientBuilderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,16 @@
package io.dapr.client;

import io.dapr.serializer.DaprObjectSerializer;
import io.grpc.ChannelCredentials;
import io.grpc.Grpc;
import io.grpc.ManagedChannelBuilder;
import io.grpc.TlsChannelCredentials;
import okhttp3.OkHttpClient;
import org.junit.Test;

import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
Expand Down Expand Up @@ -49,4 +57,20 @@ public void noStateSerializer() {
new DaprClientBuilder().withStateSerializer(null);
}

@Test
public void testBuilderWithManagedChannel(){
DaprClientBuilder daprClientBuilder = new DaprClientBuilder();
ManagedChannelBuilder managedChannelBuilder = Grpc.newChannelBuilder("localhost:8000", TlsChannelCredentials.create())
.executor(Executors.newFixedThreadPool(20));
DaprClient client = daprClientBuilder.withManagedGrpcChannel(managedChannelBuilder.build()).build();
assertNotNull(client);
}

@Test
public void testBuilderWithOkHttpClient(){
DaprClientBuilder daprClientBuilder = new DaprClientBuilder();
DaprClient client = daprClientBuilder.withOkHttpClient(new OkHttpClient.Builder().readTimeout(10, TimeUnit.SECONDS)).build();
assertNotNull(client);
}

}

0 comments on commit 1d88c76

Please sign in to comment.