Skip to content

Commit 82a3bda

Browse files
author
Sophia Tevosyan
committed
first commit
1 parent 9ef0448 commit 82a3bda

3 files changed

Lines changed: 33 additions & 4 deletions

File tree

azurefunctions/src/main/java/com/microsoft/durabletask/azurefunctions/DurableClientContext.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import com.microsoft.azure.functions.HttpStatus;
1010
import com.microsoft.durabletask.DurableTaskClient;
1111
import com.microsoft.durabletask.DurableTaskGrpcClientBuilder;
12+
import com.microsoft.durabletask.DurableTaskGrpcClientFactory;
1213
import com.microsoft.durabletask.OrchestrationMetadata;
1314
import com.microsoft.durabletask.OrchestrationRuntimeStatus;
1415

@@ -45,6 +46,10 @@ public String getTaskHubName() {
4546
* @return the Durable Task client object associated with the current function invocation.
4647
*/
4748
public DurableTaskClient getClient() {
49+
if (this.client != null) {
50+
return this.client;
51+
}
52+
4853
if (this.rpcBaseUrl == null || this.rpcBaseUrl.length() == 0) {
4954
throw new IllegalStateException("The client context wasn't populated with an RPC base URL!");
5055
}
@@ -56,7 +61,7 @@ public DurableTaskClient getClient() {
5661
throw new IllegalStateException("The client context RPC base URL was invalid!", ex);
5762
}
5863

59-
this.client = new DurableTaskGrpcClientBuilder().port(rpcURL.getPort()).build();
64+
this.client = DurableTaskGrpcClientFactory.getClient(rpcURL.getPort());
6065
return this.client;
6166
}
6267

@@ -78,9 +83,7 @@ public HttpResponseMessage waitForCompletionOrCreateCheckStatusResponse(
7883
HttpRequestMessage<?> request,
7984
String instanceId,
8085
Duration timeout) {
81-
if (this.client == null) {
82-
this.client = getClient();
83-
}
86+
this.client = getClient();
8487
OrchestrationMetadata orchestration;
8588
try {
8689
orchestration = this.client.waitForInstanceCompletion(instanceId, timeout, true);

client/src/main/java/com/microsoft/durabletask/DurableTaskGrpcClient.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,17 @@ public final class DurableTaskGrpcClient extends DurableTaskClient {
6161
this.sidecarClient = TaskHubSidecarServiceGrpc.newBlockingStub(sidecarGrpcChannel);
6262
}
6363

64+
DurableTaskGrpcClient(int port) {
65+
this.dataConverter = new JacksonDataConverter();
66+
67+
// Need to keep track of this channel so we can dispose it on close()
68+
this.managedSidecarChannel = ManagedChannelBuilder
69+
.forAddress("localhost", port)
70+
.usePlaintext()
71+
.build();
72+
this.sidecarClient = TaskHubSidecarServiceGrpc.newBlockingStub(this.managedSidecarChannel);
73+
}
74+
6475
/**
6576
* Closes the internally managed gRPC channel, if one exists.
6677
* <p>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
package com.microsoft.durabletask;
5+
6+
import java.util.concurrent.ConcurrentHashMap;
7+
import java.util.concurrent.ConcurrentMap;
8+
9+
public final class DurableTaskGrpcClientFactory {
10+
private static final ConcurrentMap<Integer, DurableTaskGrpcClient> portToClientMap = new ConcurrentHashMap<>();
11+
12+
public static DurableTaskClient getClient(int port) {
13+
return portToClientMap.computeIfAbsent(port, DurableTaskGrpcClient::new);
14+
}
15+
}

0 commit comments

Comments
 (0)