1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
|
/*
* Copyright 2015 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.inprocess;
import static com.google.common.base.Preconditions.checkNotNull;
import com.google.common.base.MoreObjects;
import io.grpc.Instrumented;
import io.grpc.InternalChannelz.SocketStats;
import io.grpc.ServerStreamTracer;
import io.grpc.internal.InternalServer;
import io.grpc.internal.ObjectPool;
import io.grpc.internal.ServerListener;
import io.grpc.internal.ServerTransportListener;
import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.ScheduledExecutorService;
import javax.annotation.concurrent.ThreadSafe;
@ThreadSafe
final class InProcessServer implements InternalServer {
private static final ConcurrentMap<String, InProcessServer> registry
= new ConcurrentHashMap<String, InProcessServer>();
static InProcessServer findServer(String name) {
return registry.get(name);
}
private final String name;
private final List<ServerStreamTracer.Factory> streamTracerFactories;
private ServerListener listener;
private boolean shutdown;
/** Defaults to be a SharedResourcePool. */
private final ObjectPool<ScheduledExecutorService> schedulerPool;
/**
* Only used to make sure the scheduler has at least one reference. Since child transports can
* outlive this server, they must get their own reference.
*/
private ScheduledExecutorService scheduler;
InProcessServer(
String name, ObjectPool<ScheduledExecutorService> schedulerPool,
List<ServerStreamTracer.Factory> streamTracerFactories) {
this.name = name;
this.schedulerPool = schedulerPool;
this.streamTracerFactories =
Collections.unmodifiableList(checkNotNull(streamTracerFactories, "streamTracerFactories"));
}
@Override
public void start(ServerListener serverListener) throws IOException {
this.listener = serverListener;
this.scheduler = schedulerPool.getObject();
// Must be last, as channels can start connecting after this point.
if (registry.putIfAbsent(name, this) != null) {
throw new IOException("name already registered: " + name);
}
}
@Override
public int getPort() {
return -1;
}
@Override
public List<Instrumented<SocketStats>> getListenSockets() {
return Collections.emptyList();
}
@Override
public void shutdown() {
if (!registry.remove(name, this)) {
throw new AssertionError();
}
scheduler = schedulerPool.returnObject(scheduler);
synchronized (this) {
shutdown = true;
listener.serverShutdown();
}
}
@Override
public String toString() {
return MoreObjects.toStringHelper(this).add("name", name).toString();
}
synchronized ServerTransportListener register(InProcessTransport transport) {
if (shutdown) {
return null;
}
return listener.transportCreated(transport);
}
ObjectPool<ScheduledExecutorService> getScheduledExecutorServicePool() {
return schedulerPool;
}
List<ServerStreamTracer.Factory> getStreamTracerFactories() {
return streamTracerFactories;
}
}
|