blob: 205eb4bddfdd679cf0057772f289886ea26a51a0 [file] [log] [blame]
Eric Andersond11e9be2015-07-17 23:02:03 -07001/*
Carl Mastrangelo60a0b0c2018-05-03 14:55:21 -07002 * Copyright 2015 The gRPC Authors
Eric Andersond11e9be2015-07-17 23:02:03 -07003 *
Carl Mastrangelo3bfd6302017-05-31 13:29:01 -07004 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
Carl Mastrangelo166108a2017-06-01 14:28:37 -07007 *
Carl Mastrangelo3bfd6302017-05-31 13:29:01 -07008 * http://www.apache.org/licenses/LICENSE-2.0
Carl Mastrangelo166108a2017-06-01 14:28:37 -07009 *
Carl Mastrangelo3bfd6302017-05-31 13:29:01 -070010 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
Eric Andersond11e9be2015-07-17 23:02:03 -070015 */
16
Kun Zhangd2929cd2015-08-04 16:32:32 -070017package io.grpc.inprocess;
Eric Andersond11e9be2015-07-17 23:02:03 -070018
ZHANG Dapeng39decad2018-02-26 17:12:04 -080019import static com.google.common.base.Preconditions.checkNotNull;
20
Eric Andersond11e9be2015-07-17 23:02:03 -070021import com.google.common.base.Preconditions;
nmittlerb687bdc2015-08-31 16:13:39 -070022import io.grpc.ExperimentalApi;
Kun Zhang903197b2017-04-07 11:03:24 -070023import io.grpc.ServerStreamTracer;
nmittlerb687bdc2015-08-31 16:13:39 -070024import io.grpc.internal.AbstractServerImplBuilder;
ZHANG Dapeng39decad2018-02-26 17:12:04 -080025import io.grpc.internal.FixedObjectPool;
Eric Anderson994f2002017-07-19 10:31:26 -070026import io.grpc.internal.GrpcUtil;
ZHANG Dapeng39decad2018-02-26 17:12:04 -080027import io.grpc.internal.ObjectPool;
28import io.grpc.internal.SharedResourcePool;
Carl Mastrangelof6582d82015-09-11 09:24:43 -070029import java.io.File;
Kun Zhang903197b2017-04-07 11:03:24 -070030import java.util.List;
ZHANG Dapeng4bac7d72018-02-23 16:44:10 -080031import java.util.UUID;
ZHANG Dapeng39decad2018-02-26 17:12:04 -080032import java.util.concurrent.ScheduledExecutorService;
Eric Andersonf0dcbc32017-11-17 06:54:05 -080033import java.util.concurrent.TimeUnit;
Carl Mastrangelof6582d82015-09-11 09:24:43 -070034
Eric Andersond11e9be2015-07-17 23:02:03 -070035/**
36 * Builder for a server that services in-process requests. Clients identify the in-process server by
37 * its name.
38 *
39 * <p>The server is intended to be fully-featured, high performance, and useful in testing.
zpencer26caa482017-07-13 08:19:29 -070040 *
41 * <h3>Using JUnit TestRule</h3>
42 * The class "GrpcServerRule" (from "grpc-java/testing") is a JUnit TestRule that
43 * creates a {@link InProcessServer} and a {@link io.grpc.ManagedChannel ManagedChannel}. This
44 * test rule contains the boilerplate code shown below. The classes "HelloWorldServerTest" and
45 * "HelloWorldClientTest" (from "grpc-java/examples") demonstrate basic usage.
46 *
47 * <h3>Usage example</h3>
48 * <h4>Server and client channel setup</h4>
49 * <pre>
ZHANG Dapeng4bac7d72018-02-23 16:44:10 -080050 * String uniqueName = InProcessServerBuilder.generateName();
51 * Server server = InProcessServerBuilder.forName(uniqueName)
zpencer26caa482017-07-13 08:19:29 -070052 * .directExecutor() // directExecutor is fine for unit tests
53 * .addService(&#47;* your code here *&#47;)
54 * .build().start();
ZHANG Dapeng4bac7d72018-02-23 16:44:10 -080055 * ManagedChannel channel = InProcessChannelBuilder.forName(uniqueName)
zpencer26caa482017-07-13 08:19:29 -070056 * .directExecutor()
57 * .build();
58 * </pre>
59 *
60 * <h4>Usage in tests</h4>
61 * The channel can be used normally. A blocking stub example:
62 * <pre>
63 * TestServiceGrpc.TestServiceBlockingStub blockingStub =
64 * TestServiceGrpc.newBlockingStub(channel);
65 * </pre>
Eric Andersond11e9be2015-07-17 23:02:03 -070066 */
Carl Mastrangelobc661e72016-05-03 14:25:55 -070067@ExperimentalApi("https://github.com/grpc/grpc-java/issues/1783")
nmittlerb687bdc2015-08-31 16:13:39 -070068public final class InProcessServerBuilder
Carl Mastrangelo51ce2042017-06-22 12:25:32 -070069 extends AbstractServerImplBuilder<InProcessServerBuilder> {
Eric Andersond11e9be2015-07-17 23:02:03 -070070 /**
71 * Create a server builder that will bind with the given name.
72 *
73 * @param name the identity of the server for clients to connect to
Eric Andersond11e9be2015-07-17 23:02:03 -070074 * @return a new builder
75 */
76 public static InProcessServerBuilder forName(String name) {
77 return new InProcessServerBuilder(name);
78 }
79
Carl Mastrangelo53f56a32017-09-06 15:21:17 -070080 /**
81 * Always fails. Call {@link #forName} instead.
82 */
83 public static InProcessServerBuilder forPort(int port) {
84 throw new UnsupportedOperationException("call forName() instead");
85 }
86
ZHANG Dapeng4bac7d72018-02-23 16:44:10 -080087 /**
88 * Generates a new server name that is unique each time.
89 */
90 public static String generateName() {
91 return UUID.randomUUID().toString();
92 }
93
Eric Andersond11e9be2015-07-17 23:02:03 -070094 private final String name;
ZHANG Dapeng39decad2018-02-26 17:12:04 -080095 private ObjectPool<ScheduledExecutorService> schedulerPool =
96 SharedResourcePool.forResource(GrpcUtil.TIMER_SERVICE);
Eric Andersond11e9be2015-07-17 23:02:03 -070097
Eric Andersond11e9be2015-07-17 23:02:03 -070098 private InProcessServerBuilder(String name) {
99 this.name = Preconditions.checkNotNull(name, "name");
Kun Zhang8edead02017-10-09 10:14:36 -0700100 // In-process transport should not record its traffic to the stats module.
101 // https://github.com/grpc/grpc-java/issues/2284
Kun Zhangca9a41a2017-11-10 17:22:22 -0800102 setStatsRecordStartedRpcs(false);
103 setStatsRecordFinishedRpcs(false);
Eric Andersonf0dcbc32017-11-17 06:54:05 -0800104 // Disable handshake timeout because it is unnecessary, and can trigger Thread creation that can
105 // break some environments (like tests).
106 handshakeTimeout(Long.MAX_VALUE, TimeUnit.SECONDS);
Eric Andersond11e9be2015-07-17 23:02:03 -0700107 }
108
ZHANG Dapeng39decad2018-02-26 17:12:04 -0800109 /**
110 * Provides a custom scheduled executor service.
111 *
112 * <p>It's an optional parameter. If the user has not provided a scheduled executor service when
113 * the channel is built, the builder will use a static cached thread pool.
114 *
115 * @return this
116 *
117 * @since 1.11.0
118 */
119 public InProcessServerBuilder scheduledExecutorService(
120 ScheduledExecutorService scheduledExecutorService) {
121 schedulerPool = new FixedObjectPool<ScheduledExecutorService>(
122 checkNotNull(scheduledExecutorService, "scheduledExecutorService"));
123 return this;
124 }
125
Eric Andersond11e9be2015-07-17 23:02:03 -0700126 @Override
Kun Zhang903197b2017-04-07 11:03:24 -0700127 protected InProcessServer buildTransportServer(
128 List<ServerStreamTracer.Factory> streamTracerFactories) {
ZHANG Dapeng39decad2018-02-26 17:12:04 -0800129 return new InProcessServer(name, schedulerPool, streamTracerFactories);
Eric Andersond11e9be2015-07-17 23:02:03 -0700130 }
Carl Mastrangelof6582d82015-09-11 09:24:43 -0700131
132 @Override
133 public InProcessServerBuilder useTransportSecurity(File certChain, File privateKey) {
134 throw new UnsupportedOperationException("TLS not supported in InProcessServer");
135 }
Eric Andersond11e9be2015-07-17 23:02:03 -0700136}