25
25
import io .rsocket .DuplexConnection ;
26
26
import io .rsocket .Payload ;
27
27
import io .rsocket .frame .FrameType ;
28
+ import io .rsocket .plugins .RequestInterceptor ;
28
29
import java .time .Duration ;
29
30
import java .util .concurrent .atomic .AtomicLongFieldUpdater ;
30
31
import org .reactivestreams .Subscription ;
@@ -51,21 +52,31 @@ final class FireAndForgetRequesterMono extends Mono<Void> implements Subscriptio
51
52
final RequesterResponderSupport requesterResponderSupport ;
52
53
final DuplexConnection connection ;
53
54
55
+ @ Nullable final RequestInterceptor requestInterceptor ;
56
+
54
57
FireAndForgetRequesterMono (Payload payload , RequesterResponderSupport requesterResponderSupport ) {
55
58
this .allocator = requesterResponderSupport .getAllocator ();
56
59
this .payload = payload ;
57
60
this .mtu = requesterResponderSupport .getMtu ();
58
61
this .maxFrameLength = requesterResponderSupport .getMaxFrameLength ();
59
62
this .requesterResponderSupport = requesterResponderSupport ;
60
63
this .connection = requesterResponderSupport .getDuplexConnection ();
64
+ this .requestInterceptor = requesterResponderSupport .getRequestInterceptor ();
61
65
}
62
66
63
67
@ Override
64
68
public void subscribe (CoreSubscriber <? super Void > actual ) {
65
69
long previousState = markSubscribed (STATE , this );
66
70
if (isSubscribedOrTerminated (previousState )) {
67
- Operators .error (
68
- actual , new IllegalStateException ("FireAndForgetMono allows only a single Subscriber" ));
71
+ final IllegalStateException e =
72
+ new IllegalStateException ("FireAndForgetMono allows only a single Subscriber" );
73
+
74
+ final RequestInterceptor requestInterceptor = this .requestInterceptor ;
75
+ if (requestInterceptor != null ) {
76
+ requestInterceptor .onReject (e , FrameType .REQUEST_FNF , null );
77
+ }
78
+
79
+ Operators .error (actual , e );
69
80
return ;
70
81
}
71
82
@@ -76,14 +87,28 @@ public void subscribe(CoreSubscriber<? super Void> actual) {
76
87
try {
77
88
if (!isValid (mtu , this .maxFrameLength , p , false )) {
78
89
lazyTerminate (STATE , this );
79
- p . release ();
80
- actual . onError (
90
+
91
+ final IllegalArgumentException e =
81
92
new IllegalArgumentException (
82
- String .format (INVALID_PAYLOAD_ERROR_MESSAGE , this .maxFrameLength )));
93
+ String .format (INVALID_PAYLOAD_ERROR_MESSAGE , this .maxFrameLength ));
94
+ final RequestInterceptor requestInterceptor = this .requestInterceptor ;
95
+ if (requestInterceptor != null ) {
96
+ requestInterceptor .onReject (e , FrameType .REQUEST_FNF , p .metadata ());
97
+ }
98
+
99
+ p .release ();
100
+
101
+ actual .onError (e );
83
102
return ;
84
103
}
85
104
} catch (IllegalReferenceCountException e ) {
86
105
lazyTerminate (STATE , this );
106
+
107
+ final RequestInterceptor requestInterceptor = this .requestInterceptor ;
108
+ if (requestInterceptor != null ) {
109
+ requestInterceptor .onReject (e , FrameType .REQUEST_FNF , null );
110
+ }
111
+
87
112
actual .onError (e );
88
113
return ;
89
114
}
@@ -93,26 +118,54 @@ public void subscribe(CoreSubscriber<? super Void> actual) {
93
118
streamId = this .requesterResponderSupport .getNextStreamId ();
94
119
} catch (Throwable t ) {
95
120
lazyTerminate (STATE , this );
121
+
122
+ final Throwable ut = Exceptions .unwrap (t );
123
+ final RequestInterceptor requestInterceptor = this .requestInterceptor ;
124
+ if (requestInterceptor != null ) {
125
+ requestInterceptor .onReject (ut , FrameType .REQUEST_FNF , p .metadata ());
126
+ }
127
+
96
128
p .release ();
97
- actual .onError (Exceptions .unwrap (t ));
129
+
130
+ actual .onError (ut );
98
131
return ;
99
132
}
100
133
134
+ final RequestInterceptor interceptor = this .requestInterceptor ;
135
+ if (interceptor != null ) {
136
+ interceptor .onStart (streamId , FrameType .REQUEST_FNF , p .metadata ());
137
+ }
138
+
101
139
try {
102
140
if (isTerminated (this .state )) {
103
141
p .release ();
142
+
143
+ if (interceptor != null ) {
144
+ interceptor .onCancel (streamId );
145
+ }
146
+
104
147
return ;
105
148
}
106
149
107
150
sendReleasingPayload (
108
151
streamId , FrameType .REQUEST_FNF , mtu , p , this .connection , this .allocator , true );
109
152
} catch (Throwable e ) {
110
153
lazyTerminate (STATE , this );
154
+
155
+ if (interceptor != null ) {
156
+ interceptor .onTerminate (streamId , e );
157
+ }
158
+
111
159
actual .onError (e );
112
160
return ;
113
161
}
114
162
115
163
lazyTerminate (STATE , this );
164
+
165
+ if (interceptor != null ) {
166
+ interceptor .onTerminate (streamId , null );
167
+ }
168
+
116
169
actual .onComplete ();
117
170
}
118
171
@@ -137,19 +190,41 @@ public Void block(Duration m) {
137
190
public Void block () {
138
191
long previousState = markSubscribed (STATE , this );
139
192
if (isSubscribedOrTerminated (previousState )) {
140
- throw new IllegalStateException ("FireAndForgetMono allows only a single Subscriber" );
193
+ final IllegalStateException e =
194
+ new IllegalStateException ("FireAndForgetMono allows only a single Subscriber" );
195
+ final RequestInterceptor requestInterceptor = this .requestInterceptor ;
196
+ if (requestInterceptor != null ) {
197
+ requestInterceptor .onReject (e , FrameType .REQUEST_FNF , null );
198
+ }
199
+ throw e ;
141
200
}
142
201
143
202
final Payload p = this .payload ;
144
203
try {
145
204
if (!isValid (this .mtu , this .maxFrameLength , p , false )) {
146
205
lazyTerminate (STATE , this );
206
+
207
+ final IllegalArgumentException e =
208
+ new IllegalArgumentException (
209
+ String .format (INVALID_PAYLOAD_ERROR_MESSAGE , this .maxFrameLength ));
210
+
211
+ final RequestInterceptor requestInterceptor = this .requestInterceptor ;
212
+ if (requestInterceptor != null ) {
213
+ requestInterceptor .onReject (e , FrameType .REQUEST_FNF , p .metadata ());
214
+ }
215
+
147
216
p .release ();
148
- throw new IllegalArgumentException (
149
- String . format ( INVALID_PAYLOAD_ERROR_MESSAGE , this . maxFrameLength )) ;
217
+
218
+ throw e ;
150
219
}
151
220
} catch (IllegalReferenceCountException e ) {
152
221
lazyTerminate (STATE , this );
222
+
223
+ final RequestInterceptor requestInterceptor = this .requestInterceptor ;
224
+ if (requestInterceptor != null ) {
225
+ requestInterceptor .onReject (e , FrameType .REQUEST_FNF , null );
226
+ }
227
+
153
228
throw Exceptions .propagate (e );
154
229
}
155
230
@@ -158,10 +233,22 @@ public Void block() {
158
233
streamId = this .requesterResponderSupport .getNextStreamId ();
159
234
} catch (Throwable t ) {
160
235
lazyTerminate (STATE , this );
236
+
237
+ final RequestInterceptor requestInterceptor = this .requestInterceptor ;
238
+ if (requestInterceptor != null ) {
239
+ requestInterceptor .onReject (Exceptions .unwrap (t ), FrameType .REQUEST_FNF , p .metadata ());
240
+ }
241
+
161
242
p .release ();
243
+
162
244
throw Exceptions .propagate (t );
163
245
}
164
246
247
+ final RequestInterceptor interceptor = this .requestInterceptor ;
248
+ if (interceptor != null ) {
249
+ interceptor .onStart (streamId , FrameType .REQUEST_FNF , p .metadata ());
250
+ }
251
+
165
252
try {
166
253
sendReleasingPayload (
167
254
streamId ,
@@ -173,10 +260,20 @@ public Void block() {
173
260
true );
174
261
} catch (Throwable e ) {
175
262
lazyTerminate (STATE , this );
263
+
264
+ if (interceptor != null ) {
265
+ interceptor .onTerminate (streamId , e );
266
+ }
267
+
176
268
throw Exceptions .propagate (e );
177
269
}
178
270
179
271
lazyTerminate (STATE , this );
272
+
273
+ if (interceptor != null ) {
274
+ interceptor .onTerminate (streamId , null );
275
+ }
276
+
180
277
return null ;
181
278
}
182
279
0 commit comments