-
-
Notifications
You must be signed in to change notification settings - Fork 302
/
main.go
158 lines (139 loc) · 3.17 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
152
153
154
155
156
157
158
package margo_pkg
import (
"bytes"
"encoding/base64"
"encoding/json"
"flag"
"fmt"
"io"
"log"
"os"
"runtime"
"strings"
"sync"
"time"
)
var (
byeLck = sync.Mutex{}
byeFuncs *byeFunc = nil
numbers = &counter{}
logger = log.New(os.Stderr, "margo: ", log.Ldate|log.Ltime|log.Lshortfile)
sendCh = make(chan Response, 100)
)
type counter struct {
lck sync.Mutex
n uint64
}
func (c *counter) next() uint64 {
c.lck.Lock()
defer c.lck.Unlock()
c.n += 1
return c.n
}
func (c *counter) val() uint64 {
c.lck.Lock()
defer c.lck.Unlock()
return c.n
}
func (c *counter) nextString() string {
c.lck.Lock()
defer c.lck.Unlock()
c.n += 1
return fmt.Sprint(c.n)
}
type byeFunc struct {
prev *byeFunc
f func()
}
func byeDefer(f func()) {
byeLck.Lock()
defer byeLck.Unlock()
byeFuncs = &byeFunc{
prev: byeFuncs,
f: f,
}
}
func Main() {
do := "-"
poll := 0
wait := false
dump_env := false
maxMemDefault := 1000
maxMem := 0
tag := ""
flags := flag.NewFlagSet("MarGo", flag.ExitOnError)
flags.BoolVar(&dump_env, "env", dump_env, "if true, dump all environment variables as a json map to stdout and exit")
flags.BoolVar(&wait, "wait", wait, "Whether or not to wait for outstanding requests (which may be hanging forever) when exiting")
flags.IntVar(&poll, "poll", poll, "If N is greater than zero, send a response every N seconds. The token will be `margo.poll`")
flags.StringVar(&do, "do", "-", "Process the specified operations(lines) and exit. `-` means operate as normal (`-do` implies `-wait=true`)")
flags.StringVar(&tag, "tag", tag, "Requests will include a member `tag' with this value")
flags.IntVar(&maxMem, "oom", maxMemDefault, "The maximum amount of memory MarGo is allowed to use. If memory use reaches this value, MarGo dies :'(")
flags.Parse(os.Args[1:])
// 4 is arbitrary,
runtime.GOMAXPROCS(runtime.NumCPU() + 4)
if maxMem <= 0 {
maxMem = maxMemDefault
}
startOomKiller(maxMem)
if dump_env {
m := defaultEnv()
for _, s := range os.Environ() {
p := strings.SplitN(s, "=", 2)
if len(p) == 2 {
m[p[0]] = p[1]
} else {
m[p[0]] = ""
}
}
json.NewEncoder(os.Stdout).Encode(m)
os.Exit(0)
}
var in io.Reader = os.Stdin
doCall := do != "-"
if doCall {
b64 := "base64:"
if strings.HasPrefix(do, b64) {
s, _ := base64.StdEncoding.DecodeString(do[len(b64):])
in = bytes.NewReader(s)
} else {
in = strings.NewReader(do)
}
}
broker := NewBroker(in, os.Stdout, tag)
if poll > 0 {
pollSeconds := time.Second * time.Duration(poll)
pollCounter := &counter{}
go func() {
for {
time.Sleep(pollSeconds)
broker.SendNoLog(Response{
Token: "margo.poll",
Data: M{
"time": time.Now().String(),
"seq": pollCounter.nextString(),
},
})
}
}()
}
go func() {
for r := range sendCh {
broker.SendNoLog(r)
}
}()
broker.Loop(!doCall, (wait || doCall))
byeLck.Lock()
defer byeLck.Unlock() // keep this here for the sake of code correctness
for b := byeFuncs; b != nil; b = b.prev {
func() {
defer func() {
err := recover()
if err != nil {
logger.Println("PANIC defer:", err)
}
}()
b.f()
}()
}
os.Exit(0)
}