Skip to content

Commit

Permalink
fix(db): fixed StmtMaxIdleTime and time.After issues (#38)
Browse files Browse the repository at this point in the history
  • Loading branch information
cnlangzi authored Apr 20, 2024
1 parent 536481b commit 665c28e
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 16 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [1.4.5] - 2014-04-19
## [1.4.5] - 2014-04-20
### Changed
- used int64 instead of int in `Queryer.Count` (#37)
- fixed timer performance issue (#38)
- fixed StmtMaxIdleTime missing issue (#38)

## [1.4.4] - 2014-04-19
### Added
Expand Down
5 changes: 4 additions & 1 deletion context_stmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,11 @@ func (db *Context) closeStaleStmt() {
}

func (db *Context) checkIdleStmt() {
delay := time.NewTicker(db.stmtMaxIdleTime)
defer delay.Stop()

for {
<-time.After(db.stmtMaxIdleTime)
<-delay.C

db.closeStaleStmt()
}
Expand Down
8 changes: 6 additions & 2 deletions context_stmt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,11 @@ func TestStmt(t *testing.T) {
_, err = d.Exec("INSERT INTO `rows`(`id`) VALUES(4)")
require.NoError(t, err)

db := Open(d)
stmtMaxIdleTime := StmtMaxIdleTime

db.stmtMaxIdleTime = 1 * time.Second
StmtMaxIdleTime = 1 * time.Second
db := Open(d)
StmtMaxIdleTime = stmtMaxIdleTime

tests := []struct {
name string
Expand Down Expand Up @@ -123,7 +125,9 @@ func TestStmt(t *testing.T) {
require.NoError(t, err)
require.Equal(t, int64(1), affected)

db.stmtsMutex.Lock()
s, ok := db.stmts[q]
db.stmtsMutex.Unlock()
require.True(t, ok)
require.False(t, s.isUsing)

Expand Down
22 changes: 10 additions & 12 deletions db.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,25 +27,22 @@ type DB struct {
// Open creates a new DB instance with the provided database connections.
func Open(dbs ...*sql.DB) *DB {
d := &DB{
Context: &Context{
DB: dbs[0],
stmts: make(map[string]*Stmt),
Index: 0,
stmtMaxIdleTime: StmtMaxIdleTime,
},
dhts: make(map[string]*shardid.DHT),
}

for i, db := range dbs {
ctx := &Context{
DB: db,
Index: i,
stmts: make(map[string]*Stmt),
DB: db,
Index: i,
stmts: make(map[string]*Stmt),
stmtMaxIdleTime: StmtMaxIdleTime,
}
d.dbs = append(d.dbs, ctx)
go ctx.checkIdleStmt()
}

d.Context = d.dbs[0]

return d
}

Expand All @@ -58,9 +55,10 @@ func (db *DB) Add(dbs ...*sql.DB) {

for i, d := range dbs {
ctx := &Context{
DB: d,
Index: n + i,
stmts: make(map[string]*Stmt),
DB: d,
Index: n + i,
stmts: make(map[string]*Stmt),
stmtMaxIdleTime: StmtMaxIdleTime,
}
db.dbs = append(db.dbs, ctx)
go ctx.checkIdleStmt()
Expand Down

0 comments on commit 665c28e

Please sign in to comment.