Skip to content
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

pgxstore: Fix race condition in cleanup #207

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pgxstore/pgxstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ func New(pool *pgxpool.Pool) *PostgresStore {
func NewWithCleanupInterval(pool *pgxpool.Pool, cleanupInterval time.Duration) *PostgresStore {
p := &PostgresStore{pool: pool}
if cleanupInterval > 0 {
p.stopCleanup = make(chan bool)
go p.startCleanup(cleanupInterval)
}
return p
Expand Down Expand Up @@ -99,7 +100,6 @@ func (p *PostgresStore) All() (map[string][]byte, error) {
}

func (p *PostgresStore) startCleanup(interval time.Duration) {
p.stopCleanup = make(chan bool)
ticker := time.NewTicker(interval)
for {
select {
Expand Down
21 changes: 21 additions & 0 deletions pgxstore/pgxstore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"context"
"os"
"reflect"
"strconv"
"testing"
"time"

Expand Down Expand Up @@ -241,6 +242,26 @@ func TestCleanup(t *testing.T) {
}
}

func TestStopCleanup(t *testing.T) {
ctx := context.Background()

dsn := os.Getenv("SCS_POSTGRES_TEST_DSN")
pool, err := pgxpool.New(ctx, dsn)
if err != nil {
t.Fatal(err)
}
defer pool.Close()

for i := 0; i < 100; i++ {
t.Run(strconv.Itoa(i), func(t *testing.T) {
t.Parallel()
p := New(pool)
time.Sleep(100 * time.Millisecond)
defer p.StopCleanup()
})
}
}

func TestStopNilCleanup(t *testing.T) {
dsn := os.Getenv("SCS_POSTGRES_TEST_DSN")
pool, err := pgxpool.New(context.Background(), dsn)
Expand Down