27
27
import org .reactivestreams .Subscription ;
28
28
import reactor .core .CoreSubscriber ;
29
29
import reactor .core .Fuseable ;
30
- import reactor .core .Scannable ;
31
30
import reactor .core .publisher .Flux ;
32
31
import reactor .core .publisher .Mono ;
33
32
import reactor .core .publisher .Operators ;
34
- import reactor .core .publisher .Sinks ;
35
33
36
34
/** An implementation of {@link DuplexConnection} that connects inside the same JVM. */
37
35
final class LocalDuplexConnection implements DuplexConnection {
@@ -40,7 +38,7 @@ final class LocalDuplexConnection implements DuplexConnection {
40
38
private final ByteBufAllocator allocator ;
41
39
private final Flux <ByteBuf > in ;
42
40
43
- private final Sinks . Empty <Void > onClose ;
41
+ private final Mono <Void > onClose ;
44
42
45
43
private final UnboundedProcessor out ;
46
44
@@ -58,7 +56,7 @@ final class LocalDuplexConnection implements DuplexConnection {
58
56
ByteBufAllocator allocator ,
59
57
Flux <ByteBuf > in ,
60
58
UnboundedProcessor out ,
61
- Sinks . Empty <Void > onClose ) {
59
+ Mono <Void > onClose ) {
62
60
this .address = new LocalSocketAddress (name );
63
61
this .allocator = Objects .requireNonNull (allocator , "allocator must not be null" );
64
62
this .in = Objects .requireNonNull (in , "in must not be null" );
@@ -69,24 +67,23 @@ final class LocalDuplexConnection implements DuplexConnection {
69
67
@ Override
70
68
public void dispose () {
71
69
out .onComplete ();
72
- onClose .tryEmitEmpty ();
73
70
}
74
71
75
72
@ Override
76
- @ SuppressWarnings ("ConstantConditions" )
77
73
public boolean isDisposed () {
78
- return onClose . scan ( Scannable . Attr . TERMINATED ) || onClose . scan ( Scannable . Attr . CANCELLED );
74
+ return out . isDisposed ( );
79
75
}
80
76
81
77
@ Override
82
78
public Mono <Void > onClose () {
83
- return onClose . asMono () ;
79
+ return onClose ;
84
80
}
85
81
86
82
@ Override
87
83
public Flux <ByteBuf > receive () {
88
84
return in .transform (
89
- Operators .<ByteBuf , ByteBuf >lift ((__ , actual ) -> new ByteBufReleaserOperator (actual )));
85
+ Operators .<ByteBuf , ByteBuf >lift (
86
+ (__ , actual ) -> new ByteBufReleaserOperator (actual , this )));
90
87
}
91
88
92
89
@ Override
@@ -119,11 +116,14 @@ static class ByteBufReleaserOperator
119
116
implements CoreSubscriber <ByteBuf >, Subscription , Fuseable .QueueSubscription <ByteBuf > {
120
117
121
118
final CoreSubscriber <? super ByteBuf > actual ;
119
+ final LocalDuplexConnection parent ;
122
120
123
121
Subscription s ;
124
122
125
- public ByteBufReleaserOperator (CoreSubscriber <? super ByteBuf > actual ) {
123
+ public ByteBufReleaserOperator (
124
+ CoreSubscriber <? super ByteBuf > actual , LocalDuplexConnection parent ) {
126
125
this .actual = actual ;
126
+ this .parent = parent ;
127
127
}
128
128
129
129
@ Override
@@ -136,17 +136,22 @@ public void onSubscribe(Subscription s) {
136
136
137
137
@ Override
138
138
public void onNext (ByteBuf buf ) {
139
- actual .onNext (buf );
140
- buf .release ();
139
+ try {
140
+ actual .onNext (buf );
141
+ } finally {
142
+ buf .release ();
143
+ }
141
144
}
142
145
143
146
@ Override
144
147
public void onError (Throwable t ) {
148
+ parent .out .onError (t );
145
149
actual .onError (t );
146
150
}
147
151
148
152
@ Override
149
153
public void onComplete () {
154
+ parent .out .onComplete ();
150
155
actual .onComplete ();
151
156
}
152
157
@@ -158,6 +163,7 @@ public void request(long n) {
158
163
@ Override
159
164
public void cancel () {
160
165
s .cancel ();
166
+ parent .out .onComplete ();
161
167
}
162
168
163
169
@ Override
0 commit comments