Skip to content

Commit fd60c13

Browse files
committed
feat: add tableName config for all table based stores
1 parent 421ef1d commit fd60c13

File tree

7 files changed

+297
-91
lines changed

7 files changed

+297
-91
lines changed

cockroachdbstore/cockroachdbstore.go

Lines changed: 44 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,39 +2,68 @@ package cockroachdbstore
22

33
import (
44
"database/sql"
5+
"fmt"
56
"log"
67
"time"
78
)
89

10+
type Config struct {
11+
// CleanUpInterval is the interval between each cleanup operation.
12+
// If set to 0, the cleanup operation is disabled.
13+
CleanUpInterval time.Duration
14+
15+
// TableName is the name of the table where the session data will be stored.
16+
// If not set, it will default to "sessions".
17+
TableName string
18+
}
19+
920
// CockroachDBStore represents the session store.
1021
type CockroachDBStore struct {
1122
db *sql.DB
1223
stopCleanup chan bool
24+
tableName string
1325
}
1426

1527
// New returns a new CockroachDBStore instance, with a background cleanup goroutine
1628
// that runs every 5 minutes to remove expired session data.
1729
func New(db *sql.DB) *CockroachDBStore {
18-
return NewWithCleanupInterval(db, 5*time.Minute)
30+
return NewWithConfig(db, Config{CleanUpInterval: 5 * time.Minute})
1931
}
2032

2133
// NewWithCleanupInterval returns a new CockroachDBStore instance. The cleanupInterval
2234
// parameter controls how frequently expired session data is removed by the
2335
// background cleanup goroutine. Setting it to 0 prevents the cleanup goroutine
2436
// from running (i.e. expired sessions will not be removed).
2537
func NewWithCleanupInterval(db *sql.DB, cleanupInterval time.Duration) *CockroachDBStore {
26-
p := &CockroachDBStore{db: db}
27-
if cleanupInterval > 0 {
28-
go p.startCleanup(cleanupInterval)
38+
return NewWithConfig(db, Config{CleanUpInterval: cleanupInterval})
39+
}
40+
41+
// NewWithConfig returns a new CockroachDBStore instance using the provided config.
42+
// If the TableName field is empty, it will be set to "sessions".
43+
// If the CleanUpInterval field is 0, the cleanup goroutine will not be started.
44+
func NewWithConfig(db *sql.DB, config Config) *CockroachDBStore {
45+
if config.TableName == "" {
46+
config.TableName = "sessions"
47+
}
48+
49+
store := &CockroachDBStore{
50+
db: db,
51+
tableName: config.TableName,
2952
}
30-
return p
53+
54+
if config.CleanUpInterval > 0 {
55+
go store.startCleanup(config.CleanUpInterval)
56+
}
57+
58+
return store
3159
}
3260

3361
// Find returns the data for a given session token from the CockroachDBStore instance.
3462
// If the session token is not found or is expired, the returned exists flag will
3563
// be set to false.
3664
func (p *CockroachDBStore) Find(token string) (b []byte, exists bool, err error) {
37-
row := p.db.QueryRow("SELECT data FROM sessions WHERE token = $1 AND current_timestamp < expiry", token)
65+
query := fmt.Sprintf("SELECT data FROM %s WHERE token = $1 AND current_timestamp < expiry", p.tableName)
66+
row := p.db.QueryRow(query, token)
3867
err = row.Scan(&b)
3968
if err == sql.ErrNoRows {
4069
return nil, false, nil
@@ -48,24 +77,24 @@ func (p *CockroachDBStore) Find(token string) (b []byte, exists bool, err error)
4877
// given expiry time. If the session token already exists, then the data and expiry
4978
// time are updated.
5079
func (p *CockroachDBStore) Commit(token string, b []byte, expiry time.Time) error {
51-
_, err := p.db.Exec("INSERT INTO sessions (token, data, expiry) VALUES ($1, $2, $3) ON CONFLICT (token) DO UPDATE SET data = EXCLUDED.data, expiry = EXCLUDED.expiry", token, b, expiry)
52-
if err != nil {
53-
return err
54-
}
55-
return nil
80+
query := fmt.Sprintf("INSERT INTO %s (token, data, expiry) VALUES ($1, $2, $3) ON CONFLICT (token) DO UPDATE SET data = EXCLUDED.data, expiry = EXCLUDED.expiry", p.tableName)
81+
_, err := p.db.Exec(query, token, b, expiry)
82+
return err
5683
}
5784

5885
// Delete removes a session token and corresponding data from the CockroachDBStore
5986
// instance.
6087
func (p *CockroachDBStore) Delete(token string) error {
61-
_, err := p.db.Exec("DELETE FROM sessions WHERE token = $1", token)
88+
query := fmt.Sprintf("DELETE FROM %s WHERE token = $1", p.tableName)
89+
_, err := p.db.Exec(query, token)
6290
return err
6391
}
6492

6593
// All returns a map containing the token and data for all active (i.e.
6694
// not expired) sessions in the CockroachDBStore instance.
6795
func (p *CockroachDBStore) All() (map[string][]byte, error) {
68-
rows, err := p.db.Query("SELECT token, data FROM sessions WHERE current_timestamp < expiry")
96+
query := fmt.Sprintf("SELECT token, data FROM %s WHERE current_timestamp < expiry", p.tableName)
97+
rows, err := p.db.Query(query)
6998
if err != nil {
7099
return nil, err
71100
}
@@ -129,6 +158,7 @@ func (p *CockroachDBStore) StopCleanup() {
129158
}
130159

131160
func (p *CockroachDBStore) deleteExpired() error {
132-
_, err := p.db.Exec("DELETE FROM sessions WHERE expiry < current_timestamp")
161+
query := fmt.Sprintf("DELETE FROM %s WHERE expiry < current_timestamp", p.tableName)
162+
_, err := p.db.Exec(query)
133163
return err
134164
}

mongodbstore/mongodbstore.go

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,25 +22,49 @@ type MongoDBStore struct {
2222
stopCleanup chan bool
2323
}
2424

25+
type Config struct {
26+
// CleanUpInterval is the interval between each cleanup operation.
27+
// If set to 0, the cleanup operation is disabled.
28+
CleanupInterval time.Duration
29+
30+
// CollectionName is the name of the collection where the session data will be stored.
31+
// If not set, it will default to "sessions".
32+
CollectionName string
33+
}
34+
2535
// New returns a new MongoDBStore instance, with a background cleanup goroutine that
2636
// runs every minute to remove expired session data.
2737
func New(db *mongo.Database) *MongoDBStore {
28-
return NewWithCleanupInterval(db, time.Minute)
38+
return NewWithConfig(db, Config{
39+
CleanupInterval: time.Minute,
40+
})
2941
}
3042

3143
// NewWithCleanupInterval returns a new MongoDBStore instance. The cleanupInterval
3244
// parameter controls how frequently expired session data is removed by the
3345
// background cleanup goroutine. Setting it to 0 prevents the cleanup goroutine
3446
// from running (i.e. expired sessions will not be removed).
3547
func NewWithCleanupInterval(db *mongo.Database, cleanupInterval time.Duration) *MongoDBStore {
36-
collection := db.Collection("sessions")
48+
return NewWithConfig(db, Config{
49+
CleanupInterval: cleanupInterval,
50+
})
51+
}
52+
53+
// NewWithConfig returns a new MongoDBStore instance with the given configuration.
54+
// If the CollectionName field is empty, it will be set to "sessions".
55+
// If the CleanupInterval field is 0, the cleanup goroutine will not be started.
56+
func NewWithConfig(db *mongo.Database, config Config) *MongoDBStore {
57+
if config.CollectionName == "" {
58+
config.CollectionName = "sessions"
59+
}
60+
collection := db.Collection(config.CollectionName)
3761

3862
m := &MongoDBStore{
3963
collection: collection,
4064
}
4165

42-
if cleanupInterval > 0 {
43-
go m.startCleanup(cleanupInterval)
66+
if config.CleanupInterval > 0 {
67+
go m.startCleanup(config.CleanupInterval)
4468
}
4569

4670
return m

mssqlstore/mssqlstore.go

Lines changed: 46 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package mssqlstore
22

33
import (
44
"database/sql"
5+
"fmt"
56
"log"
67
"time"
78
)
@@ -10,31 +11,59 @@ import (
1011
type MSSQLStore struct {
1112
db *sql.DB
1213
stopCleanup chan bool
14+
tableName string
15+
}
16+
17+
type Config struct {
18+
// CleanUpInterval is the interval between each cleanup operation.
19+
// If set to 0, the cleanup operation is disabled.
20+
CleanUpInterval time.Duration
21+
22+
// TableName is the name of the table where the session data will be stored.
23+
// If not set, it will default to "sessions".
24+
TableName string
1325
}
1426

1527
// New returns a new MSSQLStore instance, with a background cleanup goroutine
1628
// that runs every 5 minutes to remove expired session data.
1729
func New(db *sql.DB) *MSSQLStore {
18-
return NewWithCleanupInterval(db, 5*time.Minute)
30+
return NewWithConfig(db, Config{
31+
CleanUpInterval: 5 * time.Minute,
32+
})
1933
}
2034

2135
// NewWithCleanupInterval returns a new MSSQLStore instance. The cleanupInterval
2236
// parameter controls how frequently expired session data is removed by the
2337
// background cleanup goroutine. Setting it to 0 prevents the cleanup goroutine
2438
// from running (i.e. expired sessions will not be removed).
2539
func NewWithCleanupInterval(db *sql.DB, cleanupInterval time.Duration) *MSSQLStore {
26-
m := &MSSQLStore{db: db}
27-
if cleanupInterval > 0 {
28-
go m.startCleanup(cleanupInterval)
40+
return NewWithConfig(db, Config{
41+
CleanUpInterval: cleanupInterval,
42+
})
43+
}
44+
45+
// NewWithConfig returns a new MSSQLStore instance with the given configuration.
46+
// If the TableName field is empty, it will be set to "sessions".
47+
// If the CleanUpInterval field is 0, the cleanup goroutine will not be started.
48+
func NewWithConfig(db *sql.DB, config Config) *MSSQLStore {
49+
if config.TableName == "" {
50+
config.TableName = "sessions"
2951
}
52+
53+
m := &MSSQLStore{db: db, tableName: config.TableName}
54+
if config.CleanUpInterval > 0 {
55+
go m.startCleanup(config.CleanUpInterval)
56+
}
57+
3058
return m
3159
}
3260

3361
// Find returns the data for a given session token from the MSSQLStore instance.
3462
// If the session token is not found or is expired, the returned exists flag will
3563
// be set to false.
3664
func (m *MSSQLStore) Find(token string) (b []byte, exists bool, err error) {
37-
row := m.db.QueryRow("SELECT data FROM sessions WHERE token = @p1 AND GETUTCDATE() < expiry", token)
65+
query := fmt.Sprintf("SELECT data FROM %s WHERE token = @p1 AND GETUTCDATE() < expiry", m.tableName)
66+
row := m.db.QueryRow(query, token)
3867
err = row.Scan(&b)
3968
if err == sql.ErrNoRows {
4069
return nil, false, nil
@@ -48,26 +77,26 @@ func (m *MSSQLStore) Find(token string) (b []byte, exists bool, err error) {
4877
// given expiry time. If the session token already exists, then the data and expiry
4978
// time are updated.
5079
func (m *MSSQLStore) Commit(token string, b []byte, expiry time.Time) error {
51-
_, err := m.db.Exec(`MERGE INTO sessions WITH (HOLDLOCK) AS T USING (VALUES(@p1)) AS S (token) ON (T.token = S.token)
52-
WHEN MATCHED THEN UPDATE SET data = @p2, expiry = @p3
53-
WHEN NOT MATCHED THEN INSERT (token, data, expiry) VALUES(@p1, @p2, @p3);`, token, b, expiry.UTC())
54-
if err != nil {
55-
return err
56-
}
57-
return nil
80+
query := fmt.Sprintf(`MERGE INTO %s WITH (HOLDLOCK) AS T USING (VALUES(@p1)) AS S (token) ON (T.token = S.token)
81+
WHEN MATCHED THEN UPDATE SET data = @p2, expiry = @p3
82+
WHEN NOT MATCHED THEN INSERT (token, data, expiry) VALUES(@p1, @p2, @p3);`, m.tableName)
83+
_, err := m.db.Exec(query, token, b, expiry.UTC())
84+
return err
5885
}
5986

6087
// Delete removes a session token and corresponding data from the MSSQLStore
6188
// instance.
6289
func (m *MSSQLStore) Delete(token string) error {
63-
_, err := m.db.Exec("DELETE FROM sessions WHERE token = @p1", token)
90+
query := fmt.Sprintf("DELETE FROM %s WHERE token = @p1", m.tableName)
91+
_, err := m.db.Exec(query, token)
6492
return err
6593
}
6694

6795
// All returns a map containing the token and data for all active (i.e.
6896
// not expired) sessions in the MSSQLStore instance.
6997
func (m *MSSQLStore) All() (map[string][]byte, error) {
70-
rows, err := m.db.Query("SELECT token, data FROM sessions WHERE GETUTCDATE() < expiry")
98+
query := fmt.Sprintf("SELECT token, data FROM %s WHERE GETUTCDATE() < expiry", m.tableName)
99+
rows, err := m.db.Query(query)
71100
if err != nil {
72101
return nil, err
73102
}
@@ -115,7 +144,7 @@ func (m *MSSQLStore) startCleanup(interval time.Duration) {
115144
}
116145

117146
// StopCleanup terminates the background cleanup goroutine for the MSSQLStore
118-
// instance. It's rare to terminate this; generally MSSQLStore instances and
147+
// instance. It's rare to terminate this; generally MSSQLStore instances and // instance.
119148
// their cleanup goroutines are intended to be long-lived and run for the lifetime
120149
// of your application.
121150
//
@@ -131,6 +160,7 @@ func (m *MSSQLStore) StopCleanup() {
131160
}
132161

133162
func (m *MSSQLStore) deleteExpired() error {
134-
_, err := m.db.Exec("DELETE FROM sessions WHERE expiry < GETUTCDATE()")
163+
query := fmt.Sprintf("DELETE FROM %s WHERE expiry < GETUTCDATE()", m.tableName)
164+
_, err := m.db.Exec(query)
135165
return err
136166
}

0 commit comments

Comments
 (0)