18
18
19
19
import io .netty .buffer .ByteBuf ;
20
20
import io .netty .buffer .ByteBufAllocator ;
21
+ import io .netty .util .CharsetUtil ;
21
22
import io .rsocket .DuplexConnection ;
22
23
import io .rsocket .exceptions .ConnectionErrorException ;
23
24
import io .rsocket .exceptions .Exceptions ;
@@ -54,6 +55,7 @@ public class ClientRSocketSession
54
55
final Retry retry ;
55
56
final boolean cleanupStoreOnKeepAlive ;
56
57
final ByteBuf resumeToken ;
58
+ final String session ;
57
59
58
60
volatile Subscription s ;
59
61
static final AtomicReferenceFieldUpdater <ClientRSocketSession , Subscription > S =
@@ -71,20 +73,30 @@ public ClientRSocketSession(
71
73
Retry retry ,
72
74
boolean cleanupStoreOnKeepAlive ) {
73
75
this .resumeToken = resumeToken ;
76
+ this .session = resumeToken .toString (CharsetUtil .UTF_8 );
74
77
this .connectionFactory =
75
78
connectionFactory .flatMap (
76
79
dc -> {
80
+ final long impliedPosition = resumableFramesStore .frameImpliedPosition ();
81
+ final long position = resumableFramesStore .framePosition ();
77
82
dc .sendFrame (
78
83
0 ,
79
84
ResumeFrameCodec .encode (
80
85
dc .alloc (),
81
86
resumeToken .retain (),
82
87
// server uses this to release its cache
83
- resumableFramesStore . frameImpliedPosition () , // observed on the client side
88
+ impliedPosition , // observed on the client side
84
89
// server uses this to check whether there is no mismatch
85
- resumableFramesStore . framePosition () // sent from the client sent
90
+ position // sent from the client sent
86
91
));
87
- logger .debug ("Resume Frame has been sent" );
92
+
93
+ if (logger .isDebugEnabled ()) {
94
+ logger .debug (
95
+ "Side[client]|Session[{}]. ResumeFrame[impliedPosition[{}], position[{}]] has been sent." ,
96
+ session ,
97
+ impliedPosition ,
98
+ position );
99
+ }
88
100
89
101
return connectionTransformer .apply (dc );
90
102
});
@@ -105,7 +117,12 @@ void reconnect(int index) {
105
117
if (this .s == Operators .cancelledSubscription ()
106
118
&& S .compareAndSet (this , Operators .cancelledSubscription (), null )) {
107
119
keepAliveSupport .stop ();
108
- logger .debug ("Connection[" + index + "] is lost. Reconnecting to resume..." );
120
+ if (logger .isDebugEnabled ()) {
121
+ logger .debug (
122
+ "Side[client]|Session[{}]. Connection[{}] is lost. Reconnecting to resume..." ,
123
+ session ,
124
+ index );
125
+ }
109
126
connectionFactory .retryWhen (retry ).timeout (resumeSessionDuration ).subscribe (this );
110
127
}
111
128
}
@@ -155,21 +172,30 @@ public void onNext(Tuple2<ByteBuf, DuplexConnection> tuple2) {
155
172
DuplexConnection nextDuplexConnection = tuple2 .getT2 ();
156
173
157
174
if (!Operators .terminate (S , this )) {
158
- logger .debug ("Session has already been expired. Terminating received connection" );
175
+ if (logger .isDebugEnabled ()) {
176
+ logger .debug (
177
+ "Side[client]|Session[{}]. Session has already been expired. Terminating received connection" ,
178
+ session );
179
+ }
159
180
final ConnectionErrorException connectionErrorException =
160
181
new ConnectionErrorException ("resumption_server=[Session Expired]" );
161
182
nextDuplexConnection .sendErrorAndClose (connectionErrorException );
183
+ nextDuplexConnection .receive ().subscribe ().dispose ();
162
184
return ;
163
185
}
164
186
165
187
final int streamId = FrameHeaderCodec .streamId (shouldBeResumeOKFrame );
166
188
if (streamId != 0 ) {
167
- logger .debug (
168
- "Illegal first frame received. RESUME_OK frame must be received before any others. Terminating received connection" );
169
- resumableConnection .dispose ();
189
+ if (logger .isDebugEnabled ()) {
190
+ logger .debug (
191
+ "Side[client]|Session[{}]. Illegal first frame received. RESUME_OK frame must be received before any others. Terminating received connection" ,
192
+ session );
193
+ }
170
194
final ConnectionErrorException connectionErrorException =
171
195
new ConnectionErrorException ("RESUME_OK frame must be received before any others" );
196
+ resumableConnection .dispose (connectionErrorException );
172
197
nextDuplexConnection .sendErrorAndClose (connectionErrorException );
198
+ nextDuplexConnection .receive ().subscribe ().dispose ();
173
199
return ;
174
200
}
175
201
@@ -183,7 +209,8 @@ public void onNext(Tuple2<ByteBuf, DuplexConnection> tuple2) {
183
209
final long position = resumableFramesStore .framePosition ();
184
210
final long impliedPosition = resumableFramesStore .frameImpliedPosition ();
185
211
logger .debug (
186
- "ResumeOK FRAME received. ServerResumeState{observedFramesPosition[{}]}. ClientResumeState{observedFramesPosition[{}], sentFramesPosition[{}]}" ,
212
+ "Side[client]|Session[{}]. ResumeOK FRAME received. ServerResumeState[remoteImpliedPosition[{}]]. ClientResumeState[impliedPosition[{}], position[{}]]" ,
213
+ session ,
187
214
remoteImpliedPos ,
188
215
impliedPosition ,
189
216
position );
@@ -194,42 +221,54 @@ public void onNext(Tuple2<ByteBuf, DuplexConnection> tuple2) {
194
221
}
195
222
} catch (IllegalStateException e ) {
196
223
logger .debug ("Exception occurred while releasing frames in the frameStore" , e );
197
- resumableConnection .dispose ();
224
+ resumableConnection .dispose (e );
198
225
final ConnectionErrorException t = new ConnectionErrorException (e .getMessage (), e );
199
226
nextDuplexConnection .sendErrorAndClose (t );
227
+ nextDuplexConnection .receive ().subscribe ().dispose ();
200
228
return ;
201
229
}
202
230
203
231
if (resumableConnection .connect (nextDuplexConnection )) {
204
232
keepAliveSupport .start ();
205
- logger .debug ("Session has been resumed successfully" );
233
+ if (logger .isDebugEnabled ()) {
234
+ logger .debug (
235
+ "Side[client]|Session[{}]. Session has been resumed successfully" , session );
236
+ }
206
237
} else {
207
- logger .debug ("Session has already been expired. Terminating received connection" );
238
+ if (logger .isDebugEnabled ()) {
239
+ logger .debug (
240
+ "Side[client]|Session[{}]. Session has already been expired. Terminating received connection" ,
241
+ session );
242
+ }
208
243
final ConnectionErrorException connectionErrorException =
209
244
new ConnectionErrorException ("resumption_server_pos=[Session Expired]" );
210
245
nextDuplexConnection .sendErrorAndClose (connectionErrorException );
246
+ nextDuplexConnection .receive ().subscribe ().dispose ();
211
247
}
212
248
} else {
213
249
logger .debug (
214
- "Mismatching remote and local state. Expected RemoteImpliedPosition[{}] to be greater or equal to the LocalPosition[{}]. Terminating received connection" ,
250
+ "Side[client]|Session[{}]. Mismatching remote and local state. Expected RemoteImpliedPosition[{}] to be greater or equal to the LocalPosition[{}]. Terminating received connection" ,
251
+ session ,
215
252
remoteImpliedPos ,
216
253
position );
217
- resumableConnection .dispose ();
218
254
final ConnectionErrorException connectionErrorException =
219
255
new ConnectionErrorException ("resumption_server_pos=[" + remoteImpliedPos + "]" );
256
+ resumableConnection .dispose (connectionErrorException );
220
257
nextDuplexConnection .sendErrorAndClose (connectionErrorException );
258
+ nextDuplexConnection .receive ().subscribe ().dispose ();
221
259
}
222
260
} else if (frameType == FrameType .ERROR ) {
223
261
final RuntimeException exception = Exceptions .from (0 , shouldBeResumeOKFrame );
224
262
logger .debug ("Received error frame. Terminating received connection" , exception );
225
- resumableConnection .dispose ();
263
+ resumableConnection .dispose (exception );
226
264
} else {
227
265
logger .debug (
228
266
"Illegal first frame received. RESUME_OK frame must be received before any others. Terminating received connection" );
229
- resumableConnection .dispose ();
230
267
final ConnectionErrorException connectionErrorException =
231
268
new ConnectionErrorException ("RESUME_OK frame must be received before any others" );
269
+ resumableConnection .dispose (connectionErrorException );
232
270
nextDuplexConnection .sendErrorAndClose (connectionErrorException );
271
+ nextDuplexConnection .receive ().subscribe ().dispose ();
233
272
}
234
273
}
235
274
@@ -239,7 +278,7 @@ public void onError(Throwable t) {
239
278
Operators .onErrorDropped (t , currentContext ());
240
279
}
241
280
242
- resumableConnection .dispose ();
281
+ resumableConnection .dispose (t );
243
282
}
244
283
245
284
@ Override
0 commit comments