diff --git a/common/network/thread.go b/common/network/thread.go index 58ccebb..ac3ca69 100644 --- a/common/network/thread.go +++ b/common/network/thread.go @@ -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 {