Skip to content

Commit

Permalink
make delSegment idemopotent
Browse files Browse the repository at this point in the history
  • Loading branch information
xtaci committed Jan 3, 2019
1 parent 2dc62d8 commit 9b1a605
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions kcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,11 @@ func (kcp *KCP) newSegment(size int) (seg segment) {
}

// delSegment recycles a KCP segment
func (kcp *KCP) delSegment(seg segment) {
xmitBuf.Put(seg.data)
func (kcp *KCP) delSegment(seg *segment) {
if seg.data != nil {
xmitBuf.Put(seg.data)
seg.data = nil
}
}

// PeekSize checks the size of next message in the recv queue
Expand Down Expand Up @@ -239,7 +242,7 @@ func (kcp *KCP) Recv(buffer []byte) (n int) {
buffer = buffer[len(seg.data):]
n += len(seg.data)
count++
kcp.delSegment(*seg)
kcp.delSegment(seg)
if seg.frg == 0 {
break
}
Expand Down Expand Up @@ -412,7 +415,7 @@ func (kcp *KCP) parse_una(una uint32) {
for k := range kcp.snd_buf {
seg := &kcp.snd_buf[k]
if _itimediff(una, seg.sn) > 0 {
kcp.delSegment(*seg)
kcp.delSegment(seg)
count++
} else {
break
Expand Down

0 comments on commit 9b1a605

Please sign in to comment.