-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgateway_option.go
101 lines (86 loc) · 2.47 KB
/
gateway_option.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package orm
import (
"time"
"github.com/phogolabs/orm/dialect"
"github.com/phogolabs/orm/dialect/sql"
)
// Option represents a Gateway option
type Option interface {
Apply(*Gateway) error
}
// OptionFunc represents a function that can be used to set option
type OptionFunc func(*Gateway) error
// Apply applies the option
func (fn OptionFunc) Apply(gateway *Gateway) error {
return fn(gateway)
}
// WithLogger sets the logger
func WithLogger(logger dialect.Logger) Option {
fn := func(g *Gateway) error {
if driver, ok := g.engine.querier.(*sql.Driver); ok {
g.engine.querier = dialect.Log(driver, logger)
}
return nil
}
return OptionFunc(fn)
}
// WithMaxIdleConns sets the maximum number of connections in the idle
// connection pool.
//
// If MaxOpenConns is greater than 0 but less than the new MaxIdleConns,
// then the new MaxIdleConns will be reduced to match the MaxOpenConns limit.
//
// If n <= 0, no idle connections are retained.
//
// The default max idle connections is currently 2. This may change in
// a future release.
func WithMaxIdleConns(value int) Option {
fn := func(g *Gateway) error {
if driver, ok := g.engine.querier.(*sql.Driver); ok {
driver.DB().SetMaxIdleConns(value)
}
return nil
}
return OptionFunc(fn)
}
// WithMaxOpenConns sets the maximum number of open connections to the database.
//
// If MaxIdleConns is greater than 0 and the new MaxOpenConns is less than
// MaxIdleConns, then MaxIdleConns will be reduced to match the new
// MaxOpenConns limit.
//
// If n <= 0, then there is no limit on the number of open connections.
// The default is 0 (unlimited).
func WithMaxOpenConns(value int) Option {
fn := func(g *Gateway) error {
if driver, ok := g.engine.querier.(*sql.Driver); ok {
driver.DB().SetMaxOpenConns(value)
}
return nil
}
return OptionFunc(fn)
}
// WithConnMaxLifetime sets the maximum amount of time a connection may be reused.
//
// Expired connections may be closed lazily before reuse.
//
// If d <= 0, connections are reused forever.
func WithConnMaxLifetime(duration time.Duration) Option {
fn := func(g *Gateway) error {
if driver, ok := g.engine.querier.(*sql.Driver); ok {
driver.DB().SetConnMaxLifetime(duration)
}
return nil
}
return OptionFunc(fn)
}
// WithRoutine creates the gateway with a given routine
func WithRoutine(source FileSystem) Option {
fn := func(g *Gateway) error {
if err := g.engine.provider.ReadDir(source); err != nil {
return err
}
return nil
}
return OptionFunc(fn)
}