Eric Anderson | d11e9be | 2015-07-17 23:02:03 -0700 | [diff] [blame] | 1 | /* |
Carl Mastrangelo | 60a0b0c | 2018-05-03 14:55:21 -0700 | [diff] [blame] | 2 | * Copyright 2015 The gRPC Authors |
Eric Anderson | d11e9be | 2015-07-17 23:02:03 -0700 | [diff] [blame] | 3 | * |
Carl Mastrangelo | 3bfd630 | 2017-05-31 13:29:01 -0700 | [diff] [blame] | 4 | * 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 Mastrangelo | 166108a | 2017-06-01 14:28:37 -0700 | [diff] [blame] | 7 | * |
Carl Mastrangelo | 3bfd630 | 2017-05-31 13:29:01 -0700 | [diff] [blame] | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
Carl Mastrangelo | 166108a | 2017-06-01 14:28:37 -0700 | [diff] [blame] | 9 | * |
Carl Mastrangelo | 3bfd630 | 2017-05-31 13:29:01 -0700 | [diff] [blame] | 10 | * 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 Anderson | d11e9be | 2015-07-17 23:02:03 -0700 | [diff] [blame] | 15 | */ |
| 16 | |
Kun Zhang | d2929cd | 2015-08-04 16:32:32 -0700 | [diff] [blame] | 17 | package io.grpc.inprocess; |
Eric Anderson | d11e9be | 2015-07-17 23:02:03 -0700 | [diff] [blame] | 18 | |
ZHANG Dapeng | 39decad | 2018-02-26 17:12:04 -0800 | [diff] [blame] | 19 | import static com.google.common.base.Preconditions.checkNotNull; |
| 20 | |
Eric Anderson | d11e9be | 2015-07-17 23:02:03 -0700 | [diff] [blame] | 21 | import com.google.common.base.Preconditions; |
nmittler | b687bdc | 2015-08-31 16:13:39 -0700 | [diff] [blame] | 22 | import io.grpc.ExperimentalApi; |
Kun Zhang | 903197b | 2017-04-07 11:03:24 -0700 | [diff] [blame] | 23 | import io.grpc.ServerStreamTracer; |
nmittler | b687bdc | 2015-08-31 16:13:39 -0700 | [diff] [blame] | 24 | import io.grpc.internal.AbstractServerImplBuilder; |
ZHANG Dapeng | 39decad | 2018-02-26 17:12:04 -0800 | [diff] [blame] | 25 | import io.grpc.internal.FixedObjectPool; |
Eric Anderson | 994f200 | 2017-07-19 10:31:26 -0700 | [diff] [blame] | 26 | import io.grpc.internal.GrpcUtil; |
ZHANG Dapeng | 39decad | 2018-02-26 17:12:04 -0800 | [diff] [blame] | 27 | import io.grpc.internal.ObjectPool; |
| 28 | import io.grpc.internal.SharedResourcePool; |
Carl Mastrangelo | f6582d8 | 2015-09-11 09:24:43 -0700 | [diff] [blame] | 29 | import java.io.File; |
Kun Zhang | 903197b | 2017-04-07 11:03:24 -0700 | [diff] [blame] | 30 | import java.util.List; |
ZHANG Dapeng | 4bac7d7 | 2018-02-23 16:44:10 -0800 | [diff] [blame] | 31 | import java.util.UUID; |
ZHANG Dapeng | 39decad | 2018-02-26 17:12:04 -0800 | [diff] [blame] | 32 | import java.util.concurrent.ScheduledExecutorService; |
Eric Anderson | f0dcbc3 | 2017-11-17 06:54:05 -0800 | [diff] [blame] | 33 | import java.util.concurrent.TimeUnit; |
Carl Mastrangelo | f6582d8 | 2015-09-11 09:24:43 -0700 | [diff] [blame] | 34 | |
Eric Anderson | d11e9be | 2015-07-17 23:02:03 -0700 | [diff] [blame] | 35 | /** |
| 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. |
zpencer | 26caa48 | 2017-07-13 08:19:29 -0700 | [diff] [blame] | 40 | * |
| 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 Dapeng | 4bac7d7 | 2018-02-23 16:44:10 -0800 | [diff] [blame] | 50 | * String uniqueName = InProcessServerBuilder.generateName(); |
| 51 | * Server server = InProcessServerBuilder.forName(uniqueName) |
zpencer | 26caa48 | 2017-07-13 08:19:29 -0700 | [diff] [blame] | 52 | * .directExecutor() // directExecutor is fine for unit tests |
| 53 | * .addService(/* your code here */) |
| 54 | * .build().start(); |
ZHANG Dapeng | 4bac7d7 | 2018-02-23 16:44:10 -0800 | [diff] [blame] | 55 | * ManagedChannel channel = InProcessChannelBuilder.forName(uniqueName) |
zpencer | 26caa48 | 2017-07-13 08:19:29 -0700 | [diff] [blame] | 56 | * .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 Anderson | d11e9be | 2015-07-17 23:02:03 -0700 | [diff] [blame] | 66 | */ |
Carl Mastrangelo | bc661e7 | 2016-05-03 14:25:55 -0700 | [diff] [blame] | 67 | @ExperimentalApi("https://github.com/grpc/grpc-java/issues/1783") |
nmittler | b687bdc | 2015-08-31 16:13:39 -0700 | [diff] [blame] | 68 | public final class InProcessServerBuilder |
Carl Mastrangelo | 51ce204 | 2017-06-22 12:25:32 -0700 | [diff] [blame] | 69 | extends AbstractServerImplBuilder<InProcessServerBuilder> { |
Eric Anderson | d11e9be | 2015-07-17 23:02:03 -0700 | [diff] [blame] | 70 | /** |
| 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 Anderson | d11e9be | 2015-07-17 23:02:03 -0700 | [diff] [blame] | 74 | * @return a new builder |
| 75 | */ |
| 76 | public static InProcessServerBuilder forName(String name) { |
| 77 | return new InProcessServerBuilder(name); |
| 78 | } |
| 79 | |
Carl Mastrangelo | 53f56a3 | 2017-09-06 15:21:17 -0700 | [diff] [blame] | 80 | /** |
| 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 Dapeng | 4bac7d7 | 2018-02-23 16:44:10 -0800 | [diff] [blame] | 87 | /** |
| 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 Anderson | d11e9be | 2015-07-17 23:02:03 -0700 | [diff] [blame] | 94 | private final String name; |
ZHANG Dapeng | 39decad | 2018-02-26 17:12:04 -0800 | [diff] [blame] | 95 | private ObjectPool<ScheduledExecutorService> schedulerPool = |
| 96 | SharedResourcePool.forResource(GrpcUtil.TIMER_SERVICE); |
Eric Anderson | d11e9be | 2015-07-17 23:02:03 -0700 | [diff] [blame] | 97 | |
Eric Anderson | d11e9be | 2015-07-17 23:02:03 -0700 | [diff] [blame] | 98 | private InProcessServerBuilder(String name) { |
| 99 | this.name = Preconditions.checkNotNull(name, "name"); |
Kun Zhang | 8edead0 | 2017-10-09 10:14:36 -0700 | [diff] [blame] | 100 | // In-process transport should not record its traffic to the stats module. |
| 101 | // https://github.com/grpc/grpc-java/issues/2284 |
Kun Zhang | ca9a41a | 2017-11-10 17:22:22 -0800 | [diff] [blame] | 102 | setStatsRecordStartedRpcs(false); |
| 103 | setStatsRecordFinishedRpcs(false); |
Eric Anderson | f0dcbc3 | 2017-11-17 06:54:05 -0800 | [diff] [blame] | 104 | // 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 Anderson | d11e9be | 2015-07-17 23:02:03 -0700 | [diff] [blame] | 107 | } |
| 108 | |
ZHANG Dapeng | 39decad | 2018-02-26 17:12:04 -0800 | [diff] [blame] | 109 | /** |
| 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 Anderson | d11e9be | 2015-07-17 23:02:03 -0700 | [diff] [blame] | 126 | @Override |
Kun Zhang | 903197b | 2017-04-07 11:03:24 -0700 | [diff] [blame] | 127 | protected InProcessServer buildTransportServer( |
| 128 | List<ServerStreamTracer.Factory> streamTracerFactories) { |
ZHANG Dapeng | 39decad | 2018-02-26 17:12:04 -0800 | [diff] [blame] | 129 | return new InProcessServer(name, schedulerPool, streamTracerFactories); |
Eric Anderson | d11e9be | 2015-07-17 23:02:03 -0700 | [diff] [blame] | 130 | } |
Carl Mastrangelo | f6582d8 | 2015-09-11 09:24:43 -0700 | [diff] [blame] | 131 | |
| 132 | @Override |
| 133 | public InProcessServerBuilder useTransportSecurity(File certChain, File privateKey) { |
| 134 | throw new UnsupportedOperationException("TLS not supported in InProcessServer"); |
| 135 | } |
Eric Anderson | d11e9be | 2015-07-17 23:02:03 -0700 | [diff] [blame] | 136 | } |