This repository has been archived by the owner on Feb 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 54
/
daemon.go
142 lines (130 loc) · 2.81 KB
/
daemon.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
package main
import (
"github.com/limetext/log4go"
"flag"
"github.com/quarnster/completion/clang"
"github.com/quarnster/completion/content"
"github.com/quarnster/completion/java"
cnet "github.com/quarnster/completion/net"
"github.com/robmerrell/comandante"
"net"
"net/rpc"
"net/rpc/jsonrpc"
"os"
"os/signal"
"runtime/debug"
"time"
)
var (
port = "/tmp/completion.rpc"
proto = "unix"
rmFile = false
)
func init() {
daemon := comandante.NewCommand("daemon", "Run rpc daemon", runRpcDaemon)
daemon.FlagInit = daemonFlagInit
bin.RegisterCommand(daemon)
}
func daemonFlagInit(fs *flag.FlagSet) {
fs.StringVar(&port, "port", port, "TCP port the server will listen on")
fs.StringVar(&proto, "proto", proto, "Network protocol type (tcp, udp, unix, see http://golang.org/pkg/net/)")
fs.BoolVar(&rmFile, "remove", rmFile, "Remove unix socket file if it already exists (when using unix protocol)")
}
type Daemon struct {
quit bool
l net.Listener
server *rpc.Server
}
func (d *Daemon) init() error {
d.quit = false
ifs := []interface{}{
&content.Session{},
&clang.Clang{},
&java.Java{},
&cnet.Net{},
}
d.server = rpc.NewServer()
for _, i := range ifs {
if err := d.server.Register(i); err != nil {
return err
}
}
if proto == "unix" && rmFile {
os.Remove(port)
}
var err error
d.l, err = net.Listen(proto, port)
if err != nil {
return err
}
return nil
}
func (d *Daemon) close() error {
d.quit = true
return d.l.Close()
}
func runRpcDaemon() error {
var (
d Daemon
err error
)
if err = d.init(); err != nil {
return err
}
defer d.close()
return d.serverloop()
}
func (d *Daemon) handleConn(conn net.Conn) {
s := time.Now()
conn.SetDeadline(time.Time{})
codec := jsonrpc.NewServerCodec(conn)
defer func() {
codec.Close()
if r := recover(); r != nil {
log4go.Error("Recovered from panic: %v, stack: %s", r, string(debug.Stack()))
}
log4go.Debug("Serviced in %f milliseconds", time.Since(s).Seconds()*1000)
}()
for {
if err := d.server.ServeRequest(codec); err != nil {
log4go.Error("Error handling request: %v", err)
break
}
}
}
func (d *Daemon) serverloop() error {
errorcount := 0
sigchan := make(chan os.Signal)
conchan := make(chan net.Conn)
errchan := make(chan error)
signal.Notify(sigchan, os.Interrupt, os.Kill)
go func() {
for {
if conn, err := d.l.Accept(); err != nil {
if d.quit {
return
}
errchan <- log4go.Error("Error accepting connection: %s", err)
} else {
conchan <- conn
}
}
}()
outer:
for {
select {
case s := <-sigchan:
log4go.Debug("Exiting due to signal: %s", s)
d.quit = true
break outer
case conn := <-conchan:
go d.handleConn(conn)
case <-errchan:
errorcount++
if errorcount > 10 {
return log4go.Error("Too many errors, shutting server down")
}
}
}
return nil
}