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

[PIP-165] Auto release idle connections #963

Merged
merged 10 commits into from
Mar 3, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
18 changes: 9 additions & 9 deletions pulsar/client_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ import (
)

const (
defaultConnectionTimeout = 10 * time.Second
defaultOperationTimeout = 30 * time.Second
defaultKeepAliveInterval = 30 * time.Second
defaultMemoryLimitBytes = 64 * 1024 * 1024
defaultConnectionMaxIdleTime = 60 * time.Second
minConnectionMaxIdleTime = 60 * time.Second
defaultConnectionTimeout = 10 * time.Second
defaultOperationTimeout = 30 * time.Second
defaultKeepAliveInterval = 30 * time.Second
defaultMemoryLimitBytes = 64 * 1024 * 1024
defaultConnMaxIdleTime = 180 * time.Second
minConnMaxIdleTime = 60 * time.Second
)

type client struct {
Expand All @@ -60,10 +60,10 @@ func newClient(options ClientOptions) (Client, error) {

connectionMaxIdleTime := options.ConnectionMaxIdleTime
if connectionMaxIdleTime == 0 {
connectionMaxIdleTime = defaultConnectionMaxIdleTime
} else if connectionMaxIdleTime > 0 && connectionMaxIdleTime < minConnectionMaxIdleTime {
connectionMaxIdleTime = defaultConnMaxIdleTime
} else if connectionMaxIdleTime > 0 && connectionMaxIdleTime < minConnMaxIdleTime {
return nil, newError(InvalidConfiguration, fmt.Sprintf("Connection max idle time should be at least %f "+
"seconds", minConnectionMaxIdleTime.Seconds()))
"seconds", minConnMaxIdleTime.Seconds()))
} else {
logger.Debugf("Disable auto release idle connections")
}
Expand Down
2 changes: 1 addition & 1 deletion pulsar/client_impl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1131,7 +1131,7 @@ func TestConfigureConnectionMaxIdleTime(t *testing.T) {
ConnectionMaxIdleTime: 1 * time.Second,
})

assert.Error(t, err, "Should be failed when the connectionMaxIdleTime is less than minConnectionMaxIdleTime")
assert.Error(t, err, "Should be failed when the connectionMaxIdleTime is less than minConnMaxIdleTime")

cli, err := NewClient(ClientOptions{
URL: serviceURL,
Expand Down
30 changes: 18 additions & 12 deletions pulsar/internal/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -936,23 +936,29 @@ func (c *connection) ResetLastActive() {
}

func (c *connection) isIdle() bool {
c.pendingLock.Lock()
if len(c.pendingReqs) != 0 {
return false
{
c.pendingLock.Lock()
defer c.pendingLock.Unlock()
if len(c.pendingReqs) != 0 {
return false
}
}
c.pendingLock.Unlock()

c.listenersLock.RLock()
if len(c.listeners) != 0 {
return false
{
c.listenersLock.RLock()
shibd marked this conversation as resolved.
Show resolved Hide resolved
defer c.listenersLock.RUnlock()
if len(c.listeners) != 0 {
return false
}
}
c.listenersLock.RUnlock()

c.consumerHandlersLock.Lock()
if len(c.consumerHandlers) != 0 {
return false
{
c.consumerHandlersLock.Lock()
defer c.consumerHandlersLock.Unlock()
if len(c.consumerHandlers) != 0 {
return false
}
}
c.consumerHandlersLock.Unlock()

if len(c.incomingRequestsCh) != 0 || len(c.writeRequestsCh) != 0 {
return false
Expand Down
4 changes: 2 additions & 2 deletions pulsar/internal/connection_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func NewConnectionPool(
metrics: metrics,
closeCh: make(chan struct{}),
}
go p.cleanConnections(connectionMaxIdleTime)
go p.checkAndCleanIdleConnections(connectionMaxIdleTime)
return p
}

Expand Down Expand Up @@ -142,7 +142,7 @@ func (p *connectionPool) getMapKey(addr *url.URL) string {
return fmt.Sprint(addr.Host, '-', idx)
}

func (p *connectionPool) cleanConnections(maxIdleTime time.Duration) {
func (p *connectionPool) checkAndCleanIdleConnections(maxIdleTime time.Duration) {
if maxIdleTime < 0 {
return
}
Expand Down
4 changes: 2 additions & 2 deletions pulsar/internal/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ package internal

import "time"

// These method should only used by tests
// These method should only be used by tests

func StartCleanConnectionsTask(p *ConnectionPool, connectionMaxIdleTime time.Duration) {
go (*p).(*connectionPool).cleanConnections(connectionMaxIdleTime)
go (*p).(*connectionPool).checkAndCleanIdleConnections(connectionMaxIdleTime)
}

func GetConnectionsCount(p *ConnectionPool) int {
Expand Down