@@ -528,6 +528,7 @@ type serverConn struct {
528
528
headerTableSize uint32
529
529
peerMaxHeaderListSize uint32 // zero means unknown (default)
530
530
canonHeader map [string ]string // http2-lower-case -> Go-Canonical-Case
531
+ canonHeaderKeysSize int // canonHeader keys size in bytes
531
532
writingFrame bool // started writing a frame (on serve goroutine or separate)
532
533
writingFrameAsync bool // started a frame on its own goroutine but haven't heard back on wroteFrameCh
533
534
needsFrameFlush bool // last frame write wasn't a flush
@@ -704,6 +705,13 @@ func (sc *serverConn) condlogf(err error, format string, args ...interface{}) {
704
705
}
705
706
}
706
707
708
+ // maxCachedCanonicalHeadersKeysSize is an arbitrarily-chosen limit on the size
709
+ // of the entries in the canonHeader cache.
710
+ // This should be larger than the size of unique, uncommon header keys likely to
711
+ // be sent by the peer, while not so high as to permit unreasonable memory usage
712
+ // if the peer sends an unbounded number of unique header keys.
713
+ const maxCachedCanonicalHeadersKeysSize = 2048
714
+
707
715
func (sc * serverConn ) canonicalHeader (v string ) string {
708
716
sc .serveG .check ()
709
717
buildCommonHeaderMapsOnce ()
@@ -719,14 +727,10 @@ func (sc *serverConn) canonicalHeader(v string) string {
719
727
sc .canonHeader = make (map [string ]string )
720
728
}
721
729
cv = http .CanonicalHeaderKey (v )
722
- // maxCachedCanonicalHeaders is an arbitrarily-chosen limit on the number of
723
- // entries in the canonHeader cache. This should be larger than the number
724
- // of unique, uncommon header keys likely to be sent by the peer, while not
725
- // so high as to permit unreasonable memory usage if the peer sends an unbounded
726
- // number of unique header keys.
727
- const maxCachedCanonicalHeaders = 32
728
- if len (sc .canonHeader ) < maxCachedCanonicalHeaders {
730
+ size := 100 + len (v )* 2 // 100 bytes of map overhead + key + value
731
+ if sc .canonHeaderKeysSize + size <= maxCachedCanonicalHeadersKeysSize {
729
732
sc .canonHeader [v ] = cv
733
+ sc .canonHeaderKeysSize += size
730
734
}
731
735
return cv
732
736
}
0 commit comments