feat: 🎸 set the default timeout for every session
This commit is contained in:
parent
779a74ccff
commit
0851ae825d
11
engine.go
11
engine.go
|
@ -46,6 +46,7 @@ type Engine struct {
|
||||||
DatabaseTZ *time.Location // The timezone of the database
|
DatabaseTZ *time.Location // The timezone of the database
|
||||||
|
|
||||||
logSessionID bool // create session id
|
logSessionID bool // create session id
|
||||||
|
timeoutSecond uint // if > 0 set default timeout for session
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewEngine new a db manager according to the parameter. Currently support four
|
// NewEngine new a db manager according to the parameter. Currently support four
|
||||||
|
@ -273,6 +274,16 @@ func (engine *Engine) SetDefaultCacher(cacher caches.Cacher) {
|
||||||
engine.cacherMgr.SetDefaultCacher(cacher)
|
engine.cacherMgr.SetDefaultCacher(cacher)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SetSessionTimeout set the default timeout for every session
|
||||||
|
func (engine *Engine) SetSessionTimeout(timeout uint) {
|
||||||
|
engine.timeoutSecond = timeout
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetDefaultTimeout return the default timeout of new session
|
||||||
|
func (engine *Engine) GetDefaultTimeout() uint {
|
||||||
|
return engine.timeoutSecond
|
||||||
|
}
|
||||||
|
|
||||||
// GetDefaultCacher returns the default cacher
|
// GetDefaultCacher returns the default cacher
|
||||||
func (engine *Engine) GetDefaultCacher() caches.Cacher {
|
func (engine *Engine) GetDefaultCacher() caches.Cacher {
|
||||||
return engine.cacherMgr.GetDefaultCacher()
|
return engine.cacherMgr.GetDefaultCacher()
|
||||||
|
|
10
session.go
10
session.go
|
@ -85,6 +85,7 @@ type Session struct {
|
||||||
lastSQLArgs []interface{}
|
lastSQLArgs []interface{}
|
||||||
|
|
||||||
ctx context.Context
|
ctx context.Context
|
||||||
|
cancel context.CancelFunc
|
||||||
sessionType sessionType
|
sessionType sessionType
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -106,9 +107,13 @@ func newSession(engine *Engine) *Session {
|
||||||
} else {
|
} else {
|
||||||
ctx = engine.defaultContext
|
ctx = engine.defaultContext
|
||||||
}
|
}
|
||||||
|
var cancel context.CancelFunc
|
||||||
|
if engine.timeoutSecond > 0 {
|
||||||
|
ctx, cancel = context.WithTimeout(ctx, time.Second*time.Duration(engine.timeoutSecond))
|
||||||
|
}
|
||||||
session := &Session{
|
session := &Session{
|
||||||
ctx: ctx,
|
ctx: ctx,
|
||||||
|
cancel: cancel,
|
||||||
engine: engine,
|
engine: engine,
|
||||||
tx: nil,
|
tx: nil,
|
||||||
statement: statements.NewStatement(
|
statement: statements.NewStatement(
|
||||||
|
@ -144,6 +149,9 @@ func newSession(engine *Engine) *Session {
|
||||||
|
|
||||||
// Close release the connection from pool
|
// Close release the connection from pool
|
||||||
func (session *Session) Close() error {
|
func (session *Session) Close() error {
|
||||||
|
if session.cancel != nil{
|
||||||
|
session.cancel()
|
||||||
|
}
|
||||||
for _, v := range session.stmtCache {
|
for _, v := range session.stmtCache {
|
||||||
if err := v.Close(); err != nil {
|
if err := v.Close(); err != nil {
|
||||||
return err
|
return err
|
||||||
|
|
Loading…
Reference in New Issue