Skip to content

Commit ddd9420

Browse files
committed
mailbox: simplify ClientStatus methods
To make a future refactor diff more simple, the ClientStatus methods are slightly simplified here.
1 parent f2a6d39 commit ddd9420

File tree

1 file changed

+25
-37
lines changed

1 file changed

+25
-37
lines changed

mailbox/client_conn.go

Lines changed: 25 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -233,51 +233,40 @@ func (c *ClientConn) Done() <-chan struct{} {
233233
return c.quit
234234
}
235235

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-
264236
// setStatus is used to set a new connection status and to call the onNewStatus
265237
// callback function.
266238
func (c *ClientConn) setStatus(s ClientStatus) {
267239
c.statusMu.Lock()
268240
defer c.statusMu.Unlock()
269241

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+
}
272250

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) {
277251
c.status = s
278252
c.onNewStatus(s)
279253
}
280254

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+
281270
// recvFromStream is used to receive a payload from the receive socket.
282271
// The function is passed to and used by the gbn connection.
283272
// 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) {
314303
log.Debugf("Client: got error message from receive "+
315304
"socket: %v", err)
316305

317-
c.setStatusByErr(err)
318-
306+
c.setStatus(statusFromError(err))
319307
c.createReceiveMailBox(ctx, retryWait)
320308
c.receiveStreamMu.Unlock()
321309
continue
@@ -378,7 +366,7 @@ func (c *ClientConn) sendToStream(ctx context.Context, payload []byte) error {
378366
log.Debugf("Client: got failure on send socket, "+
379367
"re-trying: %v", err)
380368

381-
c.setStatusByErr(err)
369+
c.setStatus(statusFromError(err))
382370
c.createSendMailBox(ctx, retryWait)
383371
c.sendStreamMu.Unlock()
384372
continue

0 commit comments

Comments
 (0)