-
Notifications
You must be signed in to change notification settings - Fork 181
Unify db connections, add otel to pool #1371
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
djeebus
wants to merge
4
commits into
main
Choose a base branch
from
add-otel-to-sql
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,76 +1,18 @@ | ||
| package client | ||
|
|
||
| import ( | ||
| "context" | ||
|
|
||
| "github.com/jackc/pgx/v5" | ||
| "github.com/jackc/pgx/v5/pgxpool" | ||
| _ "github.com/lib/pq" | ||
| "go.uber.org/zap" | ||
|
|
||
| database "github.com/e2b-dev/infra/packages/db/queries" | ||
| "github.com/e2b-dev/infra/packages/shared/pkg/utils" | ||
| ) | ||
|
|
||
| type Client struct { | ||
| *database.Queries | ||
|
|
||
| conn *pgxpool.Pool | ||
| } | ||
|
|
||
| type Option func(config *pgxpool.Config) | ||
|
|
||
| func WithMaxConnections(maxConns int32) Option { | ||
| return func(config *pgxpool.Config) { | ||
| config.MaxConns = maxConns | ||
| } | ||
| } | ||
|
|
||
| func WithMinIdle(minIdle int32) Option { | ||
| return func(config *pgxpool.Config) { | ||
| config.MinIdleConns = minIdle | ||
| } | ||
| } | ||
|
|
||
| func NewClient(ctx context.Context, options ...Option) (*Client, error) { | ||
| databaseURL := utils.RequiredEnv("POSTGRES_CONNECTION_STRING", "Postgres connection string") | ||
|
|
||
| // Parse the connection pool configuration | ||
| config, err := pgxpool.ParseConfig(databaseURL) | ||
| if err != nil { | ||
| zap.L().Error("Unable to parse database URL", zap.Error(err)) | ||
|
|
||
| return nil, err | ||
| } | ||
|
|
||
| // Set the default number of connections | ||
| for _, option := range options { | ||
| option(config) | ||
| } | ||
|
|
||
| // Create the connection pool | ||
| pool, err := pgxpool.NewWithConfig(ctx, config) | ||
| if err != nil { | ||
| zap.L().Error("Unable to create connection pool", zap.Error(err)) | ||
| } | ||
|
|
||
| func NewClient(pool *pgxpool.Pool) *Client { | ||
| queries := database.New(pool) | ||
|
|
||
| return &Client{Queries: queries, conn: pool}, nil | ||
| } | ||
|
|
||
| func (db *Client) Close() error { | ||
| db.conn.Close() | ||
| return nil | ||
| } | ||
|
|
||
| // WithTx runs the given function in a transaction. | ||
| func (db *Client) WithTx(ctx context.Context) (*Client, pgx.Tx, error) { | ||
| tx, err := db.conn.BeginTx(ctx, pgx.TxOptions{}) | ||
| if err != nil { | ||
| return nil, nil, err | ||
| } | ||
|
|
||
| client := &Client{Queries: db.Queries.WithTx(tx), conn: db.conn} | ||
| return client, tx, nil | ||
| return &Client{Queries: queries} | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bug: Database Pool Not Closed on Shutdown
The database connection pool (
dbPool) is created but not reliably closed across multiple services. This leads to resource leaks, asdbPool.Close()is either missing from cleanup routines or placed where it won't execute during graceful application shutdown.Additional Locations (2)
packages/docker-reverse-proxy/main.go#L102-L111tests/integration/seed.go#L48-L58