-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
main.go
151 lines (132 loc) · 3.45 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
// nedomi is a HTTP media caching server. It aims to increase performance by
// choosing chaching algorithms suitable for media files.
package main
import (
"flag"
"fmt"
"os"
"os/exec"
"path/filepath"
"runtime"
"runtime/pprof"
"strconv"
"syscall"
"time"
"github.com/ironsmile/nedomi/app"
"github.com/ironsmile/nedomi/config"
"github.com/ironsmile/nedomi/types"
)
var (
// Version will be reported if the -v flag is used
Version = "not build through make"
// BuildTime is the build time of the binary
BuildTime string
// GitHash is the commit hash from which the version is build
GitHash string
// GitTag the tag (if any) of the commit
// from which the version is build
GitTag string
// Dirty is flag telling whether th e build is dirty - build in part from uncommitted code
// setting it to 'true' means that it is.
Dirty string
)
// The following will be populated from the command line with via `flag`
var (
testConfig bool
showVersion bool
cpuprofile string
)
func init() {
flag.BoolVar(&testConfig, "t", false, "Test configuration file and exit")
flag.BoolVar(&showVersion, "v", false, "Print version information")
flag.StringVar(&cpuprofile, "cpuprofile", "", "Write cpu profile to this file")
runtime.GOMAXPROCS(runtime.NumCPU())
}
//!TODO: implement some "unit" tests for this :)
func run() int {
if cpuprofile != "" {
f, err := os.Create(cpuprofile)
if err != nil {
fmt.Fprintf(os.Stderr, "Could not create cpuprofile file: %s\n", err)
return 1
}
if err := pprof.StartCPUProfile(f); err != nil {
panic(err)
}
defer pprof.StopCPUProfile()
}
// error probably means this is not build with make
n, _ := strconv.ParseInt(BuildTime, 10, 64)
buildTime := time.Unix(n, 0)
var appVersion = types.AppVersion{
Version: Version,
GitHash: GitHash,
GitTag: GitTag,
BuildTime: buildTime,
Dirty: Dirty == "true",
}
if showVersion {
fmt.Printf("nedomi version %s\n", appVersion)
return 0
}
appInstance, err := app.New(appVersion, config.Get)
if err != nil {
fmt.Fprintf(os.Stderr, "Couldn't initialize nedomi: %s\n", err)
return 4
}
if err := upTheLimits(); err != nil {
fmt.Fprintf(os.Stderr, "Couldn't up the limits: %s\n", err)
return 6
}
if testConfig {
return 0 // still doesn't work :)
}
if err := appInstance.Run(); err != nil {
fmt.Fprintf(os.Stderr, "Nedomi exit with error: %s\n", err)
return 5
}
return 0
}
func absolutizeArgv0() error {
if filepath.IsAbs(os.Args[0]) {
return nil
}
argv0, err := exec.LookPath(os.Args[0])
if err != nil {
return err
}
argv0, err = filepath.Abs(argv0)
if err != nil {
return err
}
os.Args[0] = argv0
return nil
}
// raise limits to their hard limit
func upTheLimits() error {
// !TODO maybe raise to a configurable value
if err := upLimitToHard(syscall.RLIMIT_NOFILE); err != nil {
return err
}
// !TODO raise other limits when necessary
return nil
}
// upLimitToHard ups a resource limit identified
// by the resource argument to it's hard limit
// the returned error is either from syscall.(Get|Set)rlimit
func upLimitToHard(resource int) error {
var resLimit = new(syscall.Rlimit)
if err := syscall.Getrlimit(resource, resLimit); err != nil {
return err
}
resLimit.Cur = resLimit.Max
return syscall.Setrlimit(resource, resLimit)
}
func main() {
if err := absolutizeArgv0(); err != nil {
fmt.Fprintf(os.Stderr, "Error while absolutizing the path to the executable: %s\n", err)
os.Exit(9)
}
flag.Parse()
os.Exit(run())
}