Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,13 @@

package com.google.net.stubby;

import static com.google.net.stubby.AbstractServiceBuilder.DEFAULT_EXECUTOR;

import com.google.common.base.Preconditions;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import com.google.net.stubby.SharedResourceHolder.Resource;
import com.google.net.stubby.transport.ClientTransportFactory;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import javax.annotation.Nullable;

Expand All @@ -46,6 +47,26 @@
* @param <BuilderT> The concrete type of this builder.
*/
public abstract class AbstractChannelBuilder<BuilderT extends AbstractChannelBuilder<BuilderT>> {
static final Resource<ExecutorService> DEFAULT_EXECUTOR =
new Resource<ExecutorService>() {
private static final String name = "grpc-default-executor";
@Override
public ExecutorService create() {
return Executors.newCachedThreadPool(new ThreadFactoryBuilder()
.setNameFormat(name + "-%d").build());
}

@Override
public void close(ExecutorService instance) {
instance.shutdown();
}

@Override
public String toString() {
return name;
}
};

@Nullable
private ExecutorService userExecutor;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,26 @@

package com.google.net.stubby;

import static com.google.net.stubby.AbstractChannelBuilder.DEFAULT_EXECUTOR;

import com.google.common.base.Preconditions;
import com.google.common.util.concurrent.Service;
import com.google.net.stubby.transport.ServerListener;

import java.util.concurrent.ExecutorService;

import javax.annotation.Nullable;

/**
* The base class for server builders.
*
* @param <BuilderT> The concrete type for this builder.
*/
public abstract class AbstractServerBuilder<BuilderT extends AbstractServerBuilder<BuilderT>>
extends AbstractServiceBuilder<ServerImpl, BuilderT> {
public abstract class AbstractServerBuilder<BuilderT extends AbstractServerBuilder<BuilderT>> {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice simplification


private final HandlerRegistry registry;
@Nullable
private ExecutorService userExecutor;

/**
* Constructs using a given handler registry.
Expand All @@ -61,6 +66,21 @@ protected AbstractServerBuilder() {
this.registry = new MutableHandlerRegistryImpl();
}

/**
* Provides a custom executor.
*
* <p>It's an optional parameter. If the user has not provided an executor when the server is
* built, the builder will use a static cached thread pool.
*
* <p>The server won't take ownership of the given executor. It's caller's responsibility to
* shut down the executor when it's desired.
*/
@SuppressWarnings("unchecked")
public final BuilderT executor(ExecutorService executor) {
userExecutor = executor;
return (BuilderT) this;
}

/**
* Adds a service implementation to the handler registry.
*
Expand All @@ -76,10 +96,33 @@ public final BuilderT addService(ServerServiceDefinition service) {
throw new UnsupportedOperationException("Underlying HandlerRegistry is not mutable");
}

@Override
protected final ServerImpl buildImpl(ExecutorService executor) {
/**
* Builds a server using the given parameters.
*
* <p>The returned service will not been started or be bound a port. You will need to start it
* with {@link ServerImpl#start()}.
*/
public ServerImpl build() {
final ExecutorService executor;
final boolean releaseExecutor;
if (userExecutor != null) {
executor = userExecutor;
releaseExecutor = false;
} else {
executor = SharedResourceHolder.get(DEFAULT_EXECUTOR);
releaseExecutor = true;
}

ServerImpl server = new ServerImpl(executor, registry);
server.setTransportServer(buildTransportServer(server.serverListener()));
server.setTerminationRunnable(new Runnable() {
@Override
public void run() {
if (releaseExecutor) {
SharedResourceHolder.release(DEFAULT_EXECUTOR, executor);
}
}
});
return server;
}

Expand Down
194 changes: 0 additions & 194 deletions core/src/main/java/com/google/net/stubby/AbstractServiceBuilder.java

This file was deleted.

4 changes: 1 addition & 3 deletions core/src/main/java/com/google/net/stubby/Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,11 @@

package com.google.net.stubby;

import com.google.common.util.concurrent.Service;

import javax.annotation.concurrent.ThreadSafe;

/**
* Server for listening for and dispatching incoming calls. Although Server is an interface, it is
* not expected to be implemented by application code or interceptors.
*/
@ThreadSafe
public interface Server extends Service {}
public interface Server {}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this interface buy us anything anymore? Do we expect it to grow?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It didn't really buy us much before either. I honestly don't know if we expect it to grow. It seems trashing the interface and renaming ServerImpl to Server would be fine. If we claim the interface served a purpose before, then I can add start(), shutdown(), etc to the interface. We can't do that on client-side because the interface is used for interception, but that isn't the case on server-side.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm fine keeping it for now ... we can always remove later.

Loading