Skip to content

Commit

Permalink
Minor code clean-ups (#554)
Browse files Browse the repository at this point in the history
  • Loading branch information
robsdedude authored Dec 14, 2023
1 parent 84b51fe commit ddb0635
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
24 changes: 17 additions & 7 deletions neo4j/internal/pool/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,14 @@ serverLoop:
return nil, nil
}

func (p *Pool) Borrow(ctx context.Context, getServerNames func() []string, wait bool, boltLogger log.BoltLogger, idlenessTimeout time.Duration, auth *idb.ReAuthToken) (idb.Connection, error) {
func (p *Pool) Borrow(
ctx context.Context,
getServerNames func() []string,
wait bool,
boltLogger log.BoltLogger,
idlenessTimeout time.Duration,
auth *idb.ReAuthToken,
) (idb.Connection, error) {
for {
if p.closed {
return nil, &errorutil.PoolClosed{}
Expand Down Expand Up @@ -279,10 +286,13 @@ func (p *Pool) Borrow(ctx context.Context, getServerNames func() []string, wait
}
}

func (p *Pool) tryBorrow(ctx context.Context, serverName string, boltLogger log.BoltLogger, idlenessTimeout time.Duration, auth *idb.ReAuthToken) (idb.Connection, error) {
// For now, lock complete servers map to avoid over connecting but with the downside
// that long connect times will block connects to other servers as well. To fix this
// we would need to add a pending connect to the server and lock per server.
func (p *Pool) tryBorrow(
ctx context.Context,
serverName string,
boltLogger log.BoltLogger,
idlenessTimeout time.Duration,
auth *idb.ReAuthToken,
) (idb.Connection, error) {
p.serversMut.Lock()
var unlock = new(sync.Once)
defer unlock.Do(p.serversMut.Unlock)
Expand Down Expand Up @@ -346,10 +356,10 @@ func (p *Pool) tryBorrow(ctx context.Context, serverName string, boltLogger log.
func (p *Pool) unreg(ctx context.Context, serverName string, c idb.Connection, now time.Time) {
p.serversMut.Lock()
defer p.serversMut.Unlock()
p.unregUnlocked(ctx, serverName, c, now)
p.unregLocked(ctx, serverName, c, now)
}

func (p *Pool) unregUnlocked(ctx context.Context, serverName string, c idb.Connection, now time.Time) {
func (p *Pool) unregLocked(ctx context.Context, serverName string, c idb.Connection, now time.Time) {
defer func() {
// Close connection in another thread to avoid potential long blocking operation during close.
go c.Close(ctx)
Expand Down
4 changes: 1 addition & 3 deletions neo4j/session_with_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -524,9 +524,7 @@ func (s *sessionWithContext) getConnection(ctx context.Context, mode idb.AccessM
if timeout > 0 {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, timeout)
if cancel != nil {
defer cancel()
}
defer cancel()
deadline, _ := ctx.Deadline()
s.log.Debugf(log.Session, s.logId, "connection acquisition timeout is %s, resolved deadline is: %s", timeout, deadline)
} else if deadline, ok := ctx.Deadline(); ok {
Expand Down

0 comments on commit ddb0635

Please sign in to comment.