310 Commits
Author SHA1 Message Date
xtaci 2693fa8e71 perf: reduce heap allocations and fix memory leaks across multiple components
Optimizations:
- ringbuffer: optimize Len(), IsEmpty(), Discard(), Clear() methods with
  branchless arithmetic and simplified logic
- fec: fix memory leak in shardHeap.Pop() by setting element to nil before
  truncating slice, preventing GC from holding stale references
- timedsched: fix memory leak in timedFuncHeap.Pop() with same nil-before-truncate
  pattern; optimize prepend() using slice swap instead of append
- autotune: add sortCache field to Autotuner to reuse slice allocation in
  FindPeriod(), avoiding repeated allocations during period detection
- snmp: replace fmt.Sprint with strconv.FormatUint for better performance
  in Copy() and Reset() methods
- entropy: remove defer overhead in rngAES.Read() and rngChacha8.Read()
  hot paths for reduced function call cost
- bufferpool: pre-allocate ErrBufferPoolSizeExceeded error; reset slice
  length on Put() to allow proper reuse
- sess: remove defer overhead in 6 setter methods (SetWriteDelay,
  SetWindowSize, SetStreamMode, SetACKNoDelay, SetDUP, SetNoDelay)

These changes reduce memory allocations, fix potential memory leaks in
heap-based data structures, and eliminate defer overhead in frequently
called methods.
2026-01-29 12:22:33 +08:00
xtaci 53c14d4570 refactor: unify and simplify OOB handler interface for UDPSession
- Replace atomic.Pointer[func([]byte)] with atomic.Value for OOB callback storage to improve compatibility.
- Rename SetCallbackForOOB to SetOOBHandler and GetMaxSizeForOOB to GetOOBMaxSize for clearer, consistent API naming.
- Add locking in SendOOB to ensure thread safety during OOB packet construction.
- Update related test cases in sess_test.go to use the new interface names.
- Revise comments and documentation for better clarity and maintainability.

user takes care of the nil handler

use 0xffffffff as the seqid of OOB

return error on nil handler

fix race in test

shorten test data size

add more test on OOB

enhance Error test

allow nil from sender side

add OOBPackets in snmp

use dummy handler instead of resetting the atomic.Value

add test for dummy handler

remove a comment

formalize callback function prototype

use const

rename to expose

Add comments to clarify KCP input and FEC decoding order for FEC packets.

fix test

comments

add TestOOB_OneSideHandler
2025-12-27 23:25:03 +08:00
Lonny Wong eb7d8ee32f Add out-of-band (OOB) data support 2025-12-27 10:12:56 +08:00
xtaci bca9b95082 refine struct to support oob 2025-12-24 20:40:47 +08:00
xtaci f83359c85f fix: should return insteading of break 2025-12-18 16:50:19 +08:00
xtaci e74b1f39d3 fix a comment
fix comment
2025-12-14 20:32:27 +08:00
xtaci 651ba3b014 optimize comments 2025-12-12 22:36:34 +08:00
xtaci e929e2cfba Revert "use simple way to control accept queue"
This reverts commit 6f3b813b19.
2025-12-11 21:40:44 +08:00
xtaci 1dd1bea5f0 Revert "fix handling accept queue"
This reverts commit 3396395742.
2025-12-11 21:40:31 +08:00
xtaci 3396395742 fix handling accept queue 2025-12-11 21:32:50 +08:00
xtaci fcbca0b517 SetMtu at the start of the session 2025-12-11 17:14:58 +08:00
xtaci 6f3b813b19 use simple way to control accept queue 2025-12-11 13:27:02 +08:00
xtaci 75a223bd68 add an explicit setting headerSize to 0 2025-12-11 13:03:40 +08:00
xtaci e26683cb34 record error of min packet size 2025-12-11 12:19:18 +08:00
Coia Prant 752f25858e feat: support AEAD 2025-12-10 16:49:39 +08:00
xtaci 048e1714d9 add more comments for clarity 2025-12-10 15:22:27 +08:00
Coia Prant d58f562e75 chores: use global entropy & add chacha8 2025-12-10 12:26:31 +08:00
Coia Prant 011b73564b refactor: udp session (#316) 2025-12-09 16:27:34 +08:00
xtaci cedfa3e298 add a comment to platform 2025-12-09 15:36:00 +08:00
Coia Prant 6504d5b9f9 readLoop 和 tx 优化 (#313)
* chores: use modern platform optimize

* refactor: readloop, tx

* chores: drop older linux compatibility

Go 1.24+ requires linux kernel 3.2
2025-12-09 15:04:29 +08:00
Coia Prant 24b381788c fix: wrapped udp connection (#312) 2025-12-09 14:32:08 +08:00
xtaci d8b28827ef fix mtu calc 2025-12-09 11:13:05 +08:00
xtaci 7fa14a0cee Squashed commit of the following:
commit 6568f08d6c
Author: binhu <jinnblue@126.com>
Date:   Mon Dec 8 22:32:29 2025 +0800

    fix(sess): revert WriteBuffers Send use kcp fragment; make TestSetMTU correct.

commit 7822300957
Author: jinnblue <jinnblue@126.com>
Date:   Mon Dec 8 00:48:01 2025 +0800

    fix(sess): panic caused by mtu, when fec/crypt opened and mss < mtu <= mtulimit

commit b651b55aec
Author: jinnblue <jinnblue@126.com>
Date:   Sun Dec 7 21:58:54 2025 +0800

    fix(sess): use correct field(wd) for write deadlines; kcp output non-blocking chPostProcessing avoid deadlocks(mu)
2025-12-08 22:45:00 +08:00
xtaci 11301cf49f fix write deadline which uses the read deadline value 2025-12-07 22:26:38 +08:00
xtaci a2e1064a35 remove obsolete init() 2025-11-26 12:57:28 +08:00
xtaci ec8e1bf5e3 add IKCP prefix to IFLUSH_xxx 2025-11-25 21:10:56 +08:00
xtaci bbe6fe0bc1 use [type] to format debug message, and changed logoutput to log, DebugLog to debugLog 2025-11-22 20:56:09 +08:00
xtaci 8740a1c5c4 use PacketType to differentiate a regular packet or a packet from FEC 2025-11-22 20:36:42 +08:00
JinnBlue a32123b5dc feat(trace): add trace log to enhance KCP debug (#308)
* feat(trace): add trace log to enhance KCP debug

* feat(trace): update comments
2025-11-22 12:00:20 +08:00
xtaci b243d51f0c seperate xmitBuf into a bufferPool struct 2025-11-19 20:39:22 +08:00
xtaci 38bdf290c4 fix atomic.Store nil value 2025-11-15 22:44:34 +08:00
xtaci f796de1485 use panic when WaitN returns err 2025-11-15 21:16:33 +08:00
xtaci fe591de1d9 implement a userspace pacing 2025-11-15 21:11:44 +08:00
xtaci fd9b680226 optimize chPostProcessing 2025-11-14 16:19:53 +08:00
xtaci dd8804a75f add a dev baclog constant to chPostProcessing 2025-11-14 16:01:21 +08:00
xtaci 02b2f025bd make flush() function to accept an enum instead of bool for better readability 2025-11-14 13:50:42 +08:00
xtaci 949cb75f24 add a flag to temporiarly disable fastack if RTO has not triggered 2025-11-12 16:55:57 +08:00
binhu 6e2b52a4cf fix(sess): DataRace caused by UDPSession SetDeadline and Read/Write in diff goroutine. 2025-11-03 10:09:03 +08:00
xtaci ff7cf0b51c optimize error handling 2025-06-09 14:53:32 +08:00
xtaci 4aa82fae89 use heap to manager shard recovery in fec 2025-06-09 14:42:55 +08:00
xtaci 56de076e83 Squashed commit of the following:
commit 745517fcc4
Author: xtaci <imap@live.com>
Date:   Fri Jun 6 11:13:27 2025 +0800

    fix grow

commit 1b6d34570a
Author: xtaci <imap@live.com>
Date:   Fri Jun 6 10:48:07 2025 +0800

    Squashed commit of the following:

    commit 8f90af5057
    Author: xtaci <imap@live.com>
    Date:   Fri Jun 6 10:46:52 2025 +0800

        add ForEach in ringbuffer

    commit 82250c1f06
    Author: xtaci <imap@live.com>
    Date:   Fri Jun 6 10:29:23 2025 +0800

        add test for grow()

    commit 174824b596
    Author: xtaci <imap@live.com>
    Date:   Fri Jun 6 10:08:05 2025 +0800

        remove fmt.Print

    commit 6534dfe542
    Author: xtaci <imap@live.com>
    Date:   Fri Jun 6 10:06:18 2025 +0800

        rename to ringbuffer

    commit 2f44ba9a82
    Author: xtaci <imap@live.com>
    Date:   Fri Jun 6 10:03:14 2025 +0800

        add comments

    commit 3f49975468
    Author: xtaci <imap@live.com>
    Date:   Fri Jun 6 10:02:14 2025 +0800

        save

    commit c505f434cc
    Author: xtaci <daniel820313@gmail.com>
    Date:   Thu Jun 5 17:47:17 2025 +0800

        add ring

commit 3b4daea2cd
Author: FULI <daniel820313@gmail.com>
Date:   Fri Jun 6 10:30:51 2025 +0800

    RingBuffer (#290)

    * add ring

    * save

    * add comments

    * rename to ringbuffer

    * remove fmt.Print

    * add test for grow()

commit e0169fb60e
Author: xtaci <imap@live.com>
Date:   Sun May 25 00:09:13 2025 +0800

    make fec.go more clear

commit 97d2673e8a
Author: xtaci <imap@live.com>
Date:   Wed May 21 13:55:08 2025 +0800

    add comments to fec.go

commit 5ef7e8042f
Author: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Date:   Tue May 20 10:38:46 2025 +0800

    Bump golang.org/x/net from 0.36.0 to 0.38.0 (#287)

    Bumps [golang.org/x/net](https://github.com/golang/net) from 0.36.0 to 0.38.0.
    - [Commits](https://github.com/golang/net/compare/v0.36.0...v0.38.0)

    ---
    updated-dependencies:
    - dependency-name: golang.org/x/net
      dependency-version: 0.38.0
      dependency-type: direct:production
    ...

    Signed-off-by: dependabot[bot] <support@github.com>
    Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

commit 11ae2d1c3e
Author: xtaci <imap@live.com>
Date:   Tue Apr 15 10:40:08 2025 +0800

    update go.mod

commit 53b48e42c2
Author: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Date:   Tue Apr 15 10:38:02 2025 +0800

    Bump golang.org/x/net from 0.23.0 to 0.36.0 (#286)

    Bumps [golang.org/x/net](https://github.com/golang/net) from 0.23.0 to 0.36.0.
    - [Commits](https://github.com/golang/net/compare/v0.23.0...v0.36.0)

    ---
    updated-dependencies:
    - dependency-name: golang.org/x/net
      dependency-version: 0.36.0
      dependency-type: direct:production
    ...

    Signed-off-by: dependabot[bot] <support@github.com>
    Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

commit f327c26f53
Author: Roman Gurov <44749519+rgurov@users.noreply.github.com>
Date:   Fri Apr 11 05:11:14 2025 +0300

    fix: typo in README.md (#284)

commit c5078bd33b
Author: Roman Gurov <44749519+rgurov@users.noreply.github.com>
Date:   Wed Apr 9 10:44:14 2025 +0300

    fix: make timeout error implement net.Error (#283)

commit 3c579515d6
Author: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Date:   Tue Feb 18 14:33:01 2025 +0800

    Bump golang.org/x/crypto from 0.21.0 to 0.31.0 (#281)

    Bumps [golang.org/x/crypto](https://github.com/golang/crypto) from 0.21.0 to 0.31.0.
    - [Commits](https://github.com/golang/crypto/compare/v0.21.0...v0.31.0)

    ---
    updated-dependencies:
    - dependency-name: golang.org/x/crypto
      dependency-type: direct:production
    ...

    Signed-off-by: dependabot[bot] <support@github.com>
    Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

commit 5c80bedd4b
Author: xiabin <snyh@snyh.org>
Date:   Fri Dec 27 13:08:22 2024 +0800

    fix: reading low-level net.PacketConn when UDPSession is closed (#280)
2025-06-06 11:18:40 +08:00
Coia Prant da3a437911 fix: memory leak (#279) 2024-10-04 12:51:53 +08:00
xtaci 03783d6583 mark SetStreamMode deprecated 2024-09-03 10:38:05 +08:00
xtaci b15c0d13a0 fix https://github.com/xtaci/kcp-go/issues/273 2024-08-28 13:49:21 +08:00
xtaci e2e53727b4 added Control() function to give user privilege to set net.PacketConn 2024-08-23 22:31:35 +08:00
xtaci 77f5fe2174 add NewConn4 to let user delegate the conn to UDPSession to close 2024-08-19 16:03:21 +08:00
xtaci bf6f88f918 do not reserve in kcp core, reserve bytes in callback func 2024-07-31 14:57:52 +08:00
xtaci cc0944139b fix recycle 2024-07-28 01:02:18 +08:00
xtaci 40de342290 add a depicted ASCII graph for general flow of the session 2024-07-27 22:19:19 +08:00
xtaci 651dc4a6a4 automatic cork/uncork with channel, make critical section smaller 2024-07-27 21:29:14 +08:00