Skip to content

Commit

Permalink
fix(spanner): fix pool.numInUse exceeding MaxOpened (#6344)
Browse files Browse the repository at this point in the history
Co-authored-by: rahul2393 <rahulyadavsep92@gmail.com>
  • Loading branch information
hengfengli and rahul2393 committed Jul 19, 2022
1 parent 0c82089 commit 882b325
Showing 1 changed file with 8 additions and 8 deletions.
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

0 comments on commit 882b325

Please sign in to comment.