I've confirmed that a period of inactivity is about 60 minutes. I added an
idle timer which release a channel after a period of inactivity but a newly
created channel is still unusable.
Below is the logcat once the channel is terminated and created.
06-26 13:41:47.612 28807-28891/com.test I/ManagedChannelImpl: [{0}]
Terminated
06-26 14:39:01.128 28807-28917/com.test I/ManagedChannelImpl: [{0}] Created
with target {1}
I re-created a channel with the same config after a certain period of
inactivity (an hour) but the newly created channel still doesn't make a
request to a server. I installed a packet capturing app and my app didn't
any make a request to a server after this happens. Could you guide me what
I need to do to fix this issue?
public static synchronized ManagedChannelImpl get() throws
RuntimeException {
if (_interceptor == null) {
throw new IllegalArgumentException("A Non-null ClientInterceptor must
be provided");
}
if (channel == null || channel.isShutdown() || channel.isTerminated()) {
channel = OkHttpChannelBuilder.forAddress(BuildConfig.HOST, BuildConfig
.PORT)
.sslSocketFactory(provideSSLSocketFactory())
.intercept(_interceptor)
.compressorRegistry(CompressorRegistry.getDefaultInstance())
.decompressorRegistry(DecompressorRegistry.getDefaultInstance())
.build();
}
return channel;
}
static SSLSocketFactory provideSSLSocketFactory() {
try {
CertificateFactory cf = CertificateFactory.getInstance("X.509");
InputStream caInput =
AndroidContext.getContext().getResources().openRawResource(R.raw.
api_rancam_com);
Certificate ca;
try {
ca = cf.generateCertificate(caInput);
} finally {
caInput.close();
}
String keyStoreType = KeyStore.getDefaultType();
KeyStore keyStore = KeyStore.getInstance(keyStoreType);
keyStore.load(null, null);
keyStore.setCertificateEntry("ca", ca);
String tmfAlgorithm = TrustManagerFactory.getDefaultAlgorithm();
TrustManagerFactory tmf = TrustManagerFactory.getInstance(tmfAlgorithm
);
tmf.init(keyStore);
SSLContext context = SSLContext.getInstance("TLS");
context.init(null, tmf.getTrustManagers(), null);
return context.getSocketFactory();
} catch (Exception e) {
Timber.e(e, "Failed to create SSLSocketFactory");
return null;
}
}
public static void release() {
try {
if (channel != null) {
Timber.d("Releasing channel");
channel.shutdown();
channel = null;
Timber.d("Channel released");
}
} catch (Exception e) {
Timber.e(e, "Failed to release channel");
channel = null;
}
}
Above is the code that I use to create and release a ManagedChannel.
Thank you in advance.
On Sunday, June 26, 2016 at 1:06:06 AM UTC+9, Eric Anderson wrote:
>
> On Sat, Jun 25, 2016 at 4:36 AM, Taehyun Park <[email protected]
> <javascript:>> wrote:
>
>> I'm using grpc java on Android and I found a very weird issue. After a
>> certain period a ManagedChannel no longer works.
>>
>
> Was that after a period of inactivity? Were you on good WiFi (one that you
> trust), bad WiFi, or cellular?
>
> I instantiated a ManagedChannel when there is no cached channel then cache
>> it until the number of active channels is 0. My app worked fine and didn't
>> have a problem when it's launched. but all grpc calls stopped working after
>> a certain period. The app wasn't closed but it was in a backstack.
>> I searched a similar issue in grpc issue tracker on github but I'm not
>> sure if https://github.com/grpc/grpc-java/issues/1636 and
>> https://github.com/grpc/grpc-java/issues/1648 are the issue I'm having.
>>
>
> When discussing keepalive, the general assumption is the network
> misbehaved (which is not uncommon on mobile). Keepalive is only going to be
> active when an RPC is outstanding. That means that it will need to be
> combined with channel idleness to close TCP connections after inactivity.
>
> https://github.com/grpc/grpc-java/issues/1972
> https://github.com/grpc/grpc-java/issues/1276
>
> Both are planned for 1.0 in order to give a full solution to this sort of
> issue (assuming that failures are due to poor networks). Idleness in
> general is useful. With it you really don't have much reason to cache the
> channel like you were. You could create the channel eagerly (which starts
> IDLE) and the channel can release resources when there is a period of
> inactivity.
>
--
You received this message because you are subscribed to the Google Groups
"grpc.io" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/grpc-io/c5390be9-d4ce-49a2-bc83-d1066a60e85f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.