36
36
import io .rsocket .lease .ResponderLeaseHandler ;
37
37
import java .util .function .Consumer ;
38
38
import java .util .function .LongConsumer ;
39
+ import javax .annotation .Nullable ;
39
40
import org .reactivestreams .Processor ;
40
41
import org .reactivestreams .Publisher ;
41
42
import org .reactivestreams .Subscriber ;
@@ -302,7 +303,7 @@ private void handleFrame(ByteBuf frame) {
302
303
case REQUEST_STREAM :
303
304
int streamInitialRequestN = RequestStreamFrameFlyweight .initialRequestN (frame );
304
305
Payload streamPayload = payloadDecoder .apply (frame );
305
- handleStream (streamId , requestStream (streamPayload ), streamInitialRequestN );
306
+ handleStream (streamId , requestStream (streamPayload ), streamInitialRequestN , null );
306
307
break ;
307
308
case REQUEST_CHANNEL :
308
309
int channelInitialRequestN = RequestChannelFrameFlyweight .initialRequestN (frame );
@@ -433,7 +434,11 @@ protected void hookFinally(SignalType type) {
433
434
response .doOnDiscard (ReferenceCounted .class , DROPPED_ELEMENTS_CONSUMER ).subscribe (subscriber );
434
435
}
435
436
436
- private void handleStream (int streamId , Flux <Payload > response , int initialRequestN ) {
437
+ private void handleStream (
438
+ int streamId ,
439
+ Flux <Payload > response ,
440
+ int initialRequestN ,
441
+ @ Nullable UnicastProcessor <Payload > requestChannel ) {
437
442
final BaseSubscriber <Payload > subscriber =
438
443
new BaseSubscriber <Payload >() {
439
444
@@ -446,6 +451,17 @@ protected void hookOnSubscribe(Subscription s) {
446
451
protected void hookOnNext (Payload payload ) {
447
452
if (!PayloadValidationUtils .isValid (mtu , payload )) {
448
453
payload .release ();
454
+ // specifically for requestChannel case so when Payload is invalid we will not be
455
+ // sending CancelFrame and ErrorFrame
456
+ // Note: CancelFrame is redundant and due to spec
457
+ // (https://github.com/rsocket/rsocket/blob/master/Protocol.md#request-channel)
458
+ // Upon receiving an ERROR[APPLICATION_ERROR|REJECTED|CANCELED|INVALID], the stream is
459
+ // terminated on both Requester and Responder.
460
+ // Upon sending an ERROR[APPLICATION_ERROR|REJECTED|CANCELED|INVALID], the stream is
461
+ // terminated on both the Requester and Responder.
462
+ if (requestChannel != null ) {
463
+ channelProcessors .remove (streamId , requestChannel );
464
+ }
449
465
cancel ();
450
466
final IllegalArgumentException t =
451
467
new IllegalArgumentException (INVALID_PAYLOAD_ERROR_MESSAGE );
@@ -495,9 +511,6 @@ private void handleChannel(int streamId, Payload payload, int initialRequestN) {
495
511
496
512
Flux <Payload > payloads =
497
513
frames
498
- .doOnCancel (
499
- () -> sendProcessor .onNext (CancelFrameFlyweight .encode (allocator , streamId )))
500
- .doOnError (t -> handleError (streamId , t ))
501
514
.doOnRequest (
502
515
new LongConsumer () {
503
516
boolean first = true ;
@@ -511,10 +524,19 @@ public void accept(long l) {
511
524
} else {
512
525
n = l ;
513
526
}
514
- sendProcessor .onNext (RequestNFrameFlyweight .encode (allocator , streamId , n ));
527
+ if (n > 0 ) {
528
+ sendProcessor .onNext (RequestNFrameFlyweight .encode (allocator , streamId , n ));
529
+ }
530
+ }
531
+ })
532
+ .doFinally (
533
+ signalType -> {
534
+ if (channelProcessors .remove (streamId , frames )) {
535
+ if (signalType == SignalType .CANCEL ) {
536
+ sendProcessor .onNext (CancelFrameFlyweight .encode (allocator , streamId ));
537
+ }
515
538
}
516
539
})
517
- .doFinally (signalType -> channelProcessors .remove (streamId ))
518
540
.doOnDiscard (ReferenceCounted .class , DROPPED_ELEMENTS_CONSUMER );
519
541
520
542
// not chained, as the payload should be enqueued in the Unicast processor before this method
@@ -523,9 +545,9 @@ public void accept(long l) {
523
545
frames .onNext (payload );
524
546
525
547
if (responderRSocket != null ) {
526
- handleStream (streamId , requestChannel (payload , payloads ), initialRequestN );
548
+ handleStream (streamId , requestChannel (payload , payloads ), initialRequestN , frames );
527
549
} else {
528
- handleStream (streamId , requestChannel (payloads ), initialRequestN );
550
+ handleStream (streamId , requestChannel (payloads ), initialRequestN , frames );
529
551
}
530
552
}
531
553
0 commit comments