@@ -50,6 +50,7 @@ class RSocketServer implements ResponderRSocket {
50
50
private final Function <Frame , ? extends Payload > frameDecoder ;
51
51
private final Consumer <Throwable > errorConsumer ;
52
52
53
+ private final Map <Integer , LimitableRequestPublisher > sendingLimitableSubscriptions ;
53
54
private final Map <Integer , Subscription > sendingSubscriptions ;
54
55
private final Map <Integer , Processor <Payload , Payload >> channelProcessors ;
55
56
@@ -81,6 +82,7 @@ class RSocketServer implements ResponderRSocket {
81
82
this .connection = connection ;
82
83
this .frameDecoder = frameDecoder ;
83
84
this .errorConsumer = errorConsumer ;
85
+ this .sendingLimitableSubscriptions = Collections .synchronizedMap (new IntObjectHashMap <>());
84
86
this .sendingSubscriptions = Collections .synchronizedMap (new IntObjectHashMap <>());
85
87
this .channelProcessors = Collections .synchronizedMap (new IntObjectHashMap <>());
86
88
@@ -89,7 +91,13 @@ class RSocketServer implements ResponderRSocket {
89
91
this .sendProcessor = new UnboundedProcessor <>();
90
92
91
93
connection
92
- .send (sendProcessor )
94
+ .send (
95
+ sendProcessor .doOnRequest (
96
+ r -> {
97
+ for (LimitableRequestPublisher lrp : sendingLimitableSubscriptions .values ()) {
98
+ lrp .increaseInternalLimit (r );
99
+ }
100
+ }))
93
101
.doFinally (this ::handleSendProcessorCancel )
94
102
.subscribe (null , this ::handleSendProcessorError );
95
103
@@ -135,6 +143,17 @@ private void handleSendProcessorError(Throwable t) {
135
143
}
136
144
});
137
145
146
+ sendingLimitableSubscriptions
147
+ .values ()
148
+ .forEach (
149
+ subscription -> {
150
+ try {
151
+ subscription .cancel ();
152
+ } catch (Throwable e ) {
153
+ errorConsumer .accept (e );
154
+ }
155
+ });
156
+
138
157
channelProcessors
139
158
.values ()
140
159
.forEach (
@@ -163,6 +182,17 @@ private void handleSendProcessorCancel(SignalType t) {
163
182
}
164
183
});
165
184
185
+ sendingLimitableSubscriptions
186
+ .values ()
187
+ .forEach (
188
+ subscription -> {
189
+ try {
190
+ subscription .cancel ();
191
+ } catch (Throwable e ) {
192
+ errorConsumer .accept (e );
193
+ }
194
+ });
195
+
166
196
channelProcessors
167
197
.values ()
168
198
.forEach (
@@ -258,6 +288,9 @@ private void cleanup() {
258
288
private synchronized void cleanUpSendingSubscriptions () {
259
289
sendingSubscriptions .values ().forEach (Subscription ::cancel );
260
290
sendingSubscriptions .clear ();
291
+
292
+ sendingLimitableSubscriptions .values ().forEach (Subscription ::cancel );
293
+ sendingLimitableSubscriptions .clear ();
261
294
}
262
295
263
296
private synchronized void cleanUpChannelProcessors () {
@@ -373,12 +406,12 @@ private void handleStream(int streamId, Flux<Payload> response, int initialReque
373
406
.transform (
374
407
frameFlux -> {
375
408
LimitableRequestPublisher <Payload > payloads =
376
- LimitableRequestPublisher .wrap (frameFlux );
377
- sendingSubscriptions .put (streamId , payloads );
409
+ LimitableRequestPublisher .wrap (frameFlux , sendProcessor . available () );
410
+ sendingLimitableSubscriptions .put (streamId , payloads );
378
411
payloads .increaseRequestLimit (initialRequestN );
379
412
return payloads ;
380
413
})
381
- .doFinally (signalType -> sendingSubscriptions .remove (streamId ))
414
+ .doFinally (signalType -> sendingLimitableSubscriptions .remove (streamId ))
382
415
.subscribe (
383
416
payload -> {
384
417
final Frame frame = Frame .PayloadFrame .from (streamId , FrameType .NEXT , payload );
@@ -423,6 +456,11 @@ private void handleKeepAliveFrame(Frame frame) {
423
456
424
457
private void handleCancelFrame (int streamId ) {
425
458
Subscription subscription = sendingSubscriptions .remove (streamId );
459
+
460
+ if (subscription == null ) {
461
+ subscription = sendingLimitableSubscriptions .get (streamId );
462
+ }
463
+
426
464
if (subscription != null ) {
427
465
subscription .cancel ();
428
466
}
@@ -434,7 +472,11 @@ private void handleError(int streamId, Throwable t) {
434
472
}
435
473
436
474
private void handleRequestN (int streamId , Frame frame ) {
437
- final Subscription subscription = sendingSubscriptions .get (streamId );
475
+ Subscription subscription = sendingSubscriptions .get (streamId );
476
+
477
+ if (subscription == null ) {
478
+ subscription = sendingLimitableSubscriptions .get (streamId );
479
+ }
438
480
if (subscription != null ) {
439
481
int n = Frame .RequestN .requestN (frame );
440
482
subscription .request (n >= Integer .MAX_VALUE ? Long .MAX_VALUE : n );
0 commit comments