forked from aphistic/golf
/
client.go
271 lines (231 loc) · 4.84 KB
/
client.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
package golf
import (
"compress/gzip"
"compress/zlib"
"errors"
"io"
"net"
"net/url"
"os"
"strings"
"sync"
"time"
)
// Compression type to use for GELF messages that are sent
const (
COMP_NONE = iota // No compression
COMP_GZIP // gzip compression
COMP_ZLIB // zlib compression
)
type Client struct {
hostname string
conn net.Conn
chnk *chunker
queue []*Message
queueMutex sync.Mutex
msgChan chan *Message
queueCtl chan int
sendCtl chan int
gz *gzip.Writer
zz *zlib.Writer
config ClientConfig
}
// Configuration used when creating a server instance
type ClientConfig struct {
ChunkSize int // The data size for each chunk sent to the server
Compression int // Compression to use for messagec.
}
/*
Create a new Client instance with the default values for ClientConfig:
{
ChunkSize: 1420,
Compression: COMP_GZIP,
}
*/
func NewClient() (*Client, error) {
cc := ClientConfig{
ChunkSize: 1420,
Compression: COMP_GZIP,
}
return NewClientWithConfig(cc)
}
// Create a new Client instance with the given ClientConfig
func NewClientWithConfig(config ClientConfig) (*Client, error) {
c := &Client{
config: config,
queue: make([]*Message, 0),
msgChan: make(chan *Message, 500),
queueCtl: make(chan int),
sendCtl: make(chan int),
}
host, err := os.Hostname()
if err != nil {
return nil, err
}
c.hostname = host
return c, nil
}
// Connect to a GELF server at the given URI.
func (c *Client) Dial(uri string) error {
parsedUri, err := url.Parse(uri)
if err != nil {
return err
}
if !strings.Contains(parsedUri.Host, ":") {
parsedUri.Host = parsedUri.Host + ":12201"
}
switch parsedUri.Scheme {
case "udp":
case "tcp":
default:
return errors.New("Unsupported scheme provided")
}
switch parsedUri.Query().Get("compress") {
case "none":
c.config.Compression = COMP_NONE
case "zlib":
c.config.Compression = COMP_ZLIB
case "gzip":
c.config.Compression = COMP_GZIP
}
conn, err := net.Dial(parsedUri.Scheme, parsedUri.Host)
if err != nil {
return err
}
c.conn = conn
c.chnk, err = newChunker(c.conn, c.config.ChunkSize)
if err != nil {
return err
}
c.gz, err = gzip.NewWriterLevel(c.chnk, gzip.DefaultCompression)
if err != nil {
return err
}
c.zz, err = zlib.NewWriterLevel(c.chnk, zlib.DefaultCompression)
if err != nil {
return err
}
go c.queueReceiver()
go c.msgSender()
return nil
}
// Close the connection to the server. This call will block until all the
// currently queued messages for the client are sent.
func (c *Client) Close() error {
if c.conn == nil {
// Already shut down so it doesn't need to run again
return nil
}
// First quit the queue and wait for it to respond
// that it's quit
c.queueCtl <- 1
for {
quitVal := <-c.queueCtl
if quitVal == 2 {
break
}
c.queueCtl <- quitVal
}
// Then quit the sender and wait for it to respond
// that it's quit
c.sendCtl <- 1
for {
quitVal := <-c.sendCtl
if quitVal == 2 {
break
}
c.sendCtl <- quitVal
}
err := c.conn.Close()
if err != nil {
return err
}
c.conn = nil
return nil
}
// Queue the given message at the end of the message queue
func (c *Client) QueueMsg(msg *Message) error {
if msg.Timestamp == nil {
curTime := time.Now()
msg.Timestamp = &curTime
}
c.msgChan <- msg
return nil
}
func (c *Client) queueReceiver() {
for {
select {
case msg := <-c.msgChan:
c.queueMutex.Lock()
c.queue = append(c.queue, msg)
c.queueMutex.Unlock()
case quitVal := <-c.queueCtl:
if quitVal == 1 {
// Don't quit if there are still
// messages in the channel
if len(c.msgChan) > 0 {
c.queueCtl <- 1
continue
}
c.queueCtl <- 2
return
}
}
}
}
func (c *Client) msgSender() {
var msg *Message
for {
c.queueMutex.Lock()
if len(c.queue) > 0 {
msg, c.queue = c.queue[0], c.queue[1:]
c.queueMutex.Unlock()
data, err := generateMsgJson(msg)
if err != nil {
// TODO Not sure what to do at this point? Fail the
// message silently?
// Might be able to add an error channel that the
// user can watch for errors
continue
}
err = c.writeMsg(data, c.conn, c.config.Compression)
if err != nil {
// TODO Same as above...
}
} else {
c.queueMutex.Unlock()
time.Sleep(1 * time.Second)
select {
case quitVal := <-c.sendCtl:
if quitVal == 1 {
c.queueMutex.Lock()
if len(c.queue) > 0 {
c.queueMutex.Unlock()
c.sendCtl <- 1
continue
}
c.queueMutex.Unlock()
c.sendCtl <- 2
return
}
default:
}
}
}
}
func (c *Client) writeMsg(data string, w io.Writer, compression int) error {
defer c.chnk.Flush()
switch compression {
case COMP_GZIP:
c.gz.Reset(c.chnk)
c.gz.Write([]byte(data))
c.gz.Flush()
case COMP_ZLIB:
c.zz.Reset(c.chnk)
c.zz.Write([]byte(data))
c.zz.Flush()
default:
c.chnk.Write([]byte(data))
}
return nil
}