Skip to content

Commit

Permalink
fix: leaked connections
Browse files Browse the repository at this point in the history
Creating a transaction and failing to end it with either a commit or
rollback leaves db pool connections in an acquired state, unavailable to
other requests. There were two instances where this was happening, this
resulted in the service eventually becoming unresponsive after the
offending code was called enough times and all the connections were
exhausted.
  • Loading branch information
crazybolillo committed Oct 8, 2024
1 parent 25cbcbf commit f33d7ed
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 7 deletions.
8 changes: 1 addition & 7 deletions internal/service/bouncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,7 @@ func (b *Bouncer) Check(ctx context.Context, endpoint, dialed string) model.Boun
Allow: false,
}

tx, err := b.Begin(ctx)
if err != nil {
slog.Error("Unable to start transaction", slog.String("reason", err.Error()))
return result
}

queries := sqlc.New(tx)
queries := sqlc.New(b.Cursor)
row, err := queries.GetEndpointByExtension(ctx, sqlc.GetEndpointByExtensionParams{
ID: endpoint,
Extension: db.Text(dialed),
Expand Down
1 change: 1 addition & 0 deletions internal/service/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ func (e *EndpointService) Update(ctx context.Context, sid int32, payload model.P
if err != nil {
return model.Endpoint{}, err
}
defer tx.Rollback(ctx)

queries := sqlc.New(tx)
endpoint, err := queries.GetEndpointByID(ctx, sid)
Expand Down

0 comments on commit f33d7ed

Please sign in to comment.