92 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 b27887e8ce fix(fec): prevent permanent blocking in auto-tune when parameters unchanged
Fix shouldTune flag not being reset when auto-detected FEC parameters
match current parameters. This could cause the decoder to permanently
discard all incoming packets if a corrupted packet triggered shouldTune
but the actual FEC configuration hadn't changed.

Move `dec.shouldTune = false` outside the parameter-change check to
ensure the flag is always reset when valid parameters are detected.
2026-01-26 11:02:30 +08:00
xtaci 071dc4a69d 1. Fix incorrect RepeatSegs counter in parse_data() function
- Previously, the repeat flag was never set to true when duplicate packets
     were detected, causing incorrect statistics reporting
   - Added missing else branch to properly mark duplicate segments
   - Impact: Correct tracking of repeated packet statistics

2. Fix memory leak in FEC decoder auto-tune functionality
   - When FEC parameters were adjusted dynamically, the old shardSet map
     was discarded without recycling its buffer pool entries
   - Added proper cleanup loop to return old shard packets to buffer pool
     before creating new shardSet
   - Impact: Prevents memory growth during long-lived connections with
     dynamic FEC parameter adjustments

Files modified:
- kcp.go: Added else branch in parse_data() to set repeat=true for duplicates
- fec.go: Added buffer cleanup loop before shardSet recreation in decode()

All existing tests pass, including FEC encoding/decoding and lossy connection tests.
2026-01-24 21:55:49 +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
xtaci bca9b95082 refine struct to support oob 2025-12-24 20:40:47 +08:00
xtaci 6c9035eb62 1. Fix memory leaks in fecDecoder:
- Properly recycle `fecPacket`s popped from `shardHeap` back to `defaultBufferPool` in `decode()`.
   - Recycle newly allocated buffers if `ReconstructData` fails (on success, they are returned to the caller to be recycled).
   - Recycle all packets within an expired shard before deleting it in `discardShards()`.

2. Correct PAWS (Protect Against Wrapped Sequence numbers) logic:
   - Update `newestShardId` comparison and expiration checks to use scaled sequence numbers () instead of raw shard IDs.
   - This ensures  correctly handles sequence wrapping boundaries, which was previously inaccurate when comparing raw shard IDs directly.
2025-12-19 13:00:09 +08:00
xtaci 66c137e98f strengthen fec input check
strict check
2025-12-14 20:12:23 +08:00
xtaci 9427d1ddeb optimize comments for fec.go 2025-12-12 22:30:27 +08:00
xtaci b8671f2675 add a line of comment 2025-12-10 10:15:27 +08:00
xtaci 19e83f34ce flushShards->discardShards 2025-12-09 19:47:37 +08:00
xtaci f4b796e048 rename minShardId to newestShardId and added some comments. Kept the reordered packet. 2025-12-09 19:44:13 +08:00
Coia Prant 6b9fc33ceb chores: drop unused code (#318)
* chores: drop unused fec fields

* chores: drop impossible check
2025-12-09 17:31:22 +08:00
xtaci 91e7483fc9 use any to substitute interface{} 2025-11-21 16:24:14 +08:00
xtaci b243d51f0c seperate xmitBuf into a bufferPool struct 2025-11-19 20:39:22 +08:00
xtaci 8fb358741d remove unused ts field 2025-06-10 13:59:46 +08:00
xtaci d149e33c9d renaming vars 2025-06-09 20:10:28 +08:00
xtaci 0e2cae1f18 comments 2025-06-09 20:00:39 +08:00
xtaci 0892360d37 fix 2025-06-09 19:53:40 +08:00
xtaci b8baad2253 fix 2025-06-09 19:50:48 +08:00
xtaci 4338d9c2a6 use more strict discarding of shards 2025-06-09 19:44:37 +08:00
xtaci 27bc69b672 Squashed commit of the following:
commit a6eb09bcae
Author: xtaci <imap@live.com>
Date:   Mon Jun 9 16:51:56 2025 +0800

    added a test to FEC

commit 01641f964b
Author: xtaci <imap@live.com>
Date:   Mon Jun 9 15:43:12 2025 +0800

    rename snmp var

commit 32d49d392a
Author: xtaci <imap@live.com>
Date:   Mon Jun 9 15:41:52 2025 +0800

    update snmp

commit bd5c742170
Author: xtaci <imap@live.com>
Date:   Mon Jun 9 15:37:15 2025 +0800

    optimize

commit fdbe42618f
Author: xtaci <imap@live.com>
Date:   Mon Jun 9 15:17:35 2025 +0800

    fix getShardId

commit 4473f93581
Author: xtaci <imap@live.com>
Date:   Mon Jun 9 15:00:56 2025 +0800

    add more info to snmp

commit ff7cf0b51c
Author: xtaci <imap@live.com>
Date:   Mon Jun 9 14:53:32 2025 +0800

    optimize error handling

commit 4aa82fae89
Author: xtaci <imap@live.com>
Date:   Mon Jun 9 14:42:55 2025 +0800

    use heap to manager shard recovery in fec
2025-06-09 17:25:56 +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
xtaci 2ffcdafd95 added an error handling to FEC encode(though it may not be called) 2024-09-11 17:02:12 +08:00
xtaci 50402daf86 comments on the code 2024-09-11 16:42:50 +08:00
xtaci 4b694a7dd5 comments on the shards searching 2024-09-11 16:29:30 +08:00
xtaci 4c4674b03a made comments on fec.go 2024-09-11 16:17:59 +08:00
xtaci 1fa6fb277f add wrap on data too 2024-08-28 15:47:16 +08:00
xtaci 05aede1bf1 fix a bug in FEC non-time continuous parityshard 2024-08-28 13:47:30 +08:00
xtaci b63d30d10e update with an ASCII depicted explaination 2024-07-27 22:06:41 +08:00
xtaci 3040333ea6 adjust comments 2024-07-27 21:54:02 +08:00
xtaci 3653cc9fea add license to source codes 2024-07-25 21:35:37 +08:00
xtaci 16b0610a85 remove unused param 2024-01-05 22:32:14 +08:00
xtaci cd06d59cba Squashed commit of the following:
commit 0e5741b954
Author: fuli <fuli@rockx.com>
Date:   Thu Jan 4 16:21:42 2024 +0800

    use latest packet compare

commit 97999b9dc2
Author: fuli <fuli@rockx.com>
Date:   Thu Jan 4 16:07:08 2024 +0800

    face race in test

commit 446bead851
Author: fuli <fuli@rockx.com>
Date:   Thu Jan 4 16:04:42 2024 +0800

    upd

commit 570e9b006e
Author: fuli <fuli@rockx.com>
Date:   Thu Jan 4 16:03:17 2024 +0800

    revert some file

commit c665db20a5
Author: fuli <fuli@rockx.com>
Date:   Thu Jan 4 15:59:16 2024 +0800

    Revert "Revert "1. simplify readloop_xxx files & tx_xxx files by merging""

    This reverts commit 717b79a156.

commit 717b79a156
Author: fuli <fuli@rockx.com>
Date:   Thu Jan 4 15:58:30 2024 +0800

    Revert "1. simplify readloop_xxx files & tx_xxx files by merging"

    This reverts commit 681bda64d1.

commit 681bda64d1
Author: fuli <fuli@rockx.com>
Date:   Thu Jan 4 15:54:29 2024 +0800

    1. simplify readloop_xxx files & tx_xxx files by merging
    2. fix the race in owned packet test

commit 7974357dd0
Author: xtaci <daniel820313@gmail.com>
Date:   Thu Jan 4 00:04:06 2024 +0800

    use unix.Milli

commit 375407b80c
Author: xtaci <daniel820313@gmail.com>
Date:   Wed Jan 3 23:40:01 2024 +0800

    add passive fec
2024-01-04 22:43:12 +08:00
xtaci ceebe176cf fix autotune fec mis-align when parameters are different 2023-10-12 23:10:50 +08:00
xtaci 691fc2febe use clear in go1.21 to zero instead of copying zeros 2023-08-11 11:33:20 +08:00
xtaci dd1ac88f92 add extra check in auto tune 2020-10-09 23:23:58 +08:00
xtaci 582575ef06 Squashed commit of the following:
commit a8ae293a9ddee042c35e2501f93fa5dfa2425302
Merge: bdaf81e 1a84985
Author: xtaci <daniel820313@gmail.com>
Date:   Fri Oct 9 22:48:13 2020 +0800

    Merge branch 'master' into autotune

commit bdaf81e88e
Author: xtaci <daniel820313@gmail.com>
Date:   Fri Oct 9 22:38:31 2020 +0800

    add loc

commit ce66a98f95
Author: xtaci <daniel820313@gmail.com>
Date:   Fri Oct 9 19:53:42 2020 +0800

    lint

commit ad7383d8a3
Author: xtaci <daniel820313@gmail.com>
Date:   Fri Oct 9 19:05:35 2020 +0800

    fix typo

commit dc0fc2364c
Author: xtaci <daniel820313@gmail.com>
Date:   Fri Oct 9 16:47:14 2020 +0800

    adjust tests

commit ec45b72c39
Author: xtaci <daniel820313@gmail.com>
Date:   Fri Oct 9 16:45:17 2020 +0800

    remove obsolete decoder in listener

commit 827aa70e29
Author: xtaci <daniel820313@gmail.com>
Date:   Fri Oct 9 16:42:42 2020 +0800

    use hierarchical defensive receiving strategy

commit f5ca6b0e3a
Author: xtaci <daniel820313@gmail.com>
Date:   Fri Oct 9 16:34:30 2020 +0800

    optimize condition

commit ff34f7683f
Author: xtaci <daniel820313@gmail.com>
Date:   Fri Oct 9 15:18:00 2020 +0800

    fix test

commit a08b274651
Author: xtaci <daniel820313@gmail.com>
Date:   Fri Oct 9 15:13:20 2020 +0800

    solve compability issue with non FEC sender

commit ab7596783b
Author: xtaci <daniel820313@gmail.com>
Date:   Fri Oct 9 15:02:34 2020 +0800

    implements autotune in fec decoder

commit 260f61b3eb
Author: xtaci <daniel820313@gmail.com>
Date:   Fri Oct 9 14:34:48 2020 +0800

    hack 16-bit to identify FEC from packet

commit 105bfec567
Author: xtaci <daniel820313@gmail.com>
Date:   Fri Oct 2 21:38:47 2020 +0800

    add autotune
2020-10-09 22:52:11 +08:00
xtaci 2ed35ac44d fix race and parityshard leaks 2019-11-07 13:30:11 +08:00
xtaci ddb5eedad0 return FEC segments to xmitBuf when session closes 2019-11-05 14:41:57 +08:00
xtaci 62a262c330 fix fec expire counting 2019-10-30 14:55:04 +00:00
xtaci 5d104dba02 change to const fecExpire 2019-10-30 12:35:20 +00:00
xtaci 31e8785c63 add a timeout policy for fec buffer 2019-10-27 13:18:53 +00:00
xtaci c749e12021 enlarge paws border 2019-05-23 22:41:45 +08:00
xtaci 39bfc772c5 len->cap 2019-05-06 13:25:40 +08:00
xtaci 23759369cb same logic applied to fec queue 2019-04-27 09:51:26 +08:00
xtaci 8c3db6588a optimization in slice front element deletion 2019-04-26 22:22:16 +08:00
xtaci 31ca7b731d rename typeFEC->typeParity 2019-04-23 11:59:03 +08:00
xtaci 348f7245b9 rename markFEC to markParity and add a line of comment 2019-04-23 11:42:06 +08:00
xtaci 01adef1dd5 omit the data copying before headerOffset 2019-04-20 20:36:11 +08:00
xtaci b44d869cfd prevent reedsolomon decoder from allocating memory 2019-04-01 21:25:46 +08:00