/
persistence.go
81 lines (73 loc) · 2.41 KB
/
persistence.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
package config
import (
"net"
"time"
"github.com/pkg/errors"
)
type PersistenceType = string
const (
PersistenceTypeMemory PersistenceType = "memory"
PersistenceTypeRedis PersistenceType = "redis"
)
var (
defaultMaxActive = uint(0)
defaultMaxIdle = uint(1000)
// DefaultPersistenceConfig is the default value of Persistence
DefaultPersistenceConfig = Persistence{
Type: PersistenceTypeMemory,
Redis: RedisPersistence{
Addr: "127.0.0.1:6379",
Password: "",
Database: 0,
MaxIdle: &defaultMaxIdle,
MaxActive: &defaultMaxActive,
IdleTimeout: 240 * time.Second,
},
}
)
// Persistence is the config of backend persistence.
type Persistence struct {
// Type is the persistence type.
// If empty, use "memory" as default.
Type PersistenceType `yaml:"type"`
// Redis is the redis configuration and must be set when Type == "redis".
Redis RedisPersistence `yaml:"redis"`
}
// RedisPersistence is the configuration of redis persistence.
type RedisPersistence struct {
// Addr is the redis server address.
// If empty, use "127.0.0.1:6379" as default.
Addr string `yaml:"addr"`
// Password is the redis password.
Password string `yaml:"password"`
// Database is the number of the redis database to be connected.
Database uint `yaml:"database"`
// MaxIdle is the maximum number of idle connections in the pool.
// If nil, use 1000 as default.
// This value will pass to redis.Pool.MaxIde.
MaxIdle *uint `yaml:"max_idle"`
// MaxActive is the maximum number of connections allocated by the pool at a given time.
// If nil, use 0 as default.
// If zero, there is no limit on the number of connections in the pool.
// This value will pass to redis.Pool.MaxActive.
MaxActive *uint `yaml:"max_active"`
// Close connections after remaining idle for this duration. If the value
// is zero, then idle connections are not closed. Applications should set
// the timeout to a value less than the server's timeout.
// Ff zero, use 240 * time.Second as default.
// This value will pass to redis.Pool.IdleTimeout.
IdleTimeout time.Duration `yaml:"idle_timeout"`
}
func (p *Persistence) Validate() error {
if p.Type != PersistenceTypeMemory && p.Type != PersistenceTypeRedis {
return errors.New("invalid persistence type")
}
_, _, err := net.SplitHostPort(p.Redis.Addr)
if err != nil {
return err
}
if p.Redis.Database < 0 {
return errors.New("invalid redis database number")
}
return nil
}