Skip to content

Commit 3d25c1e

Browse files
committedJan 8, 2016
Load Balancer abstraction.
Refactoring the `ConnectionProvider` interface to make it simple. Additionally provided a load balancer abstraction to provide higher level load balancing semantics later.
1 parent a8713a6 commit 3d25c1e

File tree

107 files changed

+3415
-2318
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

107 files changed

+3415
-2318
lines changed
 

‎rxnetty-common/src/main/java/io/reactivex/netty/HandlerNames.java

+2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
*/
2222
public enum HandlerNames {
2323

24+
SslHandler("ssl-handler"),
25+
SslConnectionEmissionHandler("ssl-connection-emitter"),
2426
WireLogging("wire-logging-handler"),
2527
PrimitiveConverter("primitive-converter"),
2628
ClientReadTimeoutHandler("client-read-timeout-handler"),

‎rxnetty-common/src/main/java/io/reactivex/netty/channel/AbstractConnectionToChannelBridge.java

+48-42
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
*
4343
* Lazy subscriptions are allowed on {@link Connection#getInput()} if and only if the channel is configured to
4444
* not read data automatically (i.e. {@link ChannelOption#AUTO_READ} is set to {@code false}). Otherwise,
45-
* if {@link Connection#getInput()} is subscribed lazily, the subscriber always recieves an error. The content
45+
* if {@link Connection#getInput()} is subscribed lazily, the subscriber always receives an error. The content
4646
* in this case is disposed upon reading.
4747
*
4848
* @param <R> Type read from the connection held by this handler.
@@ -77,7 +77,7 @@ public abstract class AbstractConnectionToChannelBridge<R, W> extends Backpressu
7777

7878
protected ConnectionEventListener eventListener;
7979
protected EventPublisher eventPublisher;
80-
private Subscriber<? super Connection<R, W>> newConnectionSub;
80+
private Subscriber<? super Channel> newChannelSub;
8181
private ReadProducer<R> readProducer;
8282
private boolean raiseErrorOnInputSubscription;
8383
private boolean connectionEmitted;
@@ -105,13 +105,6 @@ protected AbstractConnectionToChannelBridge(String thisHandlerName,
105105
this.eventPublisherAttributeKey = eventPublisherAttributeKey;
106106
}
107107

108-
protected AbstractConnectionToChannelBridge(String thisHandlerName, Subscriber<? super Connection<R, W>> connSub,
109-
AttributeKey<ConnectionEventListener> eventListenerAttributeKey,
110-
AttributeKey<EventPublisher> eventPublisherAttributeKey) {
111-
this(thisHandlerName, eventListenerAttributeKey, eventPublisherAttributeKey);
112-
newConnectionSub = connSub;
113-
}
114-
115108
@Override
116109
public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
117110
if (null == eventListener && null == eventPublisher) {
@@ -136,6 +129,15 @@ public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
136129
super.handlerAdded(ctx);
137130
}
138131

132+
@Override
133+
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
134+
if (!connectionEmitted && isValidToEmit(newChannelSub)) {
135+
emitNewConnection(ctx.channel());
136+
connectionEmitted = true;
137+
}
138+
super.channelInactive(ctx);
139+
}
140+
139141
@Override
140142
public void channelUnregistered(ChannelHandlerContext ctx) throws Exception {
141143

@@ -151,29 +153,29 @@ public void channelUnregistered(ChannelHandlerContext ctx) throws Exception {
151153
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
152154
if (evt instanceof EmitConnectionEvent) {
153155
if (!connectionEmitted) {
154-
createNewConnection(ctx.channel());
156+
emitNewConnection(ctx.channel());
155157
connectionEmitted = true;
156158
}
157159
} else if (evt instanceof ConnectionCreationFailedEvent) {
158-
if (isValidToEmit(newConnectionSub)) {
159-
newConnectionSub.onError(((ConnectionCreationFailedEvent)evt).getThrowable());
160+
if (isValidToEmit(newChannelSub)) {
161+
newChannelSub.onError(((ConnectionCreationFailedEvent)evt).getThrowable());
160162
}
161-
} else if (evt instanceof ConnectionSubscriberEvent) {
163+
} else if (evt instanceof ChannelSubscriberEvent) {
162164
@SuppressWarnings("unchecked")
163-
final ConnectionSubscriberEvent<R, W> connectionSubscriberEvent = (ConnectionSubscriberEvent<R, W>) evt;
165+
final ChannelSubscriberEvent<R, W> channelSubscriberEvent = (ChannelSubscriberEvent<R, W>) evt;
164166

165-
newConnectionSubscriber(connectionSubscriberEvent);
167+
newConnectionSubscriber(channelSubscriberEvent);
166168
} else if (evt instanceof ConnectionInputSubscriberEvent) {
167169
@SuppressWarnings("unchecked")
168170
ConnectionInputSubscriberEvent<R, W> event = (ConnectionInputSubscriberEvent<R, W>) evt;
169171

170-
newConnectionInputSubscriber(ctx.channel(), event.getSubscriber(), event.getConnection(), false);
172+
newConnectionInputSubscriber(ctx.channel(), event.getSubscriber(), false);
171173
} else if (evt instanceof ConnectionInputSubscriberResetEvent) {
172174
resetConnectionInputSubscriber();
173175
} else if (evt instanceof ConnectionInputSubscriberReplaceEvent) {
174176
@SuppressWarnings("unchecked")
175177
ConnectionInputSubscriberReplaceEvent<R, W> event = (ConnectionInputSubscriberReplaceEvent<R, W>) evt;
176-
replaceConnectionInputSubscriber(event);
178+
replaceConnectionInputSubscriber(ctx.channel(), event);
177179
}
178180

179181
super.userEventTriggered(ctx, evt);
@@ -205,8 +207,8 @@ public boolean shouldReadMore(ChannelHandlerContext ctx) {
205207

206208
@Override
207209
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
208-
if (!connectionEmitted && isValidToEmit(newConnectionSub)) {
209-
newConnectionSub.onError(cause);
210+
if (!connectionEmitted && isValidToEmit(newChannelSub)) {
211+
newChannelSub.onError(cause);
210212
} else if (isValidToEmitToReadSubscriber(readProducer)) {
211213
readProducer.sendOnError(cause);
212214
} else {
@@ -228,34 +230,38 @@ protected boolean connectionInputSubscriberExists(Channel channel) {
228230
return null != readProducer && null != readProducer.subscriber && !readProducer.subscriber.isUnsubscribed();
229231
}
230232

231-
protected void onNewReadSubscriber(Connection<R, W> connection, Subscriber<? super R> subscriber) {
233+
protected void onNewReadSubscriber(Subscriber<? super R> subscriber) {
232234
// NOOP
233235
}
234236

235-
protected final void checkEagerSubscriptionIfConfigured(Connection<R, W> connection, Channel channel) {
237+
protected final void checkEagerSubscriptionIfConfigured(Channel channel) {
236238
if (channel.config().isAutoRead() && null == readProducer) {
237239
// If the channel is set to auto-read and there is no eager subscription then, we should raise errors
238240
// when a subscriber arrives.
239241
raiseErrorOnInputSubscription = true;
240-
final Subscriber<? super R> discardAll = ConnectionInputSubscriberEvent.discardAllInput(connection)
242+
final Subscriber<? super R> discardAll = ConnectionInputSubscriberEvent.discardAllInput()
241243
.getSubscriber();
242244
final ReadProducer<R> producer = new ReadProducer<>(discardAll, channel);
243245
discardAll.setProducer(producer);
244246
readProducer = producer;
245247
}
246248
}
247249

248-
protected final Subscriber<? super Connection<R, W>> getNewConnectionSub() {
249-
return newConnectionSub;
250+
protected final Subscriber<? super Channel> getNewChannelSub() {
251+
return newChannelSub;
250252
}
251253

252-
private void createNewConnection(Channel channel) {
253-
if (isValidToEmit(newConnectionSub)) {
254-
Connection<R, W> connection = ConnectionImpl.create(channel, eventListener, eventPublisher);
255-
newConnectionSub.onNext(connection);
256-
connectionEmitted = true;
257-
checkEagerSubscriptionIfConfigured(connection, channel);
258-
newConnectionSub.onCompleted();
254+
private void emitNewConnection(Channel channel) {
255+
if (isValidToEmit(newChannelSub)) {
256+
try {
257+
newChannelSub.onNext(channel);
258+
connectionEmitted = true;
259+
checkEagerSubscriptionIfConfigured(channel);
260+
newChannelSub.onCompleted();
261+
} catch (Exception e) {
262+
logger.error("Error emitting a new connection. Closing this channel.", e);
263+
channel.close();
264+
}
259265
} else {
260266
channel.close(); // Closing the connection if not sent to a subscriber.
261267
}
@@ -271,39 +277,39 @@ private void resetConnectionInputSubscriber() {
271277
}
272278

273279
private void newConnectionInputSubscriber(final Channel channel, final Subscriber<? super R> subscriber,
274-
final Connection<R, W> connection, boolean replace) {
280+
boolean replace) {
275281
final Subscriber<? super R> connInputSub = null == readProducer ? null : readProducer.subscriber;
276282
if (isValidToEmit(connInputSub)) {
277283
if (!replace) {
278284
/*Allow only once concurrent input subscriber but allow concatenated subscribers*/
279285
subscriber.onError(ONLY_ONE_CONN_INPUT_SUB_ALLOWED);
280286
} else {
281-
setNewReadProducer(channel, subscriber, connection);
287+
setNewReadProducer(channel, subscriber);
282288
connInputSub.onCompleted();
283289
}
284290
} else if (raiseErrorOnInputSubscription) {
285291
subscriber.onError(LAZY_CONN_INPUT_SUB);
286292
} else {
287-
setNewReadProducer(channel, subscriber, connection);
293+
setNewReadProducer(channel, subscriber);
288294
}
289295
}
290296

291-
private void setNewReadProducer(Channel channel, Subscriber<? super R> subscriber, Connection<R, W> connection) {
297+
private void setNewReadProducer(Channel channel, Subscriber<? super R> subscriber) {
292298
final ReadProducer<R> producer = new ReadProducer<>(subscriber, channel);
293299
subscriber.setProducer(producer);
294-
onNewReadSubscriber(connection, subscriber);
300+
onNewReadSubscriber(subscriber);
295301
readProducer = producer;
296302
}
297303

298-
private void replaceConnectionInputSubscriber(ConnectionInputSubscriberReplaceEvent<R, W> event) {
304+
private void replaceConnectionInputSubscriber(Channel channel, ConnectionInputSubscriberReplaceEvent<R, W> event) {
299305
ConnectionInputSubscriberEvent<R, W> newSubEvent = event.getNewSubEvent();
300-
newConnectionInputSubscriber(newSubEvent.getConnection().unsafeNettyChannel(), newSubEvent.getSubscriber(),
301-
newSubEvent.getConnection(), true);
306+
newConnectionInputSubscriber(channel, newSubEvent.getSubscriber(),
307+
true);
302308
}
303309

304-
private void newConnectionSubscriber(ConnectionSubscriberEvent<R, W> event) {
305-
if (null == newConnectionSub) {
306-
newConnectionSub = event.getSubscriber();
310+
private void newConnectionSubscriber(ChannelSubscriberEvent<R, W> event) {
311+
if (null == newChannelSub) {
312+
newChannelSub = event.getSubscriber();
307313
} else {
308314
event.getSubscriber().onError(ONLY_ONE_CONN_SUB_ALLOWED);
309315
}

0 commit comments

Comments
 (0)
Please sign in to comment.