-
Notifications
You must be signed in to change notification settings - Fork 0
/
environment.go
134 lines (107 loc) · 2.94 KB
/
environment.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package redis
import (
"fmt"
"net"
"os"
"strconv"
"strings"
"time"
)
// ENV return configuration from env variables
func ENV(prefix string) *Configuration {
config := &Configuration{
Timeout: CommonTimeout(prefix),
ConnectTimeout: ConnectionTimeout(prefix),
ReadTimeout: ReadTimeout(prefix),
WriteTimeout: WriteTimeout(prefix),
MasterName: MasterName(prefix),
SentinelAddresses: SentinelAddresses(prefix),
Password: os.Getenv(fmt.Sprintf("%s_REDIS_PASSWORD", prefix)),
Database: Database(prefix),
}
// TODO: move it to Dialer
if len(config.SentinelAddresses) > 0 {
config.Sentinel = NewSentinel(config)
return config
}
config.address = os.Getenv(fmt.Sprintf("%s_REDIS_ADDRESS", prefix))
if config.address == "" {
host := os.Getenv(fmt.Sprintf("%s_REDIS_SERVICE_HOST", prefix))
if host == "" {
host = "localhost"
}
port := os.Getenv(fmt.Sprintf("%s_REDIS_SERVICE_PORT", prefix))
if port == "" {
port = "6379"
}
config.address = net.JoinHostPort(host, port)
}
return config
}
func SentinelAddresses(prefix string) []string {
addresses := os.Getenv(fmt.Sprintf("%s_REDIS_SENTINEL_ADDRESSES", prefix))
if addresses == "" {
return nil
}
return strings.Split(addresses, ",")
}
func MasterName(prefix string) string {
master := os.Getenv(fmt.Sprintf("%s_REDIS_SENTINEL_MASTER_NAME", prefix))
if master == "" {
master = os.Getenv("REDIS_SENTINEL_MASTER_NAME")
}
if master == "" {
return "mymaster"
}
return master
}
func Database(prefix string) int {
if database, err := strconv.Atoi(os.Getenv(fmt.Sprintf("%s_REDIS_DATABASE", prefix))); err == nil {
return database
}
return 0
}
// CommonTimeout общий таймаут
func CommonTimeout(prefix string) time.Duration {
value := os.Getenv(fmt.Sprintf("%s_REDIS_TIMEOUT", prefix))
if value == "" {
value = os.Getenv("REDIS_TIMEOUT")
}
if timeout, err := time.ParseDuration(value); err == nil {
return timeout
}
return 0
}
// ConnectionTimeout таймаут на подключение
func ConnectionTimeout(prefix string) time.Duration {
value := os.Getenv(fmt.Sprintf("%s_REDIS_CONNECT_TIMEOUT", prefix))
if value == "" {
value = os.Getenv("REDIS_CONNECT_TIMEOUT")
}
if timeout, err := time.ParseDuration(value); err == nil {
return timeout
}
return 0
}
// ReadTimeout таймаут на чтение
func ReadTimeout(prefix string) time.Duration {
value := os.Getenv(fmt.Sprintf("%s_REDIS_READ_TIMEOUT", prefix))
if value == "" {
value = os.Getenv("REDIS_READ_TIMEOUT")
}
if timeout, err := time.ParseDuration(value); err == nil {
return timeout
}
return 0
}
// WriteTimeout таймаут на запись
func WriteTimeout(prefix string) time.Duration {
value := os.Getenv(fmt.Sprintf("%s_REDIS_WRITE_TIMEOUT", prefix))
if value == "" {
value = os.Getenv("REDIS_WRITE_TIMEOUT")
}
if timeout, err := time.ParseDuration(value); err == nil {
return timeout
}
return 0
}