Skip to content

Commit

Permalink
add tiny sleep
Browse files Browse the repository at this point in the history
  • Loading branch information
mhamza15 committed Jan 29, 2025
1 parent 3a8f2b5 commit a4b4039
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 12 deletions.
2 changes: 0 additions & 2 deletions go/pools/smartconnpool/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -396,9 +396,7 @@ func (pool *ConnPool[C]) put(conn *Pooled[C]) {
}
}

log.Error("========================== I'M TRYING TO RETURN A CONNECTION HERE")
if !pool.wait.tryReturnConn(conn) {
log.Error("============================= tryReturnConn returned false, we're in that branch")
connSetting := conn.Conn.Setting()
if connSetting == nil {
pool.clean.Push(conn)
Expand Down
17 changes: 7 additions & 10 deletions go/pools/smartconnpool/waitlist.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ package smartconnpool

import (
"context"
"math/rand"
"sync"
"time"

"vitess.io/vitess/go/list"
"vitess.io/vitess/go/vt/log"
Expand Down Expand Up @@ -59,14 +59,10 @@ func (wl *waitlist[C]) waitForConn(ctx context.Context, setting *Setting) (*Pool
wl.mu.Lock()
// add ourselves as a waiter at the end of the waitlist
wl.list.PushBackValue(elem)
log.Errorf("========================== THIS IS WHAT THE WAITLIST LOOKS LIKE BEFORE I START WAITING:\n%+v", wl)
wl.mu.Unlock()

// block on our waiter's semaphore until somebody can hand over a connection to us
i := rand.Int()
log.Errorf("======================= %d: I'M WAITING", i)
elem.Value.sema.wait()
log.Error("======================== %d: I'M DONE WAITING", i)

// we're awake -- the conn in our waiter contains the connection that was handed
// over to us, or nothing if we've been waken up forcefully. save the conn before
Expand Down Expand Up @@ -114,8 +110,12 @@ func (wl *waitlist[C]) expire(force bool) {
func (wl *waitlist[D]) tryReturnConn(conn *Pooled[D]) bool {
// fast path: if there's nobody waiting there's nothing to do
if wl.list.Len() == 0 {
log.Error("======================= WE HIT THE FAST PATH, WE'RE RETURNING")
return false
// HACK: we're gonna sleep for a bit and try again, to see if there are still no waiters
time.Sleep(250 * time.Millisecond)
if wl.list.Len() == 0 {
log.Error("======================= WE HIT THE FAST PATH, WE'RE RETURNING")
return false
}
}
// split the slow path into a separate function to enable inlining
return wl.tryReturnConnSlow(conn)
Expand Down Expand Up @@ -153,12 +153,9 @@ func (wl *waitlist[D]) tryReturnConnSlow(conn *Pooled[D]) bool {
// maybe there isn't anybody to hand over the connection to, because we've
// raced with another client returning another connection
if target == nil {
log.Error("====================== target was nil, we couldn't find someone to give the connection to")
return false
}

log.Errorf("===================== WE'VE FOUND A TARGET: %+v", target)

// if we have a target to return the connection to, simply write the connection
// into the waiter and signal their semaphore. they'll wake up to pick up the
// connection.
Expand Down

0 comments on commit a4b4039

Please sign in to comment.