forked from chihaya/chihaya
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
78 lines (66 loc) · 1.56 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
// Copyright 2013 The Chihaya Authors. All rights reserved.
// Use of this source code is governed by the BSD 2-Clause license,
// which can be found in the LICENSE file.
package main
import (
"flag"
"log"
"os"
"os/signal"
"runtime"
"runtime/pprof"
"github.com/pushrax/chihaya/config"
"github.com/pushrax/chihaya/server"
)
var (
profile bool
configPath string
)
func init() {
flag.BoolVar(&profile, "profile", false, "Generate profiling data for pprof into chihaya.cpu")
flag.StringVar(&configPath, "config", "", "The location of a valid configuration file.")
}
func main() {
flag.Parse()
runtime.GOMAXPROCS(runtime.NumCPU())
if profile {
log.Println("Running with profiling enabled")
f, err := os.Create("chihaya.cpu")
if err != nil {
log.Fatalf("Failed to create profile file: %s\n", err)
}
defer f.Close()
pprof.StartCPUProfile(f)
}
if configPath == "" {
log.Fatalf("Must specify a configuration file")
}
conf, err := config.Open(configPath)
if err != nil {
log.Fatalf("Failed to parse configuration file: %s\n", err)
}
s, err := server.New(conf)
if err != nil {
log.Fatalf("Failed to create server: %s\n", err)
}
go func() {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
<-c
if profile {
pprof.StopCPUProfile()
}
log.Println("Caught interrupt, shutting down..")
err := s.Stop()
if err != nil {
panic("Failed to shutdown cleanly")
}
log.Println("Shutdown successfully")
<-c
os.Exit(0)
}()
err = s.ListenAndServe()
if err != nil {
log.Fatalf("Failed to start server: %s\n", err)
}
}