diff --git a/common/bufio/vectorised.go b/common/bufio/vectorised.go index 8caf29f..d3979ae 100644 --- a/common/bufio/vectorised.go +++ b/common/bufio/vectorised.go @@ -88,10 +88,7 @@ type NetVectorisedWriterWrapper struct { func (w *NetVectorisedWriterWrapper) WriteVectorised(buffers []*buf.Buffer) error { defer buf.ReleaseMulti(buffers) - netBuffers := make(net.Buffers, 0, len(buffers)) - for _, buffer := range buffers { - netBuffers = append(netBuffers, buffer.Bytes()) - } + netBuffers := net.Buffers(buf.ToSliceMulti(buffers)) return common.Error(netBuffers.WriteTo(w.upstream)) } diff --git a/common/network/thread.go b/common/network/thread.go index ae16590..73a2dd6 100644 --- a/common/network/thread.go +++ b/common/network/thread.go @@ -55,6 +55,8 @@ func IsSafePacketReader(reader any) ThreadSafePacketReader { return nil } +const DefaultHeadroom = 1024 + type FrontHeadroom interface { FrontHeadroom() int } @@ -63,28 +65,62 @@ type RearHeadroom interface { RearHeadroom() int } +type LazyHeadroom interface { + LazyHeadroom() bool +} + func CalculateFrontHeadroom(writer any) int { var headroom int - if headroomWriter, needHeadroom := writer.(FrontHeadroom); needHeadroom { - headroom = headroomWriter.FrontHeadroom() - } - if upstreamWriter, hasUpstreamWriter := writer.(WithUpstreamWriter); hasUpstreamWriter { - headroom += CalculateFrontHeadroom(upstreamWriter.UpstreamWriter()) - } else if upstream, hasUpstream := writer.(common.WithUpstream); hasUpstream { - headroom += CalculateFrontHeadroom(upstream.Upstream()) + for { + if lazyRoom, isLazy := writer.(LazyHeadroom); isLazy && lazyRoom.LazyHeadroom() { + return DefaultHeadroom + } + if headroomWriter, needHeadroom := writer.(FrontHeadroom); needHeadroom { + headroom += headroomWriter.FrontHeadroom() + } + if upstreamWriter, hasUpstreamWriter := writer.(WithUpstreamWriter); hasUpstreamWriter { + upstream := upstreamWriter.UpstreamWriter() + if upstream != nil { + writer = upstream + continue + } + } + if upstream, hasUpstream := writer.(common.WithUpstream); hasUpstream { + upstreamWriter := upstream.Upstream() + if upstreamWriter != nil { + writer = upstreamWriter + continue + } + } + break } return headroom } func CalculateRearHeadroom(writer any) int { var headroom int - if headroomWriter, needHeadroom := writer.(RearHeadroom); needHeadroom { - headroom = headroomWriter.RearHeadroom() - } - if upstreamWriter, hasUpstreamWriter := writer.(WithUpstreamWriter); hasUpstreamWriter { - headroom += CalculateRearHeadroom(upstreamWriter.UpstreamWriter()) - } else if upstream, hasUpstream := writer.(common.WithUpstream); hasUpstream { - headroom += CalculateRearHeadroom(upstream.Upstream()) + for { + if lazyRoom, isLazy := writer.(LazyHeadroom); isLazy && lazyRoom.LazyHeadroom() { + return DefaultHeadroom + } + if headroomWriter, needHeadroom := writer.(RearHeadroom); needHeadroom { + headroom += headroomWriter.RearHeadroom() + } + if upstreamWriter, hasUpstreamWriter := writer.(WithUpstreamWriter); hasUpstreamWriter { + upstream := upstreamWriter.UpstreamWriter() + if upstream != nil { + writer = upstream + continue + } + } + if upstream, hasUpstream := writer.(common.WithUpstream); hasUpstream { + upstreamWriter := upstream.Upstream() + if upstreamWriter != nil { + writer = upstreamWriter + continue + } + } + break } return headroom }