Skip to content

Commit 6c98be3

Browse files
authored
refactor(transport): extract shared stream state handling logic in loopyWriter.processData() (#9125)
Fixes: #9117 Refactors the duplicated post write stream state handling in `loopyWriter.processData()` present in `internal/transport/controlbuf.go` into a shared helper `l.updateStreamAfterWrite()`. Both empty and non empty DATA frame paths now share the same stream state handling logic. This change also adds the stream quota check `(int(l.oiws) - str.bytesOutStanding)` to the empty frame path. This is safe because empty DATA frames send 0 bytes, so `bytesOutStanding` doesn't change. RELEASE NOTES: n/a
1 parent bcaa6f4 commit 6c98be3

1 file changed

Lines changed: 13 additions & 32 deletions

File tree

‎internal/transport/controlbuf.go‎

Lines changed: 13 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -956,39 +956,16 @@ func (l *loopyWriter) processData() (bool, error) {
956956
// from data is copied to h to make as big as the maximum possible HTTP2 frame
957957
// size.
958958

959-
if len(dataItem.h) == 0 && reader.Remaining() == 0 { // Empty data frame
960-
// Client sends out empty data frame with endStream = true
961-
if err := l.framer.writeData(dataItem.streamID, dataItem.endStream, nil); err != nil {
962-
return false, err
963-
}
964-
str.itl.dequeue() // remove the empty data item from stream
965-
reader.Close()
966-
if str.itl.isEmpty() {
967-
str.state = empty
968-
} else if trailer, ok := str.itl.peek().(*headerFrame); ok { // the next item is trailers.
969-
if err := l.writeHeader(trailer.streamID, trailer.endStream, trailer.hf, trailer.onWrite); err != nil {
970-
return false, err
971-
}
972-
if err := l.cleanupStreamHandler(trailer.cleanup); err != nil {
973-
return false, err
974-
}
975-
} else {
976-
l.activeStreams.enqueue(str)
977-
}
978-
return false, nil
979-
}
980-
959+
isEmpty := len(dataItem.h) == 0 && reader.Remaining() == 0
981960
// Figure out the maximum size we can send
982961
maxSize := http2MaxFrameLen
983-
if strQuota := int(l.oiws) - str.bytesOutStanding; strQuota <= 0 { // stream-level flow control.
962+
strQuota := int(l.oiws) - str.bytesOutStanding
963+
if strQuota <= 0 && !isEmpty { // stream-level flow control.
984964
str.state = waitingOnStreamQuota
985965
return false, nil
986-
} else if maxSize > strQuota {
987-
maxSize = strQuota
988-
}
989-
if maxSize > int(l.sendQuota) { // connection-level flow control.
990-
maxSize = int(l.sendQuota)
991966
}
967+
maxSize = min(maxSize, max(strQuota, 0))
968+
maxSize = min(maxSize, int(l.sendQuota)) // connection-level flow control.
992969
// Compute how much of the header and data we can send within quota and max frame length
993970
hSize := min(maxSize, len(dataItem.h))
994971
dSize := min(maxSize-hSize, reader.Remaining())
@@ -1039,19 +1016,23 @@ func (l *loopyWriter) processData() (bool, error) {
10391016
reader.Close()
10401017
str.itl.dequeue()
10411018
}
1019+
return false, l.updateStreamAfterWrite(str)
1020+
}
1021+
1022+
func (l *loopyWriter) updateStreamAfterWrite(str *outStream) error {
10421023
if str.itl.isEmpty() {
10431024
str.state = empty
1044-
} else if trailer, ok := str.itl.peek().(*headerFrame); ok { // The next item is trailers.
1025+
} else if trailer, ok := str.itl.peek().(*headerFrame); ok { // the next item is trailers.
10451026
if err := l.writeHeader(trailer.streamID, trailer.endStream, trailer.hf, trailer.onWrite); err != nil {
1046-
return false, err
1027+
return err
10471028
}
10481029
if err := l.cleanupStreamHandler(trailer.cleanup); err != nil {
1049-
return false, err
1030+
return err
10501031
}
10511032
} else if int(l.oiws)-str.bytesOutStanding <= 0 { // Ran out of stream quota.
10521033
str.state = waitingOnStreamQuota
10531034
} else { // Otherwise add it back to the list of active streams.
10541035
l.activeStreams.enqueue(str)
10551036
}
1056-
return false, nil
1037+
return nil
10571038
}

0 commit comments

Comments
 (0)