-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
66 lines (53 loc) · 1.05 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
package main
import (
"fmt"
amqp "github.com/rabbitmq/amqp091-go"
"instabug/go/api"
"instabug/go/util"
"log"
"math"
"os"
"time"
)
func main() {
config, err := util.LoadConfig(".")
if err != nil {
log.Fatal("can not load config", err)
}
connection, err := connect(config.RabbitMqURL)
if err != nil {
log.Println(err)
os.Exit(1)
}
server := api.NewServer(connection)
err = server.Start(config.ServerAddress)
if err != nil {
log.Fatal("Can not start server", err)
}
}
func connect(url string) (*amqp.Connection, error) {
var counts int64
var backOff = 1 * time.Second
var connection *amqp.Connection
for {
c, err := amqp.Dial(url)
if err != nil {
log.Println(url)
fmt.Println("RabbitMQ not yet ready...")
counts++
} else {
connection = c
break
}
if counts > 5 {
fmt.Println(err)
return nil, err
}
backOff = time.Duration(math.Pow(float64(counts), 2)) * time.Second
log.Println("Backing off ...")
time.Sleep(backOff)
continue
}
log.Println("Connected to RabbitMQ")
return connection, nil
}