-
Notifications
You must be signed in to change notification settings - Fork 3
/
bbpd.go
98 lines (89 loc) · 2.95 KB
/
bbpd.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
// bbpd is a proxy daemon for Amazon's DynamoDB. See ../../README.md
package main
import (
"fmt"
"github.com/smugmug/bbpd/lib/bbpd_const"
"github.com/smugmug/bbpd/lib/bbpd_route"
conf "github.com/smugmug/godynamo/conf"
conf_file "github.com/smugmug/godynamo/conf_file"
conf_iam "github.com/smugmug/godynamo/conf_iam"
keepalive "github.com/smugmug/godynamo/keepalive"
"log"
"os"
"os/signal"
"runtime"
"syscall"
)
// handle signals. we prefer 1,3,15 and will panic on 2
func sigHandle(c <-chan os.Signal) {
for sig := range c {
if sig == syscall.SIGTERM || sig == syscall.SIGQUIT || sig == syscall.SIGHUP {
log.Printf("*** caught signal %v, stop\n", sig)
log.Printf("bbpd is in a closed state and is no longer accepting connections")
stop_err := bbpd_route.StopBBPD()
if stop_err != nil {
log.Printf("graceful shutdown not possible:%s", stop_err.Error())
}
log.Printf("bbpd exit\n")
os.Exit(0)
} else if sig == syscall.SIGINT {
log.Printf("*** caught signal %v, PANIC stop\n", sig)
panic("bbpd panic")
os.Exit(1)
} else {
log.Printf("**** caught unchecked signal %v\n", sig)
}
}
}
func main() {
runtime.GOMAXPROCS(runtime.NumCPU())
sigchan := make(chan os.Signal, 1)
signal.Notify(sigchan)
go sigHandle(sigchan)
// conf file must be read in before anything else, to initialize permissions etc
conf_file.Read()
conf.Vals.ConfLock.RLock()
if conf.Vals.Initialized == false {
panic("the conf.Vals global conf struct has not been initialized, " +
"invoke with conf_file.Read()")
} else {
log.Printf("global conf.Vals initialized")
}
// launch a background poller to keep conns to aws alive
if conf.Vals.Network.DynamoDB.KeepAlive {
log.Printf("launching background keepalive")
go keepalive.KeepAlive([]string{conf.Vals.Network.DynamoDB.URL})
}
// we must give up the lock on the conf before calling GoIAM below, or it
// will not be able to mutate the auth params
using_iam := (conf.Vals.UseIAM == true)
conf.Vals.ConfLock.RUnlock()
// the naive "fire and forget" IAM roles initializer and watcher.
if using_iam {
iam_ready_chan := make(chan bool)
go conf_iam.GoIAM(iam_ready_chan)
iam_ready := <-iam_ready_chan
if !iam_ready {
panic("iam is not ready? auth problem")
}
} else {
log.Printf("not using iam, assume credentials hardcoded in conf file")
}
log.Printf("starting bbpd...")
pid := syscall.Getpid()
e := fmt.Sprintf("induce panic with ctrl-c (kill -2 %v) or graceful termination with kill -[1,3,15] %v", pid, pid)
log.Printf(e)
ports := []int{bbpd_const.PORT, bbpd_const.PORT2}
start_bbpd_err := bbpd_route.StartBBPD(ports)
if start_bbpd_err == nil {
// all ports are in use. exit with 0 so our rc system does not
// respawn the program
log.Printf("all bbpd ports appear to be in use: exit with code 0")
os.Exit(0)
} else {
// abnormal exit - allow the rc system to try to respawn by returning
// exit code 1
log.Printf("bbpd invocation error")
log.Fatal(start_bbpd_err.Error())
}
}