|
14 | 14 | package io.dapr.actors.client;
|
15 | 15 |
|
16 | 16 | import com.google.protobuf.ByteString;
|
| 17 | +import io.dapr.client.resiliency.ResiliencyOptions; |
17 | 18 | import io.dapr.config.Properties;
|
18 | 19 | import io.dapr.exceptions.DaprException;
|
19 | 20 | import io.dapr.internal.opencensus.GrpcWrapper;
|
| 21 | +import io.dapr.internal.resiliency.RetryPolicy; |
| 22 | +import io.dapr.internal.resiliency.TimeoutPolicy; |
20 | 23 | import io.dapr.v1.DaprGrpc;
|
21 | 24 | import io.dapr.v1.DaprProtos;
|
22 | 25 | import io.grpc.CallOptions;
|
|
39 | 42 | */
|
40 | 43 | class DaprGrpcClient implements DaprClient {
|
41 | 44 |
|
| 45 | + /** |
| 46 | + * Timeout policy for SDK calls to Dapr API. |
| 47 | + */ |
| 48 | + private final TimeoutPolicy timeoutPolicy; |
| 49 | + |
| 50 | + /** |
| 51 | + * Retry policy for SDK calls to Dapr API. |
| 52 | + */ |
| 53 | + private final RetryPolicy retryPolicy; |
| 54 | + |
42 | 55 | /**
|
43 | 56 | * The async gRPC stub.
|
44 | 57 | */
|
45 |
| - private DaprGrpc.DaprStub client; |
| 58 | + private final DaprGrpc.DaprStub client; |
46 | 59 |
|
47 | 60 | /**
|
48 | 61 | * Internal constructor.
|
49 | 62 | *
|
50 | 63 | * @param grpcClient Dapr's GRPC client.
|
| 64 | + * @param resiliencyOptions Client resiliency options (optional) |
51 | 65 | */
|
52 |
| - DaprGrpcClient(DaprGrpc.DaprStub grpcClient) { |
| 66 | + DaprGrpcClient(DaprGrpc.DaprStub grpcClient, ResiliencyOptions resiliencyOptions) { |
53 | 67 | this.client = intercept(grpcClient);
|
| 68 | + this.timeoutPolicy = new TimeoutPolicy( |
| 69 | + resiliencyOptions == null ? null : resiliencyOptions.getTimeout()); |
| 70 | + this.retryPolicy = new RetryPolicy( |
| 71 | + resiliencyOptions == null ? null : resiliencyOptions.getMaxRetries()); |
54 | 72 | }
|
55 | 73 |
|
56 | 74 | /**
|
@@ -78,14 +96,14 @@ public Mono<byte[]> invoke(String actorType, String actorId, String methodName,
|
78 | 96 | * @param client GRPC client for Dapr.
|
79 | 97 | * @return Client after adding interceptors.
|
80 | 98 | */
|
81 |
| - private static DaprGrpc.DaprStub intercept(DaprGrpc.DaprStub client) { |
| 99 | + private DaprGrpc.DaprStub intercept(DaprGrpc.DaprStub client) { |
82 | 100 | ClientInterceptor interceptor = new ClientInterceptor() {
|
83 | 101 | @Override
|
84 | 102 | public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(
|
85 | 103 | MethodDescriptor<ReqT, RespT> methodDescriptor,
|
86 |
| - CallOptions callOptions, |
| 104 | + CallOptions options, |
87 | 105 | Channel channel) {
|
88 |
| - ClientCall<ReqT, RespT> clientCall = channel.newCall(methodDescriptor, callOptions); |
| 106 | + ClientCall<ReqT, RespT> clientCall = channel.newCall(methodDescriptor, timeoutPolicy.apply(options)); |
89 | 107 | return new ForwardingClientCall.SimpleForwardingClientCall<ReqT, RespT>(clientCall) {
|
90 | 108 | @Override
|
91 | 109 | public void start(final Listener<RespT> responseListener, final Metadata metadata) {
|
@@ -114,7 +132,8 @@ private static DaprGrpc.DaprStub intercept(ContextView context, DaprGrpc.DaprStu
|
114 | 132 | }
|
115 | 133 |
|
116 | 134 | private <T> Mono<T> createMono(Consumer<StreamObserver<T>> consumer) {
|
117 |
| - return Mono.create(sink -> DaprException.wrap(() -> consumer.accept(createStreamObserver(sink))).run()); |
| 135 | + return retryPolicy.apply( |
| 136 | + Mono.create(sink -> DaprException.wrap(() -> consumer.accept(createStreamObserver(sink))).run())); |
118 | 137 | }
|
119 | 138 |
|
120 | 139 | private <T> StreamObserver<T> createStreamObserver(MonoSink<T> sink) {
|
|
0 commit comments