add SetConnMaxIdleTime for go1.15 or higher

This commit is contained in:
Shinnosuke Sawada 2020-08-26 06:49:19 +09:00
parent f39a4cb41c
commit 164d992622
No known key found for this signature in database
GPG Key ID: 2BD8D462DBE7A458
5 changed files with 181 additions and 63 deletions

16
engine_go1.15.go Normal file
View File

@ -0,0 +1,16 @@
// +build go1.15
package xorm
import (
"time"
)
// SetConnMaxIdleTime sets the maximum amount of time a connection may be idle.
//
// Expired connections may be closed lazily before reuse.
//
// If d <= 0, connections are not closed due to a connection's idle time.
func (engine *Engine) SetConnMaxIdleTime(d time.Duration) {
engine.DB().SetConnMaxIdleTime(d)
}

19
engine_group_go1.15.go Normal file
View File

@ -0,0 +1,19 @@
// +build go1.15
package xorm
import (
"time"
)
// SetConnMaxIdleTime sets the maximum amount of time a connection may be idle.
//
// Expired connections may be closed lazily before reuse.
//
// If d <= 0, connections are not closed due to a connection's idle time.
func (eg *EngineGroup) SetConnMaxIdleTime(d time.Duration) {
eg.Engine.SetConnMaxIdleTime(d)
for i := 0; i < len(eg.slaves); i++ {
eg.slaves[i].SetConnMaxIdleTime(d)
}
}

72
engine_interface.go Normal file
View File

@ -0,0 +1,72 @@
// +build !go1.15
package xorm
import (
"context"
"database/sql"
"reflect"
"time"
"xorm.io/xorm/caches"
"xorm.io/xorm/contexts"
"xorm.io/xorm/dialects"
"xorm.io/xorm/log"
"xorm.io/xorm/names"
"xorm.io/xorm/schemas"
)
// EngineInterface defines the interface which Engine, EngineGroup will implementate.
type EngineInterface interface {
Interface
Before(func(interface{})) *Session
Charset(charset string) *Session
ClearCache(...interface{}) error
Context(context.Context) *Session
CreateTables(...interface{}) error
DBMetas() ([]*schemas.Table, error)
Dialect() dialects.Dialect
DriverName() string
DropTables(...interface{}) error
DumpAllToFile(fp string, tp ...schemas.DBType) error
GetCacher(string) caches.Cacher
GetColumnMapper() names.Mapper
GetDefaultCacher() caches.Cacher
GetTableMapper() names.Mapper
GetTZDatabase() *time.Location
GetTZLocation() *time.Location
ImportFile(fp string) ([]sql.Result, error)
MapCacher(interface{}, caches.Cacher) error
NewSession() *Session
NoAutoTime() *Session
Quote(string) string
SetCacher(string, caches.Cacher)
SetConnMaxLifetime(time.Duration)
SetColumnMapper(names.Mapper)
SetDefaultCacher(caches.Cacher)
SetLogger(logger interface{})
SetLogLevel(log.LogLevel)
SetMapper(names.Mapper)
SetMaxOpenConns(int)
SetMaxIdleConns(int)
SetQuotePolicy(dialects.QuotePolicy)
SetSchema(string)
SetTableMapper(names.Mapper)
SetTZDatabase(tz *time.Location)
SetTZLocation(tz *time.Location)
AddHook(hook contexts.Hook)
ShowSQL(show ...bool)
Sync(...interface{}) error
Sync2(...interface{}) error
StoreEngine(storeEngine string) *Session
TableInfo(bean interface{}) (*schemas.Table, error)
TableName(interface{}, ...bool) string
UnMapType(reflect.Type)
EnableSessionID(bool)
}
var (
_ EngineInterface = &Engine{}
_ EngineInterface = &EngineGroup{}
)

View File

@ -0,0 +1,73 @@
// +build go1.15
package xorm
import (
"context"
"database/sql"
"reflect"
"time"
"xorm.io/xorm/caches"
"xorm.io/xorm/contexts"
"xorm.io/xorm/dialects"
"xorm.io/xorm/log"
"xorm.io/xorm/names"
"xorm.io/xorm/schemas"
)
// EngineInterface defines the interface which Engine, EngineGroup will implementate.
type EngineInterface interface {
Interface
Before(func(interface{})) *Session
Charset(charset string) *Session
ClearCache(...interface{}) error
Context(context.Context) *Session
CreateTables(...interface{}) error
DBMetas() ([]*schemas.Table, error)
Dialect() dialects.Dialect
DriverName() string
DropTables(...interface{}) error
DumpAllToFile(fp string, tp ...schemas.DBType) error
GetCacher(string) caches.Cacher
GetColumnMapper() names.Mapper
GetDefaultCacher() caches.Cacher
GetTableMapper() names.Mapper
GetTZDatabase() *time.Location
GetTZLocation() *time.Location
ImportFile(fp string) ([]sql.Result, error)
MapCacher(interface{}, caches.Cacher) error
NewSession() *Session
NoAutoTime() *Session
Quote(string) string
SetCacher(string, caches.Cacher)
SetConnMaxLifetime(time.Duration)
SetConnMaxIdleTime(time.Duration) // only go1.15 or higher
SetColumnMapper(names.Mapper)
SetDefaultCacher(caches.Cacher)
SetLogger(logger interface{})
SetLogLevel(log.LogLevel)
SetMapper(names.Mapper)
SetMaxOpenConns(int)
SetMaxIdleConns(int)
SetQuotePolicy(dialects.QuotePolicy)
SetSchema(string)
SetTableMapper(names.Mapper)
SetTZDatabase(tz *time.Location)
SetTZLocation(tz *time.Location)
AddHook(hook contexts.Hook)
ShowSQL(show ...bool)
Sync(...interface{}) error
Sync2(...interface{}) error
StoreEngine(storeEngine string) *Session
TableInfo(bean interface{}) (*schemas.Table, error)
TableName(interface{}, ...bool) string
UnMapType(reflect.Type)
EnableSessionID(bool)
}
var (
_ EngineInterface = &Engine{}
_ EngineInterface = &EngineGroup{}
)

View File

@ -5,17 +5,7 @@
package xorm package xorm
import ( import (
"context"
"database/sql" "database/sql"
"reflect"
"time"
"xorm.io/xorm/caches"
"xorm.io/xorm/contexts"
"xorm.io/xorm/dialects"
"xorm.io/xorm/log"
"xorm.io/xorm/names"
"xorm.io/xorm/schemas"
) )
// Interface defines the interface which Engine, EngineGroup and Session will implementate. // Interface defines the interface which Engine, EngineGroup and Session will implementate.
@ -73,58 +63,6 @@ type Interface interface {
Where(interface{}, ...interface{}) *Session Where(interface{}, ...interface{}) *Session
} }
// EngineInterface defines the interface which Engine, EngineGroup will implementate.
type EngineInterface interface {
Interface
Before(func(interface{})) *Session
Charset(charset string) *Session
ClearCache(...interface{}) error
Context(context.Context) *Session
CreateTables(...interface{}) error
DBMetas() ([]*schemas.Table, error)
Dialect() dialects.Dialect
DriverName() string
DropTables(...interface{}) error
DumpAllToFile(fp string, tp ...schemas.DBType) error
GetCacher(string) caches.Cacher
GetColumnMapper() names.Mapper
GetDefaultCacher() caches.Cacher
GetTableMapper() names.Mapper
GetTZDatabase() *time.Location
GetTZLocation() *time.Location
ImportFile(fp string) ([]sql.Result, error)
MapCacher(interface{}, caches.Cacher) error
NewSession() *Session
NoAutoTime() *Session
Quote(string) string
SetCacher(string, caches.Cacher)
SetConnMaxLifetime(time.Duration)
SetColumnMapper(names.Mapper)
SetDefaultCacher(caches.Cacher)
SetLogger(logger interface{})
SetLogLevel(log.LogLevel)
SetMapper(names.Mapper)
SetMaxOpenConns(int)
SetMaxIdleConns(int)
SetQuotePolicy(dialects.QuotePolicy)
SetSchema(string)
SetTableMapper(names.Mapper)
SetTZDatabase(tz *time.Location)
SetTZLocation(tz *time.Location)
AddHook(hook contexts.Hook)
ShowSQL(show ...bool)
Sync(...interface{}) error
Sync2(...interface{}) error
StoreEngine(storeEngine string) *Session
TableInfo(bean interface{}) (*schemas.Table, error)
TableName(interface{}, ...bool) string
UnMapType(reflect.Type)
EnableSessionID(bool)
}
var ( var (
_ Interface = &Session{} _ Interface = &Session{}
_ EngineInterface = &Engine{}
_ EngineInterface = &EngineGroup{}
) )