Skip to content

Commit

Permalink
Merge pull request googleapis#34 from shinfan/dev
Browse files Browse the repository at this point in the history
Do not auto close channel if it is provided by user.
  • Loading branch information
shinfan committed Mar 23, 2016
2 parents 1c0411b + c4dc5c8 commit 0439525
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion src/main/java/com/google/api/gax/grpc/ServiceApiSettings.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;

//TODO(pongad): Don't close the channel if the user gives one to us
/**
* A settings class to configure a service api class.
*
Expand Down Expand Up @@ -66,6 +65,10 @@ public ServiceApiSettings(ImmutableList<? extends ApiCallSettings> allMethods) {
public ManagedChannel getChannel(Executor executor) {
throw new RuntimeException("No Channel or ConnectionSettings provided.");
}
@Override
public boolean shouldAutoClose() {
return true;
}
};
executorProvider = new ExecutorProvider() {
private ScheduledExecutorService executor = null;
Expand All @@ -83,6 +86,7 @@ public ScheduledExecutorService getExecutor() {
}

private interface ChannelProvider {
boolean shouldAutoClose();
ManagedChannel getChannel(Executor executor) throws IOException;
}

Expand All @@ -98,6 +102,10 @@ public ServiceApiSettings provideChannelWith(final ManagedChannel channel) {
public ManagedChannel getChannel(Executor executor) {
return channel;
}
@Override
public boolean shouldAutoClose() {
return false;
}
};
return this;
}
Expand Down Expand Up @@ -125,6 +133,11 @@ public ManagedChannel getChannel(Executor executor) throws IOException {
return channel;
}

@Override
public boolean shouldAutoClose() {
return true;
}

private String serviceHeader() {
// GAX version only works when the package is invoked as a jar. Otherwise returns null.
String gaxVersion = ChannelProvider.class.getPackage().getImplementationVersion();
Expand Down Expand Up @@ -152,6 +165,13 @@ public ManagedChannel getChannel() throws IOException {
return channelProvider.getChannel(getExecutor());
}

/**
* Returns true if the channel should be automatically closed with the API wrapper class.
*/
public boolean shouldAutoCloseChannel() {
return channelProvider.shouldAutoClose();
}

private interface ExecutorProvider {
ScheduledExecutorService getExecutor();
}
Expand Down

0 comments on commit 0439525

Please sign in to comment.