Skip to content

Commit 6468fea

Browse files
committed
Recreate managed channel when options change
1 parent c1ed458 commit 6468fea

1 file changed

Lines changed: 35 additions & 6 deletions

File tree

android/src/main/java/com/reactnativegrpc/GrpcModule.java

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,28 +63,33 @@ public void getIsInsecure(final Promise promise) {
6363
@ReactMethod
6464
public void setHost(String host) {
6565
this.host = host;
66+
this.handleOptionsChanged();
6667
}
6768

6869
@ReactMethod
6970
public void setInsecure(boolean insecure) {
7071
this.isInsecure = insecure;
72+
this.handleOptionsChanged();
7173
}
7274

7375
@ReactMethod
7476
public void setCompression(Boolean enable, String compressorName, String limit) {
7577
this.withCompression = enable;
7678
this.compressorName = compressorName;
79+
this.handleOptionsChanged();
7780
}
7881

7982
@ReactMethod
8083
public void setResponseSizeLimit(int limit) {
8184
this.responseSizeLimit = limit;
85+
this.handleOptionsChanged();
8286
}
8387

8488
public void setKeepalive(boolean enabled, int time, int timeout) {
8589
this.keepAliveEnabled = enabled;
8690
this.keepAliveTime = time;
8791
this.keepAliveTimeout = timeout;
92+
this.handleOptionsChanged();
8893
}
8994

9095
@ReactMethod
@@ -104,7 +109,15 @@ public void unaryCall(int id, String path, ReadableMap obj, ReadableMap headers,
104109

105110
@ReactMethod
106111
public void serverStreamingCall(int id, String path, ReadableMap obj, ReadableMap headers, final Promise promise) {
107-
ClientCall call = this.startGrpcCall(id, path, MethodDescriptor.MethodType.SERVER_STREAMING, headers);
112+
ClientCall call;
113+
114+
try {
115+
call = this.startGrpcCall(id, path, MethodDescriptor.MethodType.SERVER_STREAMING, headers);
116+
} catch (Exception e) {
117+
promise.reject(e);
118+
119+
return;
120+
}
108121

109122
byte[] data = Base64.decode(obj.getString("data"), Base64.NO_WRAP);
110123

@@ -122,7 +135,13 @@ public void clientStreamingCall(int id, String path, ReadableMap obj, ReadableMa
122135
ClientCall call = callsMap.get(id);
123136

124137
if (call == null) {
125-
call = this.startGrpcCall(id, path, MethodDescriptor.MethodType.CLIENT_STREAMING, headers);
138+
try {
139+
call = this.startGrpcCall(id, path, MethodDescriptor.MethodType.CLIENT_STREAMING, headers);
140+
} catch (Exception e) {
141+
promise.reject(e);
142+
143+
return;
144+
}
126145

127146
callsMap.put(id, call);
128147
}
@@ -160,7 +179,11 @@ public void cancelGrpcCall(int id, final Promise promise) {
160179
}
161180
}
162181

163-
private ClientCall startGrpcCall(int id, String path, MethodDescriptor.MethodType methodType, ReadableMap headers) {
182+
private ClientCall startGrpcCall(int id, String path, MethodDescriptor.MethodType methodType, ReadableMap headers) throws Exception {
183+
if (this.managedChannel == null) {
184+
throw new Exception("Channel not created");
185+
}
186+
164187
path = normalizePath(path);
165188

166189
final Metadata headersMetadata = new Metadata();
@@ -184,7 +207,7 @@ private ClientCall startGrpcCall(int id, String path, MethodDescriptor.MethodTyp
184207
callOptions = callOptions.withCompression(this.compressorName);
185208
}
186209

187-
ClientCall call = this.getManagedChannel().newCall(descriptor, callOptions);
210+
ClientCall call = this.managedChannel.newCall(descriptor, callOptions);
188211

189212
call.start(new ClientCall.Listener() {
190213
@Override
@@ -298,9 +321,15 @@ private static String normalizePath(String path) {
298321
return path;
299322
}
300323

301-
private ManagedChannel getManagedChannel() {
302-
if (managedChannel != null) return managedChannel;
324+
private void handleOptionsChanged() {
325+
if (this.managedChannel != null) {
326+
this.managedChannel.shutdown();
327+
}
328+
329+
this.managedChannel = createManagedChannel();
330+
}
303331

332+
private ManagedChannel createManagedChannel() {
304333
ManagedChannelBuilder channelBuilder = ManagedChannelBuilder.forTarget(this.host);
305334

306335
if (this.responseSizeLimit != null) {

0 commit comments

Comments
 (0)