-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
156 lines (131 loc) · 2.85 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
package main
import (
"context"
"io"
"net"
"os"
"os/signal"
"path/filepath"
"syscall"
log "github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"
)
func main() {
log.SetLevel(log.InfoLevel)
log.SetOutput(os.Stderr)
log.SetFormatter(&log.TextFormatter{
TimestampFormat: "2006-01-02T15:04:05-0700.000000",
})
app := &cli.App{
Name: "parallelefs",
Usage: "This program writes files in parallel to speed up EFS.",
Flags: []cli.Flag{
&cli.PathFlag{
Name: "socket",
Aliases: []string{"s"},
Required: true,
Usage: "path to the socket file to be created",
},
&cli.BoolFlag{
Name: "panic",
Required: false,
Usage: "Intentionally panic at the end of program for debugging",
},
&cli.BoolFlag{
Name: "debug",
Required: false,
Usage: "Enbale debug log",
},
},
Action: func(c *cli.Context) error {
socket, err := filepath.Abs(c.Path("socket"))
if err != nil {
return err
}
if c.Bool("debug") {
log.SetLevel(log.DebugLevel)
}
listen(socket)
return nil
},
After: func(c *cli.Context) error {
// Check remaining goroutines for debugging
if c.Bool("panic") {
panic("end of program")
}
return nil
},
}
if err := app.Run(os.Args); err != nil {
log.Panic(err)
}
}
func listen(socket string) {
// Ignore error
_ = os.Remove(socket)
listener, err := net.Listen("unix", socket)
if err != nil {
log.Panic(err)
}
defer listener.Close()
log.Debugf("started listening")
ctx, cancel := context.WithCancel(context.Background())
go func() {
for {
conn, err := listener.Accept()
if err != nil {
// This will be called immediately after closing the listener.
return
}
go func() {
defer conn.Close()
handleConnection(ctx, conn)
}()
}
}()
// Wait until interrupted
<-interruptionNotification()
log.Debugf("quitting")
cancel()
}
func interruptionNotification() <-chan os.Signal {
sigCh := make(chan os.Signal, 1)
signal.Notify(sigCh, os.Interrupt, syscall.SIGTERM, syscall.SIGQUIT)
return sigCh
}
func handleConnection(ctx context.Context, conn io.ReadWriter) {
sess := newSession()
defer sess.finalize()
ctx, cancel := context.WithCancel(ctx)
defer cancel()
log.Debugf("started new session")
recvLine := connReader(conn)
for {
select {
case <-ctx.Done():
return
case msg, ok := <-recvLine:
if !ok {
cancel()
continue
}
log.Debugf("received: %d bytes", len(msg))
// Empty request means the end of this session.
if len(msg) == 0 {
sess.finalize()
conn.Write([]byte("true\n"))
cancel()
continue
}
log.Infof("req: %s", string(msg))
res, err := sess.addTask(msg)
if err != nil {
log.Error(err)
}
resbs := []byte(res + "\n")
conn.Write(resbs)
log.Debugf("sent: %d bytes", len(resbs))
log.Infof("res: %s", string(resbs))
}
}
}