1
0
mirror of https://github.com/redis/go-redis.git synced 2025-12-03 18:31:14 +03:00
Commit Graph

220 Commits

Author SHA1 Message Date
Nedyalko Dyakov
20aa85037d address linter and tests 2025-11-11 01:04:08 +02:00
Nedyalko Dyakov
7a400c3a70 use simple channel based semaphores 2025-11-11 01:04:08 +02:00
Nedyalko Dyakov
7bf921716d Merge remote-tracking branch 'origin/ndyakov/state-machine-conn' into playground/autopipeline 2025-11-10 13:49:51 +02:00
Nedyalko Dyakov
edf6bd794f fix flaky test 2025-11-07 16:25:06 +02:00
Nedyalko Dyakov
cca0382772 preallocate state slices 2025-11-06 15:00:34 +02:00
Nedyalko Dyakov
9466c1c8a9 preallocate errors and states 2025-11-06 14:57:05 +02:00
Nedyalko Dyakov
0c5e3c93e0 Merge branch 'ndyakov/state-machine-conn' into playground/autopipeline 2025-11-05 22:34:55 +02:00
Nedyalko Dyakov
41024f72c1 CAS instead of reading the state 2025-11-05 22:05:12 +02:00
Nedyalko Dyakov
2b8023cb17 use pool size for semsize 2025-11-05 22:03:49 +02:00
Nedyalko Dyakov
4673c621ff use read instad of control 2025-11-05 21:59:57 +02:00
Nedyalko Dyakov
18b46a1ca7 fix flaky test 2025-11-05 21:35:20 +02:00
Nedyalko Dyakov
c3dbc8c2ab address linter comment 2025-11-04 18:27:28 +02:00
Nedyalko Dyakov
a39dd4c9d2 silence logs and faster hooks manager 2025-11-04 18:18:03 +02:00
Nedyalko Dyakov
c637c0824e wip 2025-11-03 00:14:24 +02:00
Nedyalko Dyakov
f672885808 wip 2025-10-31 13:04:46 +02:00
Nedyalko Dyakov
9eaeea1347 Merge branch 'ndyakov/state-machine-conn' into playground/autopipeline 2025-10-30 23:53:53 +02:00
Nedyalko Dyakov
5fa97c826c add missed method in interface 2025-10-30 21:10:22 +02:00
Nedyalko Dyakov
ef3e06fd71 Merge remote-tracking branch 'origin/master' into ndyakov/state-machine-conn 2025-10-30 19:44:45 +02:00
cyningsun
ae5434ce66 feat(pool): Improve success rate of new connections (#3518)
* async create conn

* update default values and testcase

* fix comments

* fix data race

* remove context.WithoutCancel, which is a function introduced in Go 1.21

* fix TestDialerRetryConfiguration/DefaultDialerRetries, because tryDial are likely done in async flow

* change to share failed to delivery connection to other waiting

* remove chinese comment

* fix: optimize WantConnQueue benchmarks to prevent memory exhaustion

- Fix BenchmarkWantConnQueue_Dequeue timeout issue by limiting pre-population
- Use object pooling in BenchmarkWantConnQueue_Enqueue to reduce allocations
- Optimize BenchmarkWantConnQueue_EnqueueDequeue with reusable wantConn pool
- Prevent GitHub Actions benchmark failures due to excessive memory usage

Before: BenchmarkWantConnQueue_Dequeue ran for 11+ minutes and was killed
After: All benchmarks complete in ~8 seconds with consistent performance

* format

* fix turn leaks

---------

Co-authored-by: Nedyalko Dyakov <1547186+ndyakov@users.noreply.github.com>
Co-authored-by: Hristo Temelski <hristo.temelski@redis.com>
2025-10-30 19:21:12 +02:00
Nedyalko Dyakov
d207749af5 flaky test 2025-10-30 19:19:20 +02:00
Nedyalko Dyakov
59da35ba2d improve remove conn 2025-10-29 16:23:21 +02:00
Nedyalko Dyakov
2965e3d35c fix benchmark test 2025-10-29 16:21:21 +02:00
Nedyalko Dyakov
43eeae70ab fix unsafe test 2025-10-29 16:19:04 +02:00
Nedyalko Dyakov
62eecaa75e fix assertion 2025-10-29 16:11:27 +02:00
Nedyalko Dyakov
dccf01f396 use correct timer for last health check 2025-10-29 16:00:14 +02:00
Nedyalko Dyakov
600dfe2581 100ms -> 50ms 2025-10-29 15:31:31 +02:00
Nedyalko Dyakov
b6d7cdbd84 chore(ci): Add redis 8.4-RC1-pre & examples (#3572)
* add disable maintnotifications example

* add 8.4-RC1-pre

* println -> printf for linter

* address jit comment

Fix broken initialization of idle connections

optimize push notif

wip

wip

wip

wip
2025-10-29 13:49:32 +02:00
Nedyalko Dyakov
c2d525f688 fix precision of time cache and usedAt 2025-10-29 13:49:31 +02:00
Nedyalko Dyakov
4950a2ff45 initConn sets IDLE state
- Handle unexpected conn state changes
2025-10-29 13:49:30 +02:00
Nedyalko Dyakov
d476a3813e fix(pool): pool performance (#3565)
* perf(pool): replace hookManager RWMutex with atomic.Pointer and add predefined state slices

- Replace hookManager RWMutex with atomic.Pointer for lock-free reads in hot paths
- Add predefined state slices to avoid allocations (validFromInUse, validFromCreatedOrIdle, etc.)
- Add Clone() method to PoolHookManager for atomic updates
- Update AddPoolHook/RemovePoolHook to use copy-on-write pattern
- Update all hookManager access points to use atomic Load()

Performance improvements:
- Eliminates RWMutex contention in Get/Put/Remove hot paths
- Reduces allocations by reusing predefined state slices
- Lock-free reads allow better CPU cache utilization

* perf(pool): eliminate mutex overhead in state machine hot path

The state machine was calling notifyWaiters() on EVERY Get/Put operation,
which acquired a mutex even when no waiters were present (the common case).

Fix: Use atomic waiterCount to check for waiters BEFORE acquiring mutex.
This eliminates mutex contention in the hot path (Get/Put operations).

Implementation:
- Added atomic.Int32 waiterCount field to ConnStateMachine
- Increment when adding waiter, decrement when removing
- Check waiterCount atomically before acquiring mutex in notifyWaiters()

Performance impact:
- Before: mutex lock/unlock on every Get/Put (even with no waiters)
- After: lock-free atomic check, only acquire mutex if waiters exist
- Expected improvement: ~30-50% for Get/Put operations

* perf(pool): use predefined state slices to eliminate allocations in hot path

The pool was creating new slice literals on EVERY Get/Put operation:
- popIdle(): []ConnState{StateCreated, StateIdle}
- putConn(): []ConnState{StateInUse}
- CompareAndSwapUsed(): []ConnState{StateIdle} and []ConnState{StateInUse}
- MarkUnusableForHandoff(): []ConnState{StateInUse, StateIdle, StateCreated}

These allocations were happening millions of times per second in the hot path.

Fix: Use predefined global slices defined in conn_state.go:
- validFromInUse
- validFromCreatedOrIdle
- validFromCreatedInUseOrIdle

Performance impact:
- Before: 4 slice allocations per Get/Put cycle
- After: 0 allocations (use predefined slices)
- Expected improvement: ~30-40% reduction in allocations and GC pressure

* perf(pool): optimize TryTransition to reduce atomic operations

Further optimize the hot path by:
1. Remove redundant GetState() call in the loop
2. Only check waiterCount after successful CAS (not before loop)
3. Inline the waiterCount check to avoid notifyWaiters() call overhead

This reduces atomic operations from 4-5 per Get/Put to 2-3:
- Before: GetState() + CAS + waiterCount.Load() + notifyWaiters mutex check
- After: CAS + waiterCount.Load() (only if CAS succeeds)

Performance impact:
- Eliminates 1-2 atomic operations per Get/Put
- Expected improvement: ~10-15% for Get/Put operations

* perf(pool): add fast path for Get/Put to match master performance

Introduced TryTransitionFast() for the hot path (Get/Put operations):
- Single CAS operation (same as master's atomic bool)
- No waiter notification overhead
- No loop through valid states
- No error allocation

Hot path flow:
1. popIdle(): Try IDLE → IN_USE (fast), fallback to CREATED → IN_USE
2. putConn(): Try IN_USE → IDLE (fast)

This matches master's performance while preserving state machine for:
- Background operations (handoff/reauth use UNUSABLE state)
- State validation (TryTransition still available)
- Waiter notification (AwaitAndTransition for blocking)

Performance comparison per Get/Put cycle:
- Master: 2 atomic CAS operations
- State machine (before): 5 atomic operations (2.5x slower)
- State machine (after): 2 atomic CAS operations (same as master!)

Expected improvement: Restore to baseline ~11,373 ops/sec

* combine cas

* fix linter

* try faster approach

* fast semaphore

* better inlining for hot path

* fix linter issues

* use new semaphore in auth as well

* linter should be happy now

* add comments

* Update internal/pool/conn_state.go

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* address comment

* slight reordering

* try to cache time if for non-critical calculation

* fix wrong benchmark

* add concurrent test

* fix benchmark report

* add additional expect to check output

* comment and variable rename

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
2025-10-29 13:49:26 +02:00
Nedyalko Dyakov
54281d687c optimize push notif 2025-10-28 23:32:27 +02:00
Nedyalko Dyakov
0752aecdfb Fix broken initialization of idle connections 2025-10-28 19:45:50 +02:00
Nedyalko Dyakov
d5db5340cb fix precision of time cache and usedAt 2025-10-28 12:34:09 +02:00
Nedyalko Dyakov
9448059c01 initConn sets IDLE state
- Handle unexpected conn state changes
2025-10-28 00:49:39 +02:00
Nedyalko Dyakov
7198f47baa autopipeline playground 2025-10-27 16:08:40 +02:00
Nedyalko Dyakov
080a33c3a8 fix(pool): pool performance (#3565)
* perf(pool): replace hookManager RWMutex with atomic.Pointer and add predefined state slices

- Replace hookManager RWMutex with atomic.Pointer for lock-free reads in hot paths
- Add predefined state slices to avoid allocations (validFromInUse, validFromCreatedOrIdle, etc.)
- Add Clone() method to PoolHookManager for atomic updates
- Update AddPoolHook/RemovePoolHook to use copy-on-write pattern
- Update all hookManager access points to use atomic Load()

Performance improvements:
- Eliminates RWMutex contention in Get/Put/Remove hot paths
- Reduces allocations by reusing predefined state slices
- Lock-free reads allow better CPU cache utilization

* perf(pool): eliminate mutex overhead in state machine hot path

The state machine was calling notifyWaiters() on EVERY Get/Put operation,
which acquired a mutex even when no waiters were present (the common case).

Fix: Use atomic waiterCount to check for waiters BEFORE acquiring mutex.
This eliminates mutex contention in the hot path (Get/Put operations).

Implementation:
- Added atomic.Int32 waiterCount field to ConnStateMachine
- Increment when adding waiter, decrement when removing
- Check waiterCount atomically before acquiring mutex in notifyWaiters()

Performance impact:
- Before: mutex lock/unlock on every Get/Put (even with no waiters)
- After: lock-free atomic check, only acquire mutex if waiters exist
- Expected improvement: ~30-50% for Get/Put operations

* perf(pool): use predefined state slices to eliminate allocations in hot path

The pool was creating new slice literals on EVERY Get/Put operation:
- popIdle(): []ConnState{StateCreated, StateIdle}
- putConn(): []ConnState{StateInUse}
- CompareAndSwapUsed(): []ConnState{StateIdle} and []ConnState{StateInUse}
- MarkUnusableForHandoff(): []ConnState{StateInUse, StateIdle, StateCreated}

These allocations were happening millions of times per second in the hot path.

Fix: Use predefined global slices defined in conn_state.go:
- validFromInUse
- validFromCreatedOrIdle
- validFromCreatedInUseOrIdle

Performance impact:
- Before: 4 slice allocations per Get/Put cycle
- After: 0 allocations (use predefined slices)
- Expected improvement: ~30-40% reduction in allocations and GC pressure

* perf(pool): optimize TryTransition to reduce atomic operations

Further optimize the hot path by:
1. Remove redundant GetState() call in the loop
2. Only check waiterCount after successful CAS (not before loop)
3. Inline the waiterCount check to avoid notifyWaiters() call overhead

This reduces atomic operations from 4-5 per Get/Put to 2-3:
- Before: GetState() + CAS + waiterCount.Load() + notifyWaiters mutex check
- After: CAS + waiterCount.Load() (only if CAS succeeds)

Performance impact:
- Eliminates 1-2 atomic operations per Get/Put
- Expected improvement: ~10-15% for Get/Put operations

* perf(pool): add fast path for Get/Put to match master performance

Introduced TryTransitionFast() for the hot path (Get/Put operations):
- Single CAS operation (same as master's atomic bool)
- No waiter notification overhead
- No loop through valid states
- No error allocation

Hot path flow:
1. popIdle(): Try IDLE → IN_USE (fast), fallback to CREATED → IN_USE
2. putConn(): Try IN_USE → IDLE (fast)

This matches master's performance while preserving state machine for:
- Background operations (handoff/reauth use UNUSABLE state)
- State validation (TryTransition still available)
- Waiter notification (AwaitAndTransition for blocking)

Performance comparison per Get/Put cycle:
- Master: 2 atomic CAS operations
- State machine (before): 5 atomic operations (2.5x slower)
- State machine (after): 2 atomic CAS operations (same as master!)

Expected improvement: Restore to baseline ~11,373 ops/sec

* combine cas

* fix linter

* try faster approach

* fast semaphore

* better inlining for hot path

* fix linter issues

* use new semaphore in auth as well

* linter should be happy now

* add comments

* Update internal/pool/conn_state.go

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* address comment

* slight reordering

* try to cache time if for non-critical calculation

* fix wrong benchmark

* add concurrent test

* fix benchmark report

* add additional expect to check output

* comment and variable rename

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
2025-10-27 15:06:30 +02:00
Nedyalko Dyakov
55c502dde4 try to cache time if for non-critical calculation 2025-10-27 07:30:09 +02:00
Nedyalko Dyakov
bec09a52b6 slight reordering 2025-10-26 19:22:38 +02:00
Nedyalko Dyakov
d64c4eb556 Update internal/pool/conn_state.go
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
2025-10-26 00:51:24 +03:00
Nedyalko Dyakov
f52ab34e31 add comments 2025-10-26 00:50:23 +03:00
Nedyalko Dyakov
b2225f165b Merge branch 'ndyakov/state-machine-conn' into ndyakov/pool-performance 2025-10-26 00:30:44 +03:00
Nedyalko Dyakov
0878735280 linter should be happy now 2025-10-26 00:16:43 +03:00
Nedyalko Dyakov
5ff5463c4a use new semaphore in auth as well 2025-10-25 23:06:57 +03:00
Nedyalko Dyakov
f8feb0ed4e fix linter issues 2025-10-25 22:52:37 +03:00
Nedyalko Dyakov
9ec5daee11 better inlining for hot path 2025-10-25 22:19:03 +03:00
Nedyalko Dyakov
c02fe3e0be fast semaphore 2025-10-25 22:03:46 +03:00
Nedyalko Dyakov
13a4b3f366 fix error from copilot 2025-10-25 21:53:13 +03:00
Nedyalko Dyakov
33696fb002 Update internal/pool/conn.go
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
2025-10-25 21:49:41 +03:00
Nedyalko Dyakov
bc4230766a Update internal/pool/conn_state.go
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
2025-10-25 21:49:12 +03:00
Nedyalko Dyakov
0964dccbf1 Update internal/pool/pool.go
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
2025-10-25 21:49:01 +03:00