Skip to content

Commit

Permalink
use rw mutex to serialize creation
Browse files Browse the repository at this point in the history
  • Loading branch information
demmer committed Mar 22, 2024
1 parent 0359ecd commit 1b62a71
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions go/vt/vtgateproxy/vtgateproxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,27 +48,32 @@ var (

vtGateProxy *VTGateProxy = &VTGateProxy{
targetConns: map[string]*vtgateconn.VTGateConn{},
mu: sync.Mutex{},
mu: sync.RWMutex{},
}
)

type VTGateProxy struct {
targetConns map[string]*vtgateconn.VTGateConn
mu sync.Mutex
mu sync.RWMutex
}

func (proxy *VTGateProxy) getConnection(ctx context.Context, target string) (*vtgateconn.VTGateConn, error) {
log.V(100).Infof("Getting connection for %v\n", target)

// If the connection exists, return it
proxy.mu.Lock()
proxy.mu.RLock()
existingConn := proxy.targetConns[target]
if existingConn != nil {
proxy.mu.Unlock()
proxy.mu.RUnlock()
log.V(100).Infof("Reused connection for %v\n", target)
return existingConn, nil
}
proxy.mu.Unlock()

// No luck, need to create a new one. Serialize new additions so we don't create multiple
// for a given target.
log.V(100).Infof("Need to create connection for %v\n", target)
proxy.mu.RUnlock()
proxy.mu.Lock()

// Otherwise create a new connection after dropping the lock, allowing multiple requests to
// race to create the conn for now.
Expand All @@ -82,7 +87,6 @@ func (proxy *VTGateProxy) getConnection(ctx context.Context, target string) (*vt
}

log.V(100).Infof("Created new connection for %v\n", target)
proxy.mu.Lock()
proxy.targetConns[target] = conn
proxy.mu.Unlock()

Expand Down

0 comments on commit 1b62a71

Please sign in to comment.