github.com/go-board/x-go@v0.1.2-0.20220610024734-db1323f6cb15/xdatabase/xsql/options.go (about) 1 package xsql 2 3 import "time" 4 5 type ConnectionOptions struct { 6 DriverName string 7 Dsn string 8 9 MaxIdleConn int 10 MaxOpenConn int 11 ConnMaxLifeTime time.Duration 12 } 13 14 func NewDefaultConnectionOptions() ConnectionOptions { 15 return ConnectionOptions{ 16 MaxIdleConn: 20, 17 MaxOpenConn: 50, 18 ConnMaxLifeTime: time.Second * 50, 19 } 20 } 21 22 // ConnectionOption is helper function to modify database pool options 23 type ConnectionOption func(options *ConnectionOptions) 24 25 func WithDriverName(name string) ConnectionOption { 26 return func(options *ConnectionOptions) { options.DriverName = name } 27 } 28 29 func WithDsn(dsn string) ConnectionOption { 30 return func(options *ConnectionOptions) { options.Dsn = dsn } 31 } 32 33 func WithMaxIdleConn(n int) ConnectionOption { 34 return func(options *ConnectionOptions) { options.MaxIdleConn = n } 35 } 36 37 func WithMaxOpenConn(n int) ConnectionOption { 38 return func(options *ConnectionOptions) { options.MaxOpenConn = n } 39 } 40 41 func WithConnMaxIdleTime(d time.Duration) ConnectionOption { 42 return func(options *ConnectionOptions) { options.ConnMaxLifeTime = d } 43 }