From f33d7ed399ddca94eb616c534016a4704ccd75ec Mon Sep 17 00:00:00 2001 From: CrazyBolillo Date: Mon, 7 Oct 2024 19:56:39 -0600 Subject: [PATCH] fix: leaked connections 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. --- internal/service/bouncer.go | 8 +------- internal/service/endpoint.go | 1 + 2 files changed, 2 insertions(+), 7 deletions(-) diff --git a/internal/service/bouncer.go b/internal/service/bouncer.go index bf63259..5313000 100644 --- a/internal/service/bouncer.go +++ b/internal/service/bouncer.go @@ -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), diff --git a/internal/service/endpoint.go b/internal/service/endpoint.go index ae80883..4c55195 100644 --- a/internal/service/endpoint.go +++ b/internal/service/endpoint.go @@ -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)