Add lazy headroom

This commit is contained in:
世界 2022-10-07 20:12:20 +08:00
parent c4e9bf11fa
commit ec72b91c12
No known key found for this signature in database
GPG key ID: CD109927C34A63C4
2 changed files with 51 additions and 18 deletions

View file

@ -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))
}

View file

@ -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
}