42
42
*
43
43
* Lazy subscriptions are allowed on {@link Connection#getInput()} if and only if the channel is configured to
44
44
* 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
46
46
* in this case is disposed upon reading.
47
47
*
48
48
* @param <R> Type read from the connection held by this handler.
@@ -77,7 +77,7 @@ public abstract class AbstractConnectionToChannelBridge<R, W> extends Backpressu
77
77
78
78
protected ConnectionEventListener eventListener ;
79
79
protected EventPublisher eventPublisher ;
80
- private Subscriber <? super Connection < R , W >> newConnectionSub ;
80
+ private Subscriber <? super Channel > newChannelSub ;
81
81
private ReadProducer <R > readProducer ;
82
82
private boolean raiseErrorOnInputSubscription ;
83
83
private boolean connectionEmitted ;
@@ -105,13 +105,6 @@ protected AbstractConnectionToChannelBridge(String thisHandlerName,
105
105
this .eventPublisherAttributeKey = eventPublisherAttributeKey ;
106
106
}
107
107
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
-
115
108
@ Override
116
109
public void handlerAdded (ChannelHandlerContext ctx ) throws Exception {
117
110
if (null == eventListener && null == eventPublisher ) {
@@ -136,6 +129,15 @@ public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
136
129
super .handlerAdded (ctx );
137
130
}
138
131
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
+
139
141
@ Override
140
142
public void channelUnregistered (ChannelHandlerContext ctx ) throws Exception {
141
143
@@ -151,29 +153,29 @@ public void channelUnregistered(ChannelHandlerContext ctx) throws Exception {
151
153
public void userEventTriggered (ChannelHandlerContext ctx , Object evt ) throws Exception {
152
154
if (evt instanceof EmitConnectionEvent ) {
153
155
if (!connectionEmitted ) {
154
- createNewConnection (ctx .channel ());
156
+ emitNewConnection (ctx .channel ());
155
157
connectionEmitted = true ;
156
158
}
157
159
} 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 ());
160
162
}
161
- } else if (evt instanceof ConnectionSubscriberEvent ) {
163
+ } else if (evt instanceof ChannelSubscriberEvent ) {
162
164
@ SuppressWarnings ("unchecked" )
163
- final ConnectionSubscriberEvent <R , W > connectionSubscriberEvent = (ConnectionSubscriberEvent <R , W >) evt ;
165
+ final ChannelSubscriberEvent <R , W > channelSubscriberEvent = (ChannelSubscriberEvent <R , W >) evt ;
164
166
165
- newConnectionSubscriber (connectionSubscriberEvent );
167
+ newConnectionSubscriber (channelSubscriberEvent );
166
168
} else if (evt instanceof ConnectionInputSubscriberEvent ) {
167
169
@ SuppressWarnings ("unchecked" )
168
170
ConnectionInputSubscriberEvent <R , W > event = (ConnectionInputSubscriberEvent <R , W >) evt ;
169
171
170
- newConnectionInputSubscriber (ctx .channel (), event .getSubscriber (), event . getConnection (), false );
172
+ newConnectionInputSubscriber (ctx .channel (), event .getSubscriber (), false );
171
173
} else if (evt instanceof ConnectionInputSubscriberResetEvent ) {
172
174
resetConnectionInputSubscriber ();
173
175
} else if (evt instanceof ConnectionInputSubscriberReplaceEvent ) {
174
176
@ SuppressWarnings ("unchecked" )
175
177
ConnectionInputSubscriberReplaceEvent <R , W > event = (ConnectionInputSubscriberReplaceEvent <R , W >) evt ;
176
- replaceConnectionInputSubscriber (event );
178
+ replaceConnectionInputSubscriber (ctx . channel (), event );
177
179
}
178
180
179
181
super .userEventTriggered (ctx , evt );
@@ -205,8 +207,8 @@ public boolean shouldReadMore(ChannelHandlerContext ctx) {
205
207
206
208
@ Override
207
209
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 );
210
212
} else if (isValidToEmitToReadSubscriber (readProducer )) {
211
213
readProducer .sendOnError (cause );
212
214
} else {
@@ -228,34 +230,38 @@ protected boolean connectionInputSubscriberExists(Channel channel) {
228
230
return null != readProducer && null != readProducer .subscriber && !readProducer .subscriber .isUnsubscribed ();
229
231
}
230
232
231
- protected void onNewReadSubscriber (Connection < R , W > connection , Subscriber <? super R > subscriber ) {
233
+ protected void onNewReadSubscriber (Subscriber <? super R > subscriber ) {
232
234
// NOOP
233
235
}
234
236
235
- protected final void checkEagerSubscriptionIfConfigured (Connection < R , W > connection , Channel channel ) {
237
+ protected final void checkEagerSubscriptionIfConfigured (Channel channel ) {
236
238
if (channel .config ().isAutoRead () && null == readProducer ) {
237
239
// If the channel is set to auto-read and there is no eager subscription then, we should raise errors
238
240
// when a subscriber arrives.
239
241
raiseErrorOnInputSubscription = true ;
240
- final Subscriber <? super R > discardAll = ConnectionInputSubscriberEvent .discardAllInput (connection )
242
+ final Subscriber <? super R > discardAll = ConnectionInputSubscriberEvent .discardAllInput ()
241
243
.getSubscriber ();
242
244
final ReadProducer <R > producer = new ReadProducer <>(discardAll , channel );
243
245
discardAll .setProducer (producer );
244
246
readProducer = producer ;
245
247
}
246
248
}
247
249
248
- protected final Subscriber <? super Connection < R , W >> getNewConnectionSub () {
249
- return newConnectionSub ;
250
+ protected final Subscriber <? super Channel > getNewChannelSub () {
251
+ return newChannelSub ;
250
252
}
251
253
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
+ }
259
265
} else {
260
266
channel .close (); // Closing the connection if not sent to a subscriber.
261
267
}
@@ -271,39 +277,39 @@ private void resetConnectionInputSubscriber() {
271
277
}
272
278
273
279
private void newConnectionInputSubscriber (final Channel channel , final Subscriber <? super R > subscriber ,
274
- final Connection < R , W > connection , boolean replace ) {
280
+ boolean replace ) {
275
281
final Subscriber <? super R > connInputSub = null == readProducer ? null : readProducer .subscriber ;
276
282
if (isValidToEmit (connInputSub )) {
277
283
if (!replace ) {
278
284
/*Allow only once concurrent input subscriber but allow concatenated subscribers*/
279
285
subscriber .onError (ONLY_ONE_CONN_INPUT_SUB_ALLOWED );
280
286
} else {
281
- setNewReadProducer (channel , subscriber , connection );
287
+ setNewReadProducer (channel , subscriber );
282
288
connInputSub .onCompleted ();
283
289
}
284
290
} else if (raiseErrorOnInputSubscription ) {
285
291
subscriber .onError (LAZY_CONN_INPUT_SUB );
286
292
} else {
287
- setNewReadProducer (channel , subscriber , connection );
293
+ setNewReadProducer (channel , subscriber );
288
294
}
289
295
}
290
296
291
- private void setNewReadProducer (Channel channel , Subscriber <? super R > subscriber , Connection < R , W > connection ) {
297
+ private void setNewReadProducer (Channel channel , Subscriber <? super R > subscriber ) {
292
298
final ReadProducer <R > producer = new ReadProducer <>(subscriber , channel );
293
299
subscriber .setProducer (producer );
294
- onNewReadSubscriber (connection , subscriber );
300
+ onNewReadSubscriber (subscriber );
295
301
readProducer = producer ;
296
302
}
297
303
298
- private void replaceConnectionInputSubscriber (ConnectionInputSubscriberReplaceEvent <R , W > event ) {
304
+ private void replaceConnectionInputSubscriber (Channel channel , ConnectionInputSubscriberReplaceEvent <R , W > event ) {
299
305
ConnectionInputSubscriberEvent <R , W > newSubEvent = event .getNewSubEvent ();
300
- newConnectionInputSubscriber (newSubEvent . getConnection (). unsafeNettyChannel () , newSubEvent .getSubscriber (),
301
- newSubEvent . getConnection (), true );
306
+ newConnectionInputSubscriber (channel , newSubEvent .getSubscriber (),
307
+ true );
302
308
}
303
309
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 ();
307
313
} else {
308
314
event .getSubscriber ().onError (ONLY_ONE_CONN_SUB_ALLOWED );
309
315
}
0 commit comments