@@ -233,51 +233,40 @@ func (c *ClientConn) Done() <-chan struct{} {
233
233
return c .quit
234
234
}
235
235
236
- // setStatusByErr parses the given error and sets the connection status
237
- // accordingly.
238
- func (c * ClientConn ) setStatusByErr (err error ) {
239
- c .statusMu .Lock ()
240
- defer c .statusMu .Unlock ()
241
-
242
- switch {
243
- case strings .Contains (err .Error (), "stream not found" ):
244
- c .setStatusUnsafe (ClientStatusSessionNotFound )
245
-
246
- case strings .Contains (
247
- err .Error (), "stream occupied" ):
248
-
249
- c .setStatusUnsafe (ClientStatusSessionInUse )
250
-
251
- default :
252
- // We give previously set status priority if it provides more
253
- // detail than the NotConnected status.
254
- if c .status == ClientStatusSessionInUse ||
255
- c .status == ClientStatusSessionNotFound {
256
-
257
- return
258
- }
259
-
260
- c .setStatusUnsafe (ClientStatusNotConnected )
261
- }
262
- }
263
-
264
236
// setStatus is used to set a new connection status and to call the onNewStatus
265
237
// callback function.
266
238
func (c * ClientConn ) setStatus (s ClientStatus ) {
267
239
c .statusMu .Lock ()
268
240
defer c .statusMu .Unlock ()
269
241
270
- c .setStatusUnsafe (s )
271
- }
242
+ // We give previously set status priority if it provides more detail
243
+ // than the NotConnected status.
244
+ if s == ClientStatusNotConnected &&
245
+ (c .status == ClientStatusSessionInUse ||
246
+ c .status == ClientStatusSessionNotFound ) {
247
+
248
+ return
249
+ }
272
250
273
- // setStatusUnsafe is used to set a new connection status and to call the
274
- // onNewStatus callback function. Note that this function is not thread safe
275
- // and must only be called if the statusMu lock is being held.
276
- func (c * ClientConn ) setStatusUnsafe (s ClientStatus ) {
277
251
c .status = s
278
252
c .onNewStatus (s )
279
253
}
280
254
255
+ // statusFromError parses the given error and returns an appropriate Client
256
+ // connection status.
257
+ func statusFromError (err error ) ClientStatus {
258
+ switch {
259
+ case strings .Contains (err .Error (), "stream not found" ):
260
+ return ClientStatusSessionNotFound
261
+
262
+ case strings .Contains (err .Error (), "stream occupied" ):
263
+ return ClientStatusSessionInUse
264
+
265
+ default :
266
+ return ClientStatusNotConnected
267
+ }
268
+ }
269
+
281
270
// recvFromStream is used to receive a payload from the receive socket.
282
271
// The function is passed to and used by the gbn connection.
283
272
// It therefore takes in and reacts on the cancellation of a context so that
@@ -314,8 +303,7 @@ func (c *ClientConn) recvFromStream(ctx context.Context) ([]byte, error) {
314
303
log .Debugf ("Client: got error message from receive " +
315
304
"socket: %v" , err )
316
305
317
- c .setStatusByErr (err )
318
-
306
+ c .setStatus (statusFromError (err ))
319
307
c .createReceiveMailBox (ctx , retryWait )
320
308
c .receiveStreamMu .Unlock ()
321
309
continue
@@ -378,7 +366,7 @@ func (c *ClientConn) sendToStream(ctx context.Context, payload []byte) error {
378
366
log .Debugf ("Client: got failure on send socket, " +
379
367
"re-trying: %v" , err )
380
368
381
- c .setStatusByErr ( err )
369
+ c .setStatus ( statusFromError ( err ) )
382
370
c .createSendMailBox (ctx , retryWait )
383
371
c .sendStreamMu .Unlock ()
384
372
continue
0 commit comments