Skip to content

Commit

Permalink
Merge pull request #40 from libp2p/reduce-goto-usage
Browse files Browse the repository at this point in the history
reduce usage of goto
  • Loading branch information
marten-seemann authored Feb 16, 2021
2 parents 4847d7c + a1b9bdb commit e8e3876
Showing 1 changed file with 12 additions and 18 deletions.
30 changes: 12 additions & 18 deletions stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,12 @@ START:
s.recvLock.Lock()
if s.recvBuf.Len() == 0 {
s.recvLock.Unlock()
goto WAIT
select {
case <-s.recvNotifyCh:
goto START
case <-s.readDeadline.wait():
return 0, ErrTimeout
}
}

// Read any bytes
Expand All @@ -112,14 +117,6 @@ START:
// Send a window update potentially
err = s.sendWindowUpdate()
return n, err

WAIT:
select {
case <-s.recvNotifyCh:
goto START
case <-s.readDeadline.wait():
return 0, ErrTimeout
}
}

// Write is used to write to the stream
Expand Down Expand Up @@ -164,7 +161,12 @@ START:
// If there is no data available, block
window := atomic.LoadUint32(&s.sendWindow)
if window == 0 {
goto WAIT
select {
case <-s.sendNotifyCh:
goto START
case <-s.writeDeadline.wait():
return 0, ErrTimeout
}
}

// Determine the flags if any
Expand All @@ -184,14 +186,6 @@ START:

// Unlock
return int(max), err

WAIT:
select {
case <-s.sendNotifyCh:
goto START
case <-s.writeDeadline.wait():
return 0, ErrTimeout
}
}

// sendFlags determines any flags that are appropriate
Expand Down

0 comments on commit e8e3876

Please sign in to comment.