-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
243 lines (189 loc) · 5.36 KB
/
main.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
package main
import (
"bytes"
"fmt"
"io"
"log"
"net"
"os"
"path/filepath"
"strings"
"sync"
"sync/atomic"
"time"
systemdnotify "github.com/iguanesolutions/go-systemd/v5/notify"
"gopkg.in/yaml.v2"
)
type RedisPort struct {
mutex sync.RWMutex
masterAddr *net.TCPAddr
port string
}
type Stats struct {
connectionsProxied uint64
pipesActive uint32
}
type ConfigStruct struct {
Ports []string `yaml:"ports"`
Nodes []string `yaml:"nodes"`
Auth string `yaml:"auth"`
ProxyConnectionTimeout int `yaml:"proxy_connection_timeout"`
}
var (
config ConfigStruct = ConfigStruct{
ProxyConnectionTimeout: 10,
}
globalStats Stats
)
func main() {
if systemdnotify.IsEnabled() {
log.SetFlags(log.Flags() &^ (log.Ldate | log.Ltime))
}
if len(os.Args) != 2 {
log.Fatalln("A single parameter is expected: config file name")
}
fn, err := filepath.Abs(os.Args[1])
if err != nil {
log.Fatalf("Can't get config file absolute path: %s\n", err)
}
log.Printf("Using onfiguration file %s\n", fn)
f, err := os.Open(fn)
if err != nil {
log.Fatalf("Can't open config file: %s\n", err)
}
err = yaml.NewDecoder(f).Decode(&config)
if err != nil {
log.Fatalf("Can't read config: %s\n", err)
}
f.Close()
if len(config.Ports) < 1 {
log.Fatalln("Must specify at least one listening port!")
}
if len(config.Nodes) < 1 {
log.Fatalln("Must specify at least one redis node!")
}
log.Printf("Watching the following redis servers: %s", strings.Join(config.Nodes, ", "))
log.Printf("Serving the following ports: %s", strings.Join(config.Ports, ", "))
for _, port := range config.Ports {
go ServePort(port)
}
if err := systemdnotify.Ready(); err != nil {
log.Printf("Failed to notify ready to systemd: %v\n", err)
}
ratePeriodStart := time.Now()
rateProxiedValue := globalStats.connectionsProxied
// just update systemd status time to time
for {
time.Sleep(time.Second * 5)
var delta float64
var rateProxied float64
delta, ratePeriodStart = time.Since(ratePeriodStart).Seconds(), time.Now()
rateProxied, rateProxiedValue = float64(globalStats.connectionsProxied-rateProxiedValue)/delta, globalStats.connectionsProxied
statusString := fmt.Sprintf("Active connections: %d, proxied: %d, rate: %.1f/sec",
globalStats.pipesActive/2,
globalStats.connectionsProxied,
rateProxied)
if systemdnotify.IsEnabled() {
systemdnotify.Status(statusString)
} else {
log.Println(statusString)
}
}
}
func ServePort(port string) {
var p RedisPort
p.port = port
laddr, err := net.ResolveTCPAddr("tcp", ":"+port)
if err != nil {
log.Fatalf("Failed to resolve listen address: %s\n", err)
}
go followMaster(&p)
listener, err := net.ListenTCP("tcp", laddr)
if err != nil {
log.Fatalf("Can't open listening socket for port %s: %s\n", port, err)
}
for {
conn, err := listener.AcceptTCP()
if err != nil {
log.Printf("Can't accept connection on port %s: %s\n", port, err)
continue
}
p.mutex.RLock()
if p.masterAddr != nil {
atomic.AddUint64(&globalStats.connectionsProxied, 1)
go proxy(conn, p.masterAddr)
} else {
conn.Close()
}
p.mutex.RUnlock()
}
}
func followMaster(rp *RedisPort) {
for {
var newAddr *net.TCPAddr
for attempt := 1; newAddr == nil && attempt <= 3; attempt++ {
newAddr = getMasterAddr(rp.port, attempt)
}
if newAddr == nil {
log.Printf("No masters found for port %s! Will not serve new connections until master is found...", rp.port)
} else if rp.masterAddr == nil || string(rp.masterAddr.IP) != string(newAddr.IP) || rp.masterAddr.Port != newAddr.Port {
log.Printf("Changing master to %s:%d\n", newAddr.IP, newAddr.Port)
}
rp.mutex.Lock()
rp.masterAddr = newAddr
rp.mutex.Unlock()
time.Sleep(1 * time.Second)
}
}
func proxy(local io.ReadWriteCloser, remoteAddr *net.TCPAddr) {
d := net.Dialer{Timeout: time.Duration(config.ProxyConnectionTimeout) * time.Second}
remote, err := d.Dial("tcp", remoteAddr.String())
if err != nil {
log.Println(err)
local.Close()
return
}
local.(*net.TCPConn).SetKeepAlive(true)
local.(*net.TCPConn).SetKeepAlivePeriod(5 * time.Second)
remote.(*net.TCPConn).SetKeepAlive(true)
remote.(*net.TCPConn).SetKeepAlivePeriod(5 * time.Second)
go pipe(local, remote)
go pipe(remote, local)
}
func pipe(r io.ReadCloser, w io.WriteCloser) {
atomic.AddUint32(&globalStats.pipesActive, 1) // increase by 1
defer atomic.AddUint32(&globalStats.pipesActive, ^uint32(0)) // decrease by 1
defer r.Close()
defer w.Close()
io.Copy(w, r)
}
func getMasterAddr(port string, timeout int) *net.TCPAddr {
for _, node := range config.Nodes {
d := net.Dialer{Timeout: time.Duration(timeout) * time.Second}
conn, err := d.Dial("tcp", node+":"+port)
if err != nil {
if timeout != 1 {
log.Printf("Can't connect to %s with timeout %ds: %s\n", node, timeout, err)
}
continue
}
defer conn.Close()
if config.Auth != "" {
conn.Write([]byte(fmt.Sprintf("AUTH %s\r\ninfo replication\r\n", config.Auth)))
} else {
conn.Write([]byte("info replication\r\n"))
}
b := make([]byte, 4096)
l, err := conn.Read(b)
if err != nil {
log.Printf("Can't read Redis response: %s\n", err)
}
if bytes.Contains(b[:l], []byte("role:master")) {
return conn.RemoteAddr().(*net.TCPAddr)
}
if bytes.Contains(b[:l], []byte("-NOAUTH")) {
log.Printf("%s:%s: NOAUTH Authentication required\n", node, port)
}
}
return nil
}