-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsystem.go
215 lines (174 loc) · 3.59 KB
/
system.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package cypress
import (
"fmt"
"net"
"os"
"strconv"
"time"
)
// The path on this system that the agent listens
const DefaultSocketPath = "/var/lib/cypress.sock"
var cMaxBuffer = 100
const cDefaultBacklog = 100
// A simple interface used to represent a system logger
type Logger interface {
Write(m *Message) error
Close() error
}
type localConn struct {
path string
conn net.Conn
connected bool
feeder chan *Message
shutdown chan struct{}
done chan struct{}
buffer []*Message
enc *Encoder
}
// Connect to an agent on a path and return a Logger
func ConnectTo(path string) Logger {
_, err := os.Stat(path)
if err != nil {
panic(fmt.Errorf("log path is not available: %s", err))
}
backlog := cDefaultBacklog
str := os.Getenv("LOG_BACKLOG")
if str != "" {
if i, err := strconv.Atoi(str); err == nil {
backlog = i
}
}
l := &localConn{
path: path,
feeder: make(chan *Message, backlog),
shutdown: make(chan struct{}),
done: make(chan struct{}),
}
go l.process()
return l
}
// Connect to the default system logger
func Connect() Logger {
return ConnectTo(LogPath())
}
// Return the default log path. The environment variable LOG_PATH is used
// if set, otherwise DefaultSocketPath.
func LogPath() string {
path := os.Getenv("LOG_PATH")
if path != "" {
return path
}
return DefaultSocketPath
}
// Buffer m until the logger returns
func (l *localConn) save(m *Message) {
if len(l.buffer) >= cMaxBuffer {
l.buffer = append([]*Message{m}, l.buffer[:cMaxBuffer-1]...)
} else {
l.buffer = append(l.buffer, m)
}
}
// Send Message to the logger
func (l *localConn) send(m *Message) error {
_, err := l.enc.Encode(m)
return err
}
// Write out any buffered messages to the logger
func (l *localConn) flush() {
if !l.connected {
conn, err := net.Dial("unix", l.path)
if err != nil {
return
}
l.conn = conn
l.connected = true
l.enc = NewEncoder(conn)
}
for len(l.buffer) > 0 {
m := l.buffer[0]
err := l.send(m)
if err == nil {
l.buffer = l.buffer[1:]
} else {
l.conn.Close()
l.connected = false
break
}
}
}
const cMaxTries = 10
// Write out any buffered messages to the logger before exitting
func (l *localConn) finalFlush() {
tries := 0
start:
tries++
if !l.connected {
conn, err := net.Dial("unix", l.path)
if err != nil {
if tries == cMaxTries {
fmt.Fprintf(os.Stderr, "Unable to connect to local logger to flush\n")
return
}
time.Sleep(1 * time.Second)
goto start
}
l.conn = conn
l.connected = true
l.enc = NewEncoder(conn)
}
for len(l.buffer) > 0 {
m := l.buffer[0]
err := l.send(m)
if err == nil {
l.buffer = l.buffer[1:]
} else {
l.conn.Close()
l.connected = false
time.Sleep(1 * time.Second)
goto start
}
}
}
// Work loop of the local connection. Reads messages, sends them to the
// logger, flushes the buffers, etc.
func (l *localConn) process() {
tick := time.NewTicker(1 * time.Second)
for {
select {
case m := <-l.feeder:
// fast case
if l.connected && l.send(m) == nil {
continue
}
l.save(m)
l.flush()
case <-tick.C:
l.flush()
case <-l.shutdown:
// drain any messages in feeder
outside:
for {
select {
case m := <-l.feeder:
l.save(m)
default:
break outside
}
}
l.finalFlush()
l.done <- struct{}{}
return
}
} // for(ever)
}
// Close the background proces goroutine
func (l *localConn) Close() error {
l.shutdown <- struct{}{}
<-l.done
return nil
}
// Write the Message to the Logger
func (l *localConn) Write(m *Message) error {
l.feeder <- m
return nil
}