Skip to content
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

[INLONG-11703][SDK] Improve the cleanExpiredConnTicker select logic in the connpool of the Golang SDK #11704

Merged
merged 1 commit into from
Jan 22, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ func NewConnPool(initEndpoints []string, connsPerEndpoint, size int,
}

// starts a backbround task, do rebalancing and recovering periodically
go pool.recoverAndRebalance()
go pool.innerWork()

return pool, nil
}
Expand Down Expand Up @@ -527,8 +527,8 @@ func (p *connPool) markUnavailable(ep string) {
p.retryCounts.Store(ep, 0)
}

// recoverAndRebalance recovers the down endpoint and rebalaces the conns periodically
func (p *connPool) recoverAndRebalance() {
// innerWork recovers the down endpoint, rebalaces the conns and cleans the expired conns periodically
func (p *connPool) innerWork() {
// server failure is a low-probability event, so there's basically no endpoint need to recover, a higher frequency is also acceptable
recoverTicker := time.NewTicker(10 * time.Second)
defer recoverTicker.Stop()
Expand All @@ -540,15 +540,12 @@ func (p *connPool) recoverAndRebalance() {
defer reBalanceTicker.Stop()

// clean expired conn every minute
var cleanExpiredConnTicker *time.Ticker
var cleanExpiredConnChan <-chan time.Time
if p.maxConnLifetime > 0 {
cleanExpiredConnTicker = time.NewTicker(1 * time.Minute)
cleanExpiredConnTicker := time.NewTicker(1 * time.Minute)
defer cleanExpiredConnTicker.Stop()
cleanExpiredConnChan = cleanExpiredConnTicker.C
}
defer func() {
if cleanExpiredConnTicker != nil {
cleanExpiredConnTicker.Stop()
}
}()

for {
select {
Expand All @@ -564,17 +561,8 @@ func (p *connPool) recoverAndRebalance() {
p.rebalance()
case <-p.closeCh:
return
default:
if cleanExpiredConnTicker != nil {
select {
case <-cleanExpiredConnTicker.C:
p.cleanExpiredConns()
default:
time.Sleep(time.Second)
}
} else {
time.Sleep(time.Second)
}
case <-cleanExpiredConnChan:
p.cleanExpiredConns()
}
}
}
Expand Down
Loading