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

add connection pool put back #571

Closed
wants to merge 1 commit into from
Closed
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
27 changes: 26 additions & 1 deletion redis/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,9 @@ type Pool struct {
// the timeout to a value less than the server's timeout.
IdleTimeout time.Duration

// idle connection in the pool method, True: pushBack, False: pushFront, default False
Lifo bool

// If Wait is true and the pool is at the MaxActive limit, then Get() waits
// for a connection to be returned to the pool before returning.
Wait bool
Expand Down Expand Up @@ -403,7 +406,13 @@ func (p *Pool) put(pc *poolConn, forceClose bool) error {
p.mu.Lock()
if !p.closed && !forceClose {
pc.t = nowFunc()
p.idle.pushFront(pc)

if !p.Lifo {
p.idle.pushFront(pc)
} else {
p.idle.pushBack(pc)
}

if p.idle.count > p.MaxIdle {
pc = p.idle.back
p.idle.popBack()
Expand Down Expand Up @@ -623,6 +632,22 @@ func (l *idleList) popFront() {
pc.next, pc.prev = nil, nil
}

// idle connect push list back
func (l *idleList) pushBack(pc *poolConn) {
if l.count == 0 {
l.front = pc
l.back = pc
pc.prev = nil
pc.next = nil
} else {
pc.prev = l.back
l.back.next = pc
l.back = pc
pc.next = nil
}
l.count++
}

func (l *idleList) popBack() {
pc := l.back
l.count--
Expand Down