-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
72 lines (65 loc) · 1.61 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
package main
import (
"context"
"fmt"
"log"
"math/rand"
"os"
"time"
"github.com/jackc/pgx/v5"
)
func main() {
// Make sure only messages are printed.
// Timestamps and other metadata
// can be added by log processing tool outside.
log.SetFlags(0)
const retryMin = 1 * time.Second
const retryMax = 1 * time.Hour
retryAfter := retryMin
retryCount := 0
var conn *pgx.Conn
var err error
defer func() {
if conn != nil {
conn.Close(context.Background())
}
}()
brokenConnectionOutput := os.NewFile(3, "")
for {
log.Printf("Trying to connect to database. Attempt %v\n", retryCount)
conn, err = pgx.Connect(context.Background(), "")
if err != nil {
log.Println(err)
log.Printf("Next database connection attempt in %v\n", retryAfter)
time.Sleep(retryAfter)
// Exponential backoff with equal jitter
retryAfter = (1 << retryCount) * retryMin
if retryAfter > retryMax {
retryAfter = retryMax
}
retryMilis := retryAfter.Milliseconds() / 2
retryMilis = retryMilis + (rand.Int63n(1000) * retryMilis / 1000)
retryAfter = time.Duration(retryMilis) * time.Millisecond
continue
}
log.Println("Connected to database")
retryAfter = retryMin
retryCount = 0
_, err = conn.Exec(context.Background(), "LISTEN "+os.Args[1])
if err != nil {
log.Panic(err)
}
log.Println("Waiting for notifications")
for {
notification, err := conn.WaitForNotification(context.Background())
if err != nil {
conn.Close(context.Background())
conn = nil
log.Println(err)
brokenConnectionOutput.WriteString("\n")
break
}
fmt.Println(notification.Payload)
}
}
}