forked from chrissnell/crabby
-
Notifications
You must be signed in to change notification settings - Fork 1
/
crabby.go
74 lines (57 loc) · 1.39 KB
/
crabby.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
package main
import (
"context"
"flag"
"fmt"
"log"
"net/http"
"os"
"os/signal"
"sync"
"syscall"
"time"
)
func main() {
var err error
var wg sync.WaitGroup
sigs := make(chan os.Signal, 1)
done := make(chan struct{}, 1)
cfgFile := flag.String("config", "config.yaml", "Path to config file (default: ./config.yaml)")
flag.Parse()
c, err := NewConfig(cfgFile)
if err != nil {
log.Fatalln("Error reading config file. Did you pass the -config flag? Run with -h for help.\n", err)
}
ctx := context.Background()
ctx, cancel := context.WithCancel(ctx)
s, err := NewStorage(ctx, &wg, c)
if err != nil {
log.Fatalln(err)
}
// If configured, start a goroutine to report internal metrics from the Go runtime
if c.ReportInternalMetrics {
go startInternalMetrics(ctx, &wg, s, c.InternalMetricsInterval)
}
tr := &http.Transport{
Proxy: http.ProxyFromEnvironment,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
// We have to disable keep-alives to keep our server connection time
// measurements accurate
DisableKeepAlives: true,
}
client := &http.Client{
Transport: tr,
}
defer tr.CloseIdleConnections()
StartJobs(ctx, &wg, c, s, client)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
go func(cancel context.CancelFunc) {
<-sigs
cancel()
close(done)
}(cancel)
<-done
wg.Wait()
fmt.Println("Exiting!")
}