@@ -556,6 +556,19 @@ func (t *http2Client) createHeaderFields(ctx context.Context, callHdr *CallHdr)
556556 // Make the slice of certain predictable size to reduce allocations made by append.
557557 hfLen := 7 // :method, :scheme, :path, :authority, content-type, user-agent, te
558558 hfLen += len (authData ) + len (callAuthData )
559+ registeredCompressors := t .registeredCompressors
560+ if callHdr .PreviousAttempts > 0 {
561+ hfLen ++
562+ }
563+ if callHdr .SendCompress != "" {
564+ hfLen ++
565+ }
566+ if registeredCompressors != "" {
567+ hfLen ++
568+ }
569+ if _ , ok := ctx .Deadline (); ok {
570+ hfLen ++
571+ }
559572 headerFields := make ([]hpack.HeaderField , 0 , hfLen )
560573 headerFields = append (headerFields , hpack.HeaderField {Name : ":method" , Value : "POST" })
561574 headerFields = append (headerFields , hpack.HeaderField {Name : ":scheme" , Value : t .scheme })
@@ -568,7 +581,6 @@ func (t *http2Client) createHeaderFields(ctx context.Context, callHdr *CallHdr)
568581 headerFields = append (headerFields , hpack.HeaderField {Name : "grpc-previous-rpc-attempts" , Value : strconv .Itoa (callHdr .PreviousAttempts )})
569582 }
570583
571- registeredCompressors := t .registeredCompressors
572584 if callHdr .SendCompress != "" {
573585 headerFields = append (headerFields , hpack.HeaderField {Name : "grpc-encoding" , Value : callHdr .SendCompress })
574586 // Include the outgoing compressor name when compressor is not registered
0 commit comments