-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
200 lines (166 loc) · 4.82 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
package main
import (
"github.com/johto/notifyutils/notifydispatcher"
"github.com/lib/pq"
"fmt"
"github.com/prometheus/client_golang/prometheus"
"os"
"sync"
"time"
)
// Implements a wrapper for pq.Listener for use between the PostgreSQL server
// and NotifyDispatcher. Here we collect some statistics and pass the
// notifications on to the dispatcher.
type pqListenerWrapper struct {
l *pq.Listener
ch chan *pq.Notification
inputChannelSaturationRatio *prometheus.Desc
dispatcherChannelSaturationRatio *prometheus.Desc
}
func newPqListenerWrapper(l *pq.Listener) (*pqListenerWrapper, error) {
w := &pqListenerWrapper{
l: l,
ch: make(chan *pq.Notification, 4),
}
w.inputChannelSaturationRatio = prometheus.NewDesc(
"allas_input_channel_saturation_ratio",
"main notification input Go channel saturation",
nil,
nil,
)
w.dispatcherChannelSaturationRatio = prometheus.NewDesc(
"allas_dispatcher_channel_saturation_ratio",
"dispatcher notification Go channel saturation",
nil,
nil,
)
err := Config.Prometheus.RegisterMetricsCollector(w)
if err != nil {
return nil, err
}
go w.workerGoroutine()
return w, nil
}
func (w *pqListenerWrapper) Describe(ch chan<- *prometheus.Desc) {
ch <- w.inputChannelSaturationRatio
ch <- w.dispatcherChannelSaturationRatio
}
func (w *pqListenerWrapper) Collect(ch chan<- prometheus.Metric) {
inputChSaturation := float64(len(w.l.Notify)) / float64(cap(w.l.Notify))
ch <- prometheus.MustNewConstMetric(w.inputChannelSaturationRatio, prometheus.GaugeValue, inputChSaturation)
dispatcherChSaturation := float64(len(w.ch)) / float64(cap(w.ch))
ch <- prometheus.MustNewConstMetric(w.dispatcherChannelSaturationRatio, prometheus.GaugeValue, dispatcherChSaturation)
}
func (w *pqListenerWrapper) workerGoroutine() {
input := w.l.NotificationChannel()
for {
m := <-input
MetricNotificationsReceived.Inc()
w.ch <- m
}
}
func (w *pqListenerWrapper) Listen(channel string) error {
return w.l.Listen(channel)
}
func (w *pqListenerWrapper) Unlisten(channel string) error {
return w.l.Unlisten(channel)
}
func (w *pqListenerWrapper) NotificationChannel() <-chan *pq.Notification {
return w.ch
}
// runs in its own goroutine
func listenerPinger(listener *pq.Listener) {
for {
time.Sleep(60 * time.Second)
_ = listener.Ping()
}
}
func printUsage() {
fmt.Fprintf(os.Stderr, `Usage:
%s [--help] configfile
Options:
--help display this help and exit
`, os.Args[0])
}
func main() {
InitErrorLog(os.Stderr)
if len(os.Args) != 2 {
printUsage()
os.Exit(1)
} else if os.Args[1] == "--help" {
printUsage()
os.Exit(1)
}
err := readConfigFile(os.Args[1])
if err != nil {
elog.Fatalf("error while reading configuration file: %s", err)
}
if len(Config.Databases) == 0 {
elog.Fatalf("at least one database must be configured")
}
l, err := Config.Listen.Listen()
if err != nil {
elog.Fatalf("could not open listen socket: %s", err)
}
err = Config.Prometheus.Setup()
if err != nil {
elog.Fatalf("Prometheus exporter setup failed: %s", err)
}
var m sync.Mutex
var connStatusNotifier chan struct{}
listenerStateChange := func(ev pq.ListenerEventType, err error) {
switch ev {
case pq.ListenerEventConnectionAttemptFailed:
elog.Warningf("Listener: could not connect to the database: %s", err.Error())
case pq.ListenerEventDisconnected:
elog.Warningf("Listener: lost connection to the database: %s", err.Error())
m.Lock()
close(connStatusNotifier)
connStatusNotifier = nil
m.Unlock()
case pq.ListenerEventReconnected,
pq.ListenerEventConnected:
elog.Logf("Listener: connected to the database")
m.Lock()
connStatusNotifier = make(chan struct{})
m.Unlock()
}
}
// make sure pq.Listener doesn't pick up any env variables
os.Clearenv()
clientConnectionString := fmt.Sprintf("fallback_application_name=allas %s", Config.ClientConnInfo)
listener := pq.NewListener(
clientConnectionString,
250*time.Millisecond, 3*time.Second,
listenerStateChange,
)
listenerWrapper, err := newPqListenerWrapper(listener)
if err != nil {
elog.Fatalf("%s", err)
}
nd := notifydispatcher.NewNotifyDispatcher(listenerWrapper)
nd.SetBroadcastOnConnectionLoss(false)
nd.SetSlowReaderEliminationStrategy(notifydispatcher.NeglectSlowReaders)
// We don't strictly speaking need to be pinging the server; this is a
// workaround for PostgreSQL BUG #14830.
go listenerPinger(listener)
for {
c, err := l.Accept()
if err != nil {
panic(err)
}
Config.Listen.MaybeEnableKeepAlive(c)
var myConnStatusNotifier chan struct{}
m.Lock()
if connStatusNotifier == nil {
m.Unlock()
go RejectFrontendConnection(c)
continue
} else {
myConnStatusNotifier = connStatusNotifier
}
m.Unlock()
newConn := NewFrontendConnection(c, nd, myConnStatusNotifier)
go newConn.mainLoop(Config.StartupParameters, Config.Databases)
}
}