mirror of https://github.com/v2ray/v2ray-core
pool for data and ack segments
parent
bd4752a6ab
commit
f8e6927581
|
@ -113,6 +113,7 @@ func (l *AckList) Flush(current uint32, rto uint32) {
|
||||||
l.dirty = false
|
l.dirty = false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if l.dirty || !seg.IsEmpty() {
|
if l.dirty || !seg.IsEmpty() {
|
||||||
for _, number := range l.flushCandidates {
|
for _, number := range l.flushCandidates {
|
||||||
if seg.IsFull() {
|
if seg.IsFull() {
|
||||||
|
@ -121,9 +122,10 @@ func (l *AckList) Flush(current uint32, rto uint32) {
|
||||||
seg.PutNumber(number)
|
seg.PutNumber(number)
|
||||||
}
|
}
|
||||||
l.writer.Write(seg)
|
l.writer.Write(seg)
|
||||||
seg.Release()
|
|
||||||
l.dirty = false
|
l.dirty = false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
seg.Release()
|
||||||
}
|
}
|
||||||
|
|
||||||
type ReceivingWorker struct {
|
type ReceivingWorker struct {
|
||||||
|
@ -234,6 +236,7 @@ func (w *ReceivingWorker) Write(seg Segment) error {
|
||||||
ackSeg.Conv = w.conn.meta.Conversation
|
ackSeg.Conv = w.conn.meta.Conversation
|
||||||
ackSeg.ReceivingNext = w.nextNumber
|
ackSeg.ReceivingNext = w.nextNumber
|
||||||
ackSeg.ReceivingWindow = w.nextNumber + w.windowSize
|
ackSeg.ReceivingWindow = w.nextNumber + w.windowSize
|
||||||
|
ackSeg.Option = 0
|
||||||
if w.conn.State() == StateReadyToClose {
|
if w.conn.State() == StateReadyToClose {
|
||||||
ackSeg.Option = SegmentOptionClose
|
ackSeg.Option = SegmentOptionClose
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
package kcp
|
package kcp
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"sync"
|
||||||
|
|
||||||
"v2ray.com/core/common/buf"
|
"v2ray.com/core/common/buf"
|
||||||
"v2ray.com/core/common/serial"
|
"v2ray.com/core/common/serial"
|
||||||
)
|
)
|
||||||
|
@ -38,6 +40,12 @@ const (
|
||||||
DataSegmentOverhead = 18
|
DataSegmentOverhead = 18
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var dataSegmentPool = sync.Pool{
|
||||||
|
New: func() interface{} {
|
||||||
|
return new(DataSegment)
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
type DataSegment struct {
|
type DataSegment struct {
|
||||||
Conv uint16
|
Conv uint16
|
||||||
Option SegmentOption
|
Option SegmentOption
|
||||||
|
@ -51,7 +59,11 @@ type DataSegment struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewDataSegment() *DataSegment {
|
func NewDataSegment() *DataSegment {
|
||||||
return new(DataSegment)
|
seg := dataSegmentPool.Get().(*DataSegment)
|
||||||
|
seg.Conv = 0
|
||||||
|
seg.timeout = 0
|
||||||
|
seg.transmit = 0
|
||||||
|
return seg
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *DataSegment) parse(conv uint16, cmd Command, opt SegmentOption, buf []byte) (bool, []byte) {
|
func (s *DataSegment) parse(conv uint16, cmd Command, opt SegmentOption, buf []byte) (bool, []byte) {
|
||||||
|
@ -123,6 +135,15 @@ func (s *DataSegment) ByteSize() int32 {
|
||||||
func (s *DataSegment) Release() {
|
func (s *DataSegment) Release() {
|
||||||
s.payload.Release()
|
s.payload.Release()
|
||||||
s.payload = nil
|
s.payload = nil
|
||||||
|
dataSegmentPool.Put(s)
|
||||||
|
}
|
||||||
|
|
||||||
|
var ackSegmentPool = sync.Pool{
|
||||||
|
New: func() interface{} {
|
||||||
|
return &AckSegment{
|
||||||
|
NumberList: make([]uint32, 0, 16),
|
||||||
|
}
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
type AckSegment struct {
|
type AckSegment struct {
|
||||||
|
@ -137,9 +158,9 @@ type AckSegment struct {
|
||||||
const ackNumberLimit = 128
|
const ackNumberLimit = 128
|
||||||
|
|
||||||
func NewAckSegment() *AckSegment {
|
func NewAckSegment() *AckSegment {
|
||||||
return &AckSegment{
|
seg := ackSegmentPool.Get().(*AckSegment)
|
||||||
NumberList: make([]uint32, 0, ackNumberLimit),
|
seg.NumberList = seg.NumberList[:0]
|
||||||
}
|
return seg
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *AckSegment) parse(conv uint16, cmd Command, opt SegmentOption, buf []byte) (bool, []byte) {
|
func (s *AckSegment) parse(conv uint16, cmd Command, opt SegmentOption, buf []byte) (bool, []byte) {
|
||||||
|
@ -219,7 +240,7 @@ func (s *AckSegment) Bytes() buf.Supplier {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *AckSegment) Release() {
|
func (s *AckSegment) Release() {
|
||||||
s.NumberList = nil
|
ackSegmentPool.Put(s)
|
||||||
}
|
}
|
||||||
|
|
||||||
type CmdOnlySegment struct {
|
type CmdOnlySegment struct {
|
||||||
|
|
Loading…
Reference in New Issue