Fix copy count

This commit is contained in:
世界 2024-11-24 13:57:11 +08:00
parent fa5355e99e
commit c8f251c668
No known key found for this signature in database
GPG key ID: CD109927C34A63C4

View file

@ -29,21 +29,18 @@ func Copy(destination io.Writer, source io.Reader) (n int64, err error) {
if cachedSrc, isCached := source.(N.CachedReader); isCached { if cachedSrc, isCached := source.(N.CachedReader); isCached {
cachedBuffer := cachedSrc.ReadCached() cachedBuffer := cachedSrc.ReadCached()
if cachedBuffer != nil { if cachedBuffer != nil {
if !cachedBuffer.IsEmpty() { dataLen := cachedBuffer.Len()
dataLen := cachedBuffer.Len() _, err = destination.Write(cachedBuffer.Bytes())
for _, counter := range readCounters {
counter(int64(dataLen))
}
_, err = destination.Write(cachedBuffer.Bytes())
if err != nil {
cachedBuffer.Release()
return
}
for _, counter := range writeCounters {
counter(int64(dataLen))
}
}
cachedBuffer.Release() cachedBuffer.Release()
if err != nil {
return
}
for _, counter := range readCounters {
counter(int64(dataLen))
}
for _, counter := range writeCounters {
counter(int64(dataLen))
}
continue continue
} }
} }
@ -139,9 +136,6 @@ func CopyExtendedWithPool(originSource io.Reader, destination N.ExtendedWriter,
return return
} }
dataLen := buffer.Len() dataLen := buffer.Len()
for _, counter := range readCounters {
counter(int64(dataLen))
}
options.PostReturn(buffer) options.PostReturn(buffer)
err = destination.WriteBuffer(buffer) err = destination.WriteBuffer(buffer)
if err != nil { if err != nil {
@ -152,6 +146,9 @@ func CopyExtendedWithPool(originSource io.Reader, destination N.ExtendedWriter,
return return
} }
n += int64(dataLen) n += int64(dataLen)
for _, counter := range readCounters {
counter(int64(dataLen))
}
for _, counter := range writeCounters { for _, counter := range writeCounters {
counter(int64(dataLen)) counter(int64(dataLen))
} }
@ -258,9 +255,6 @@ func CopyPacketWithPool(originSource N.PacketReader, destination N.PacketWriter,
return return
} }
dataLen := buffer.Len() dataLen := buffer.Len()
for _, counter := range readCounters {
counter(int64(dataLen))
}
options.PostReturn(buffer) options.PostReturn(buffer)
err = destination.WritePacket(buffer, destinationAddress) err = destination.WritePacket(buffer, destinationAddress)
if err != nil { if err != nil {
@ -270,6 +264,9 @@ func CopyPacketWithPool(originSource N.PacketReader, destination N.PacketWriter,
} }
return return
} }
for _, counter := range readCounters {
counter(int64(dataLen))
}
for _, counter := range writeCounters { for _, counter := range writeCounters {
counter(int64(dataLen)) counter(int64(dataLen))
} }
@ -282,9 +279,6 @@ func WritePacketWithPool(originSource N.PacketReader, destination N.PacketWriter
options := N.NewReadWaitOptions(nil, destination) options := N.NewReadWaitOptions(nil, destination)
var notFirstTime bool var notFirstTime bool
for _, packetBuffer := range packetBuffers { for _, packetBuffer := range packetBuffers {
for _, counter := range readCounters {
counter(int64(packetBuffer.Buffer.Len()))
}
buffer := options.Copy(packetBuffer.Buffer) buffer := options.Copy(packetBuffer.Buffer)
dataLen := buffer.Len() dataLen := buffer.Len()
err = destination.WritePacket(buffer, packetBuffer.Destination) err = destination.WritePacket(buffer, packetBuffer.Destination)
@ -296,6 +290,9 @@ func WritePacketWithPool(originSource N.PacketReader, destination N.PacketWriter
} }
return return
} }
for _, counter := range readCounters {
counter(int64(dataLen))
}
for _, counter := range writeCounters { for _, counter := range writeCounters {
counter(int64(dataLen)) counter(int64(dataLen))
} }