-
Couldn't load subscription status.
- Fork 2.5k
feat(pool): Improve success rate of new connections #3518
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
cyningsun
wants to merge
18
commits into
redis:master
Choose a base branch
from
cyningsun:feat/improve_success_rate_of_new_connections
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,361
−97
Open
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
311dff7
async create conn
cyningsun 76520fa
update default values and testcase
cyningsun 4164511
fix comments
cyningsun 584e631
fix data race
cyningsun 6d5764d
remove context.WithoutCancel, which is a function introduced in Go 1.21
cyningsun 8508f71
fix TestDialerRetryConfiguration/DefaultDialerRetries, because tryDia…
cyningsun 355a21a
change to share failed to delivery connection to other waiting
cyningsun 56eb8df
remove chinese comment
cyningsun a9a3c3f
fix: optimize WantConnQueue benchmarks to prevent memory exhaustion
cyningsun a5a4c37
format
cyningsun 528e88a
Merge branch 'master' into feat/improve_success_rate_of_new_connections
ndyakov 00e381d
Merge branch 'master' into feat/improve_success_rate_of_new_connections
ndyakov 2ca3eb3
Merge branch 'master' into feat/improve_success_rate_of_new_connections
ndyakov 37b59e4
Merge branch 'master' into feat/improve_success_rate_of_new_connections
ndyakov 90e44fc
Merge branch 'master' into feat/improve_success_rate_of_new_connections
ndyakov 778d331
fix turn leaks
cyningsun ad3f7b9
Merge branch 'master' into feat/improve_success_rate_of_new_connections
ndyakov dce66fc
Merge branch 'master' into feat/improve_success_rate_of_new_connections
ndyakov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -98,6 +98,7 @@ type Options struct { | |
|
|
||
| PoolFIFO bool | ||
| PoolSize int32 | ||
| MaxConcurrentDials int | ||
| DialTimeout time.Duration | ||
| PoolTimeout time.Duration | ||
| MinIdleConns int32 | ||
|
|
@@ -126,7 +127,9 @@ type ConnPool struct { | |
| dialErrorsNum uint32 // atomic | ||
| lastDialError atomic.Value | ||
|
|
||
| queue chan struct{} | ||
| queue chan struct{} | ||
| dialsInProgress chan struct{} | ||
| dialsQueue *wantConnQueue | ||
|
|
||
| connsMu sync.Mutex | ||
| conns map[uint64]*Conn | ||
|
|
@@ -152,9 +155,11 @@ func NewConnPool(opt *Options) *ConnPool { | |
| p := &ConnPool{ | ||
| cfg: opt, | ||
|
|
||
| queue: make(chan struct{}, opt.PoolSize), | ||
| conns: make(map[uint64]*Conn), | ||
| idleConns: make([]*Conn, 0, opt.PoolSize), | ||
| queue: make(chan struct{}, opt.PoolSize), | ||
| conns: make(map[uint64]*Conn), | ||
| dialsInProgress: make(chan struct{}, opt.MaxConcurrentDials), | ||
| dialsQueue: newWantConnQueue(), | ||
| idleConns: make([]*Conn, 0, opt.PoolSize), | ||
| } | ||
|
|
||
| // Only create MinIdleConns if explicitly requested (> 0) | ||
|
|
@@ -233,6 +238,7 @@ func (p *ConnPool) checkMinIdleConns() { | |
| return | ||
| } | ||
| } | ||
|
|
||
| } | ||
|
|
||
| func (p *ConnPool) addIdleConn() error { | ||
|
|
@@ -491,9 +497,8 @@ func (p *ConnPool) getConn(ctx context.Context) (*Conn, error) { | |
|
|
||
| atomic.AddUint32(&p.stats.Misses, 1) | ||
|
|
||
| newcn, err := p.newConn(ctx, true) | ||
| newcn, err := p.queuedNewConn(ctx) | ||
| if err != nil { | ||
| p.freeTurn() | ||
| return nil, err | ||
| } | ||
|
|
||
|
|
@@ -512,6 +517,99 @@ func (p *ConnPool) getConn(ctx context.Context) (*Conn, error) { | |
| return newcn, nil | ||
| } | ||
|
|
||
| func (p *ConnPool) queuedNewConn(ctx context.Context) (*Conn, error) { | ||
| select { | ||
| case p.dialsInProgress <- struct{}{}: | ||
| // Got permission, proceed to create connection | ||
| case <-ctx.Done(): | ||
| p.freeTurn() | ||
| return nil, ctx.Err() | ||
| } | ||
|
|
||
| dialCtx, cancel := context.WithTimeout(context.Background(), p.cfg.DialTimeout) | ||
|
|
||
| w := &wantConn{ | ||
| ctx: dialCtx, | ||
| cancelCtx: cancel, | ||
| result: make(chan wantConnResult, 1), | ||
| } | ||
| var err error | ||
| defer func() { | ||
| if err != nil { | ||
| if cn := w.cancel(); cn != nil { | ||
| p.putIdleConn(ctx, cn) | ||
| p.freeTurn() | ||
| } | ||
| } | ||
| }() | ||
|
|
||
| p.dialsQueue.enqueue(w) | ||
|
|
||
| go func(w *wantConn) { | ||
| var freeTurnCalled bool | ||
| defer func() { | ||
| if err := recover(); err != nil { | ||
| if !freeTurnCalled { | ||
| p.freeTurn() | ||
| } | ||
| internal.Logger.Printf(context.Background(), "queuedNewConn panic: %+v", err) | ||
| } | ||
| }() | ||
|
|
||
| defer w.cancelCtx() | ||
| defer func() { <-p.dialsInProgress }() // Release connection creation permission | ||
|
|
||
| dialCtx := w.getCtxForDial() | ||
| cn, cnErr := p.newConn(dialCtx, true) | ||
| delivered := w.tryDeliver(cn, cnErr) | ||
| if cnErr == nil && delivered { | ||
| return | ||
| } else if cnErr == nil && !delivered { | ||
| p.putIdleConn(dialCtx, cn) | ||
| p.freeTurn() | ||
| freeTurnCalled = true | ||
| } else { | ||
| p.freeTurn() | ||
| freeTurnCalled = true | ||
| } | ||
| }(w) | ||
|
|
||
| select { | ||
| case <-ctx.Done(): | ||
| err = ctx.Err() | ||
| return nil, err | ||
| case result := <-w.result: | ||
| err = result.err | ||
| return result.cn, err | ||
| } | ||
| } | ||
|
|
||
| func (p *ConnPool) putIdleConn(ctx context.Context, cn *Conn) { | ||
| for { | ||
| w, ok := p.dialsQueue.dequeue() | ||
| if !ok { | ||
| break | ||
| } | ||
| if w.tryDeliver(cn, nil) { | ||
| return | ||
| } | ||
| } | ||
|
|
||
| cn.SetUsable(true) | ||
|
|
||
| p.connsMu.Lock() | ||
| defer p.connsMu.Unlock() | ||
|
|
||
| if p.closed() { | ||
| _ = cn.Close() | ||
| return | ||
|
Comment on lines
+604
to
+605
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. here as well, |
||
| } | ||
|
|
||
| // poolSize is increased in newConn | ||
| p.idleConns = append(p.idleConns, cn) | ||
| p.idleConnsLen.Add(1) | ||
| } | ||
|
|
||
| func (p *ConnPool) waitTurn(ctx context.Context) error { | ||
| select { | ||
| case <-ctx.Done(): | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if
putIdleConnreturns here,p.freeTurn()won't be called