Skip to content
This repository was archived by the owner on Sep 30, 2024. It is now read-only.

Commit 3d41fa9

Browse files
authored
chore/enterpriseportal: properly close DB handle (#63426)
Adds a clean shutdown for our primary DB handle, and removes some unused code. ## Test plan `sg run enterprise-portal` and stop it
1 parent 1f71b47 commit 3d41fa9

File tree

3 files changed

+35
-41
lines changed

3 files changed

+35
-41
lines changed

cmd/enterprise-portal/internal/database/BUILD.bazel

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ go_library(
1515
"//lib/errors",
1616
"//lib/managedservicesplatform/runtime",
1717
"//lib/redislock",
18-
"@com_github_jackc_pgx_v5//:pgx",
1918
"@com_github_jackc_pgx_v5//pgxpool",
2019
"@com_github_redis_go_redis_v9//:go-redis",
2120
"@com_github_sourcegraph_log//:log",

cmd/enterprise-portal/internal/database/database.go

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"context"
55
"os"
66

7-
"github.com/jackc/pgx/v5"
87
"github.com/jackc/pgx/v5/pgxpool"
98
"github.com/redis/go-redis/v9"
109
"github.com/sourcegraph/log"
@@ -52,27 +51,8 @@ func NewHandle(ctx context.Context, logger log.Logger, contract runtime.Contract
5251
return &DB{db: pool}, nil
5352
}
5453

55-
// transaction executes the given function within a transaction. If the function
56-
// returns an error, the transaction will be rolled back.
57-
func transaction(ctx context.Context, db *pgxpool.Pool, fn func(tx pgx.Tx) error) (err error) {
58-
tx, err := db.Begin(ctx)
59-
if err != nil {
60-
return errors.Wrap(err, "begin")
61-
}
62-
defer func() {
63-
rollbackErr := tx.Rollback(ctx)
64-
// Only return the rollback error if there is no other error.
65-
if err == nil {
66-
err = errors.Wrap(rollbackErr, "rollback")
67-
}
68-
}()
69-
70-
if err = fn(tx); err != nil {
71-
return err
72-
}
73-
74-
if err = tx.Commit(ctx); err != nil {
75-
return errors.Wrap(err, "commit")
76-
}
77-
return nil
54+
// Close closes all connections in the pool and rejects future Acquire calls.
55+
// Blocks until all connections are returned to pool and closed.
56+
func (db *DB) Close() {
57+
db.db.Close()
7858
}

cmd/enterprise-portal/service/service.go

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -166,24 +166,39 @@ func (Service) Initialize(ctx context.Context, logger log.Logger, contract runti
166166
},
167167
)
168168
return background.LIFOStopRoutine{
169-
background.CallbackRoutine{
170-
NameFunc: func() string { return "close IAM client" },
171-
StopFunc: func(context.Context) error {
172-
closeIAMClient()
173-
return nil
169+
// Everything else can be stopped in conjunction
170+
background.CombinedRoutine{
171+
background.CallbackRoutine{
172+
NameFunc: func() string { return "close IAM client" },
173+
StopFunc: func(context.Context) error {
174+
closeIAMClient()
175+
return nil
176+
},
174177
},
175-
},
176-
background.CallbackRoutine{
177-
NameFunc: func() string { return "close dotcom database connection pool" },
178-
StopFunc: func(context.Context) error {
179-
start := time.Now()
180-
// NOTE: If we simply shut down, some in-fly requests may be dropped as the
181-
// service exits, so we attempt to gracefully shutdown first.
182-
dotcomDB.Close()
183-
logger.Info("database connection pool closed", log.Duration("elapsed", time.Since(start)))
184-
return nil
178+
background.CallbackRoutine{
179+
NameFunc: func() string { return "close database handle" },
180+
StopFunc: func(context.Context) error {
181+
start := time.Now()
182+
// NOTE: If we simply shut down, some in-fly requests may be dropped as the
183+
// service exits, so we attempt to gracefully shutdown first.
184+
dbHandle.Close()
185+
logger.Info("database handle closed", log.Duration("elapsed", time.Since(start)))
186+
return nil
187+
},
188+
},
189+
background.CallbackRoutine{
190+
NameFunc: func() string { return "close dotcom database connection pool" },
191+
StopFunc: func(context.Context) error {
192+
start := time.Now()
193+
// NOTE: If we simply shut down, some in-fly requests may be dropped as the
194+
// service exits, so we attempt to gracefully shutdown first.
195+
dotcomDB.Close()
196+
logger.Info("dotcom database connection pool closed", log.Duration("elapsed", time.Since(start)))
197+
return nil
198+
},
185199
},
186200
},
187-
server, // stop server first
201+
// Stop server first
202+
server,
188203
}, nil
189204
}

0 commit comments

Comments
 (0)