Skip to content

Commit fdf518c

Browse files
committed
arjan's comments
1 parent 2ceced5 commit fdf518c

1 file changed

Lines changed: 18 additions & 29 deletions

File tree

internal/transport/http2_server.go

Lines changed: 18 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -573,8 +573,9 @@ func (t *http2Server) operateHeaders(ctx context.Context, frame *http2.MetaHeade
573573

574574
if s.ctx.Err() != nil {
575575
t.mu.Unlock()
576+
st := status.New(codes.DeadlineExceeded, context.DeadlineExceeded.Error())
576577
// Early abort in case the timeout was zero or so low it already fired.
577-
t.writeEarlyAbort(s.id, s.contentSubtype, status.New(codes.DeadlineExceeded, context.DeadlineExceeded.Error()), http.StatusOK, !frame.StreamEnded())
578+
t.writeEarlyAbort(s.id, s.contentSubtype, st, http.StatusOK, !frame.StreamEnded())
578579
return nil
579580
}
580581

@@ -933,34 +934,26 @@ func appendHeaderFieldsFromMD(headerFields []hpack.HeaderField, md metadata.MD)
933934
return headerFields
934935
}
935936

936-
// checkForHeaderListSize checks if the header list size exceeds the limit set
937-
// by the peer. It returns false if the limit is exceeded.
938-
func checkForHeaderListSize(hf []hpack.HeaderField, maxSendHeaderListSize *uint32) bool {
939-
if maxSendHeaderListSize == nil {
937+
func (t *http2Server) checkForHeaderListSize(hf []hpack.HeaderField) bool {
938+
if t.maxSendHeaderListSize == nil {
940939
return true
941940
}
942941
var sz int64
943942
for _, f := range hf {
944-
if sz += int64(f.Size()); sz > int64(*maxSendHeaderListSize) {
943+
if sz += int64(f.Size()); sz > int64(*t.maxSendHeaderListSize) {
944+
if t.logger.V(logLevel) {
945+
t.logger.Infof("Header list size to send violates the maximum size (%d bytes) set by client", *t.maxSendHeaderListSize)
946+
}
945947
return false
946948
}
947949
}
948950
return true
949951
}
950952

951-
func (t *http2Server) checkForHeaderListSize(it any) bool {
952-
hdrFrame := it.(*headerFrame)
953-
if !checkForHeaderListSize(hdrFrame.hf, t.maxSendHeaderListSize) {
954-
if t.logger.V(logLevel) {
955-
t.logger.Infof("Header list size to send violates the maximum size (%d bytes) set by client", *t.maxSendHeaderListSize)
956-
}
957-
return false
958-
}
959-
return true
960-
}
961-
962-
// buildEarlyAbortHF builds the header fields for an early abort response.
963-
func buildEarlyAbortHF(httpStatus uint32, contentSubtype string, stat *status.Status) []hpack.HeaderField {
953+
// writeEarlyAbort sends an early abort response with the given HTTP status and
954+
// gRPC status. If the header list size exceeds the peer's limit, it sends a
955+
// RST_STREAM instead.
956+
func (t *http2Server) writeEarlyAbort(streamID uint32, contentSubtype string, stat *status.Status, httpStatus uint32, rst bool) {
964957
hf := []hpack.HeaderField{
965958
{Name: ":status", Value: strconv.Itoa(int(httpStatus))},
966959
{Name: "content-type", Value: grpcutil.ContentType(contentSubtype)},
@@ -969,19 +962,15 @@ func buildEarlyAbortHF(httpStatus uint32, contentSubtype string, stat *status.St
969962
}
970963
if p := istatus.RawStatusProto(stat); len(p.GetDetails()) > 0 {
971964
stBytes, err := proto.Marshal(p)
965+
if err != nil {
966+
t.logger.Errorf("Failed to marshal rpc status: %s, error: %v", pretty.ToJSON(p), err)
967+
}
972968
if err == nil {
973969
hf = append(hf, hpack.HeaderField{Name: grpcStatusDetailsBinHeader, Value: encodeBinHeader(stBytes)})
974970
}
975971
}
976-
return hf
977-
}
978-
979-
// writeEarlyAbort sends an early abort response with the given HTTP status and gRPC status.
980-
// If the header list size exceeds the peer's limit, it sends a RST_STREAM instead.
981-
func (t *http2Server) writeEarlyAbort(streamID uint32, contentSubtype string, stat *status.Status, httpStatus uint32, rst bool) {
982-
hf := buildEarlyAbortHF(httpStatus, contentSubtype, stat)
983972
success, _ := t.controlBuf.executeAndPut(func() bool {
984-
return checkForHeaderListSize(hf, t.maxSendHeaderListSize)
973+
return t.checkForHeaderListSize(hf)
985974
}, &earlyAbortStream{
986975
streamID: streamID,
987976
rst: rst,
@@ -1052,7 +1041,7 @@ func (t *http2Server) writeHeaderLocked(s *ServerStream) error {
10521041
endStream: false,
10531042
onWrite: t.setResetPingStrikes,
10541043
}
1055-
success, err := t.controlBuf.executeAndPut(func() bool { return t.checkForHeaderListSize(hf) }, hf)
1044+
success, err := t.controlBuf.executeAndPut(func() bool { return t.checkForHeaderListSize(hf.hf) }, hf)
10561045
if !success {
10571046
if err != nil {
10581047
return err
@@ -1122,7 +1111,7 @@ func (t *http2Server) writeStatus(s *ServerStream, st *status.Status) error {
11221111
}
11231112

11241113
success, err := t.controlBuf.executeAndPut(func() bool {
1125-
return t.checkForHeaderListSize(trailingHeader)
1114+
return t.checkForHeaderListSize(trailingHeader.hf)
11261115
}, nil)
11271116
if !success {
11281117
if err != nil {

0 commit comments

Comments
 (0)