|
17 | 17 | package io.rsocket.transport.local;
|
18 | 18 |
|
19 | 19 | import io.rsocket.Closeable;
|
| 20 | +import io.rsocket.DuplexConnection; |
20 | 21 | import io.rsocket.transport.ClientTransport;
|
21 | 22 | import io.rsocket.transport.ServerTransport;
|
22 | 23 | import java.util.Objects;
|
| 24 | +import java.util.Set; |
23 | 25 | import java.util.UUID;
|
24 | 26 | import java.util.concurrent.ConcurrentHashMap;
|
25 | 27 | import java.util.concurrent.ConcurrentMap;
|
| 28 | +import java.util.stream.Collectors; |
26 | 29 | import reactor.core.Scannable;
|
27 | 30 | import reactor.core.publisher.Mono;
|
28 | 31 | import reactor.core.publisher.Sinks;
|
|
34 | 37 | */
|
35 | 38 | public final class LocalServerTransport implements ServerTransport<Closeable> {
|
36 | 39 |
|
37 |
| - private static final ConcurrentMap<String, ConnectionAcceptor> registry = |
| 40 | + private static final ConcurrentMap<String, ServerCloseableAcceptor> registry = |
38 | 41 | new ConcurrentHashMap<>();
|
39 | 42 |
|
40 | 43 | private final String name;
|
@@ -72,7 +75,10 @@ public static LocalServerTransport createEphemeral() {
|
72 | 75 | */
|
73 | 76 | public static void dispose(String name) {
|
74 | 77 | Objects.requireNonNull(name, "name must not be null");
|
75 |
| - registry.remove(name); |
| 78 | + ServerCloseableAcceptor sca = registry.remove(name); |
| 79 | + if (sca != null) { |
| 80 | + sca.dispose(); |
| 81 | + } |
76 | 82 | }
|
77 | 83 |
|
78 | 84 | /**
|
@@ -107,34 +113,55 @@ public Mono<Closeable> start(ConnectionAcceptor acceptor) {
|
107 | 113 | Objects.requireNonNull(acceptor, "acceptor must not be null");
|
108 | 114 | return Mono.create(
|
109 | 115 | sink -> {
|
110 |
| - ServerCloseable closeable = new ServerCloseable(name, acceptor); |
111 |
| - if (registry.putIfAbsent(name, acceptor) != null) { |
112 |
| - throw new IllegalStateException("name already registered: " + name); |
| 116 | + ServerCloseableAcceptor closeable = new ServerCloseableAcceptor(name, acceptor); |
| 117 | + if (registry.putIfAbsent(name, closeable) != null) { |
| 118 | + sink.error(new IllegalStateException("name already registered: " + name)); |
113 | 119 | }
|
114 | 120 | sink.success(closeable);
|
115 | 121 | });
|
116 | 122 | }
|
117 | 123 |
|
118 |
| - static class ServerCloseable implements Closeable { |
| 124 | + @SuppressWarnings({"ReactorTransformationOnMonoVoid", "CallingSubscribeInNonBlockingScope"}) |
| 125 | + static class ServerCloseableAcceptor implements ConnectionAcceptor, Closeable { |
119 | 126 |
|
120 | 127 | private final LocalSocketAddress address;
|
121 | 128 |
|
122 | 129 | private final ConnectionAcceptor acceptor;
|
123 | 130 |
|
124 |
| - private final Sinks.Empty<Void> onClose = Sinks.empty(); |
| 131 | + private final Set<DuplexConnection> activeConnections = ConcurrentHashMap.newKeySet(); |
| 132 | + |
| 133 | + private final Sinks.Empty<Void> onClose = Sinks.unsafe().empty(); |
125 | 134 |
|
126 |
| - ServerCloseable(String name, ConnectionAcceptor acceptor) { |
| 135 | + ServerCloseableAcceptor(String name, ConnectionAcceptor acceptor) { |
127 | 136 | Objects.requireNonNull(name, "name must not be null");
|
128 | 137 | this.address = new LocalSocketAddress(name);
|
129 | 138 | this.acceptor = acceptor;
|
130 | 139 | }
|
131 | 140 |
|
| 141 | + @Override |
| 142 | + public Mono<Void> apply(DuplexConnection duplexConnection) { |
| 143 | + activeConnections.add(duplexConnection); |
| 144 | + duplexConnection |
| 145 | + .onClose() |
| 146 | + .doFinally(__ -> activeConnections.remove(duplexConnection)) |
| 147 | + .subscribe(); |
| 148 | + return acceptor.apply(duplexConnection); |
| 149 | + } |
| 150 | + |
132 | 151 | @Override
|
133 | 152 | public void dispose() {
|
134 |
| - if (!registry.remove(address.getName(), acceptor)) { |
135 |
| - throw new AssertionError(); |
| 153 | + if (!registry.remove(address.getName(), this)) { |
| 154 | + // already disposed |
| 155 | + return; |
136 | 156 | }
|
137 |
| - onClose.tryEmitEmpty(); |
| 157 | + |
| 158 | + Mono.whenDelayError( |
| 159 | + activeConnections |
| 160 | + .stream() |
| 161 | + .peek(DuplexConnection::dispose) |
| 162 | + .map(DuplexConnection::onClose) |
| 163 | + .collect(Collectors.toList())) |
| 164 | + .subscribe(null, onClose::tryEmitError, onClose::tryEmitEmpty); |
138 | 165 | }
|
139 | 166 |
|
140 | 167 | @Override
|
|
0 commit comments