Fix calculate reader headroom

This commit is contained in:
世界 2024-05-17 13:20:35 +08:00
parent 4ab8cac5eb
commit 8fb1634c9a
No known key found for this signature in database
GPG key ID: CD109927C34A63C4

View file

@ -98,6 +98,29 @@ func CalculateFrontHeadroom(writer any) int {
return headroom
}
func calculateReaderFrontHeadroom(reader any) int {
var headroom int
for {
if reader == nil {
break
}
if lazyRoom, isLazy := reader.(LazyHeadroom); isLazy && lazyRoom.LazyHeadroom() {
return DefaultHeadroom
}
if headroomWriter, needHeadroom := reader.(FrontHeadroom); needHeadroom {
headroom += headroomWriter.FrontHeadroom()
}
if upstreamWriter, hasUpstreamWriter := reader.(WithUpstreamReader); hasUpstreamWriter {
reader = upstreamWriter.UpstreamReader()
} else if upstream, hasUpstream := reader.(common.WithUpstream); hasUpstream {
reader = upstream.Upstream()
} else {
break
}
}
return headroom
}
func CalculateRearHeadroom(writer any) int {
var headroom int
for {
@ -131,14 +154,15 @@ type WriterWithMTU interface {
func CalculateMTU(reader any, writer any) int {
readerMTU := calculateReaderMTU(reader)
readerHeadroom := calculateReaderFrontHeadroom(reader)
writerMTU := calculateWriterMTU(writer)
if readerMTU > writerMTU {
return readerMTU
return readerMTU + readerHeadroom
}
if writerMTU > buf.BufferSize {
return 0
}
return writerMTU
return writerMTU + readerHeadroom
}
func calculateReaderMTU(reader any) int {