Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

binder: Set default idle timeout to 60 seconds, and enable "strict lifecycle management". #9486

Merged
merged 10 commits into from
Nov 29, 2022
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 20 additions & 0 deletions binder/src/main/java/io/grpc/binder/BinderChannelBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package io.grpc.binder;

import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkState;

import android.app.Application;
import android.content.ComponentName;
Expand Down Expand Up @@ -124,6 +125,7 @@ public static BinderChannelBuilder forTarget(String target) {
private SecurityPolicy securityPolicy;
private InboundParcelablePolicy inboundParcelablePolicy;
private BindServiceFlags bindServiceFlags;
private boolean strictLifecycleManagement;

private BinderChannelBuilder(
@Nullable AndroidComponentAddress directAddress,
Expand All @@ -134,6 +136,7 @@ private BinderChannelBuilder(
securityPolicy = SecurityPolicies.internalOnly();
inboundParcelablePolicy = InboundParcelablePolicy.DEFAULT;
bindServiceFlags = BindServiceFlags.DEFAULTS;
idleTimeout(60, TimeUnit.SECONDS);

final class BinderChannelTransportFactoryBuilder
implements ClientTransportFactoryBuilder {
Expand Down Expand Up @@ -224,6 +227,23 @@ public BinderChannelBuilder inboundParcelablePolicy(
return this;
}

/**
* Enables strict lifecycle management. This should be called by processes with elevated procrank, where
* reliance on idle timers is inappropriate for lifecycle management.
ejona86 marked this conversation as resolved.
Show resolved Hide resolved
*/
public BinderChannelBuilder strictLifecycleManagement() {
strictLifecycleManagement = true;
super.idleTimeout(1000, TimeUnit.DAYS); // >30 days disables timeouts entirely.
return this;
}

@Override
public BinderChannelBuilder idleTimeout(long value, TimeUnit unit) {
checkState(!strictLifecycleManagement, "Idle timeouts are not supported when strict lifecycle management is enabled");
super.idleTimeout(value, unit);
return this;
}

/** Creates new binder transports. */
private static final class TransportFactory implements ClientTransportFactory {
private final Context sourceContext;
Expand Down
44 changes: 44 additions & 0 deletions binder/src/test/java/io/grpc/binder/BinderChannelBuilderTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright 2020 The gRPC Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.grpc.binder;

import static org.junit.Assert.fail;

import android.content.Context;
import androidx.test.core.app.ApplicationProvider;
import java.util.concurrent.TimeUnit;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;

@RunWith(RobolectricTestRunner.class)
public final class BinderChannelBuilderTest {
private final Context appContext = ApplicationProvider.getApplicationContext();
private final AndroidComponentAddress addr = AndroidComponentAddress.forContext(appContext);

@Test
public void strictLifecycleManagementForbidsIdleTimers() {
BinderChannelBuilder builder = BinderChannelBuilder.forAddress(addr, appContext);
builder.strictLifecycleManagement();
try {
builder.idleTimeout(10, TimeUnit.SECODNS);
fail();
} catch (IllegalStateException ise) {
// Expected.
}
}
}