-
Notifications
You must be signed in to change notification settings - Fork 441
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
contrib/database/sql: Close DB Stats goroutine on db.Close() #3025
Open
mtoffl01
wants to merge
19
commits into
main
Choose a base branch
from
mtoff/contrib-routines
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.
Open
Changes from 15 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
8f6d3a0
implement stopchan on contribroutines
mtoffl01 c7f7fd3
initialize channel at package root
mtoffl01 1b8bb5f
initialize stop channel at package root
mtoffl01 f0e844b
remove comment
mtoffl01 f0f73af
copywrite header
mtoffl01 bf4f376
Cleanup var declaration
mtoffl01 9e86acc
lock access to prevent race condition
mtoffl01 12241c9
added race detection tests to contribroutines_test.go
mtoffl01 41c3ad9
move stop chan initialization out of pollDBStats
mtoffl01 2cb3519
Apply stopchan to pgx pollPoolStats
mtoffl01 37b52f9
move lock outside of once.Do
mtoffl01 291e014
Merge branch 'main' into mtoff/contrib-routines
mtoffl01 11f13fa
Hook pollDBStats stop condition into db.Close() invocation
mtoffl01 42935d6
nits
mtoffl01 3fc1121
make tracer initialize new contribroutines.stop channel on startup
mtoffl01 5b116f8
Add reminder: implement stop condition on db.Close for pgx
mtoffl01 79ee5f5
revamp: tie pollDBStats to lifetime of db connection, only
mtoffl01 5bb8a4c
remove unused helper fn
mtoffl01 7b93aa2
nits: cleanup comments, fix undefined code call in metrics_test.go
mtoffl01 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 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 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 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 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 |
---|---|---|
|
@@ -7,6 +7,7 @@ package sql | |
|
||
import ( | ||
"context" | ||
"database/sql" | ||
"database/sql/driver" | ||
"errors" | ||
"fmt" | ||
|
@@ -529,3 +530,26 @@ func TestNamingSchema(t *testing.T) { | |
t.Run("SpanName", namingschematest.NewSpanNameTest(genSpans, assertOpV0, assertOpV1)) | ||
}) | ||
} | ||
|
||
func TestDBClose(t *testing.T) { | ||
db := setupPostgres(t) | ||
|
||
// assert that dbStop channel is closed on the call to db.Close() | ||
var wg sync.WaitGroup | ||
wg.Add(1) | ||
go func() { | ||
<-dbStop | ||
wg.Done() | ||
}() | ||
db.Close() | ||
wg.Wait() | ||
} | ||
|
||
func setupPostgres(t *testing.T) *sql.DB { | ||
driverName := "postgres" | ||
Register(driverName, &pq.Driver{}) | ||
defer unregister(driverName) | ||
db, err := Open(driverName, "postgres://postgres:[email protected]:5432/postgres?sslmode=disable") | ||
require.NoError(t, err) | ||
return db | ||
} |
This file contains 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 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 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 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 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 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 | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,34 @@ | ||||||
// Unless explicitly stated otherwise all files in this repository are licensed | ||||||
// under the Apache License Version 2.0. | ||||||
// This product includes software developed at Datadog (https://www.datadoghq.com/). | ||||||
// Copyright 2016 Datadog, Inc. | ||||||
package contribroutines | ||||||
|
||||||
import "sync" | ||||||
|
||||||
var ( | ||||||
stop chan struct{} | ||||||
once sync.Once | ||||||
mu sync.Mutex | ||||||
) | ||||||
|
||||||
func InitStopChan() { | ||||||
stop = make(chan struct{}) | ||||||
} | ||||||
|
||||||
func Stop() { | ||||||
mu.Lock() | ||||||
defer mu.Unlock() | ||||||
if stop == nil { | ||||||
InitStopChan() | ||||||
} | ||||||
once.Do(func() { | ||||||
close(stop) | ||||||
}) | ||||||
} | ||||||
|
||||||
func GetStopChan() chan struct{} { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
This would be a better name. |
||||||
mu.Lock() | ||||||
defer mu.Unlock() | ||||||
return stop | ||||||
} |
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.
Just in case this package evolves and needs to initialize more things beyond the stop chan.