-
Notifications
You must be signed in to change notification settings - Fork 0
/
tsddrain.go
146 lines (124 loc) · 2.98 KB
/
tsddrain.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
package main
import (
"bufio"
"flag"
"fmt"
"io"
"log"
"net"
"os"
"strings"
"time"
)
const (
versionString = "tsddrain v1.0\n"
)
var (
tHost = flag.String("b", "localhost:4242", "bind host")
outFile = flag.String("o", "", "output file")
flushInterval = flag.Duration("f", time.Second*2, "flush interval")
)
type ServerStat struct {
events int64
bytes int64
unknown int64
conns int
open int
}
func main() {
flag.Parse()
stats := ServerStat{}
msgChan := make(chan string, 16)
if *outFile != "" && *outFile != "-" {
go writeFile(msgChan)
}
s, err := net.Listen("tcp", *tHost)
if err != nil {
log.Fatalln("Couldn't start server", err)
}
for {
conn, err := s.Accept()
if err != nil {
log.Println("Failed to accept connection", err)
continue
}
log.Println("Accepted new connection from", conn.RemoteAddr().String())
go func() {
defer func() {
stats.open -= 1
conn.Close()
}()
stats.conns++
stats.open++
reader := bufio.NewReader(conn)
writer := bufio.NewWriter(conn)
for {
// extending the deadline on every loop pass seems to be idiomatic, but expensive
//conn.SetReadDeadline(time.Now().Add(time.Duration(6000 * time.Millisecond)))
event, err := reader.ReadString('\n')
if err != nil {
if err != io.EOF {
log.Printf("Unexpected error from %s: %s\n", conn.RemoteAddr().String(), err)
}
break
}
event = strings.TrimSpace(event)
// skip empty lines
if len(event) == 0 {
continue
}
switch {
case strings.HasPrefix(event, "put "):
stats.events++
stats.bytes += int64(len(event))
if *outFile != "" {
if *outFile == "-" {
log.Print(event)
} else {
msgChan <- event
}
}
case strings.HasPrefix(event, "version"):
writer.WriteString(versionString)
writer.Flush()
case strings.HasPrefix(event, "stats"):
t := time.Now().Unix()
fmt.Fprintf(writer, "tsdsink.events %d %d\n", t, stats.events)
fmt.Fprintf(writer, "tsdsink.bytes %d %d\n", t, stats.bytes)
fmt.Fprintf(writer, "tsdsink.connections %d %d\n", t, stats.conns)
fmt.Fprintf(writer, "tsdsink.open %d %d\n", t, stats.open)
fmt.Fprintf(writer, "tsdsink.unknown %d %d\n", t, stats.unknown)
writer.Flush()
case strings.HasPrefix(event, "reset"):
log.Println("Resetting stats")
stats = ServerStat{}
default:
log.Println("Unknown command", event)
stats.unknown++
}
}
}()
}
log.Println("Listening on %s for connections", tHost)
select {}
}
func writeFile(msgChan chan string) {
f, err := os.OpenFile(*outFile, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0660)
if err != nil {
log.Fatalln("Error opening file:", err)
}
defer f.Close()
w := bufio.NewWriter(f)
ticker := time.Tick(*flushInterval)
for {
select {
case event := <-msgChan:
_, err = fmt.Fprint(w, event)
if err != nil {
log.Println("Error writing to file", err)
}
case <-ticker:
w.Flush()
}
}
}