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

fix(spanner): fix pool.numInUse exceeding MaxOpened #6344

Merged
merged 4 commits into from
Jul 19, 2022
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
16 changes: 8 additions & 8 deletions spanner/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,12 +318,14 @@ func (s *session) getNextCheck() time.Time {
// recycle turns the session back to its home session pool.
func (s *session) recycle() {
s.setTransactionID(nil)
if !s.pool.recycle(s) {
s.pool.mu.Lock()
defer s.pool.mu.Unlock()
if !s.pool.recycleLocked(s) {
// s is rejected by its home session pool because it expired and the
// session pool currently has enough open sessions.
s.destroy(false)
}
s.pool.decNumInUse(context.Background())
s.pool.decNumInUseLocked(context.Background())
}

// destroy removes the session from its home session pool, healthcheck queue
Expand Down Expand Up @@ -1079,6 +1081,10 @@ func (p *sessionPool) takeWriteSession(ctx context.Context) (*sessionHandle, err
func (p *sessionPool) recycle(s *session) bool {
p.mu.Lock()
defer p.mu.Unlock()
return p.recycleLocked(s)
}

func (p *sessionPool) recycleLocked(s *session) bool {
if !s.isValid() || !p.valid {
// Reject the session if session is invalid or pool itself is invalid.
return false
Expand Down Expand Up @@ -1155,12 +1161,6 @@ func (p *sessionPool) incNumInUseLocked(ctx context.Context) {
}
}

func (p *sessionPool) decNumInUse(ctx context.Context) {
p.mu.Lock()
p.decNumInUseLocked(ctx)
p.mu.Unlock()
}

func (p *sessionPool) decNumInUseLocked(ctx context.Context) {
p.numInUse--
p.recordStat(ctx, SessionsCount, int64(p.numInUse), tagNumInUseSessions)
Expand Down