-
Notifications
You must be signed in to change notification settings - Fork 25
/
tcp.go
158 lines (132 loc) · 3.53 KB
/
tcp.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
package syslog
import (
"bytes"
"fmt"
"log"
"net"
"net/url"
"strconv"
"strings"
"time"
"code.cloudfoundry.org/go-loggregator/v9/rpc/loggregator_v2"
metrics "code.cloudfoundry.org/go-metric-registry"
"code.cloudfoundry.org/loggregator-agent-release/src/pkg/egress"
)
// DialFunc represents a method for creating a connection, either TCP or TLS.
type DialFunc func(addr string) (net.Conn, error)
// TCPWriter represents a syslog writer that connects over unencrypted TCP.
// This writer is not meant to be used from multiple goroutines. The same
// goroutine that calls `.Write()` should be the one that calls `.Close()`.
type TCPWriter struct {
url *url.URL
appID string
hostname string
dialFunc DialFunc
writeTimeout time.Duration
scheme string
conn net.Conn
syslogConverter *Converter
egressMetric metrics.Counter
}
// NewTCPWriter creates a new TCP syslog writer.
func NewTCPWriter(
binding *URLBinding,
netConf NetworkTimeoutConfig,
egressMetric metrics.Counter,
c *Converter,
) egress.WriteCloser {
dialer := &net.Dialer{
Timeout: netConf.DialTimeout,
KeepAlive: netConf.Keepalive,
}
df := func(addr string) (net.Conn, error) {
return dialer.Dial("tcp", addr)
}
w := &TCPWriter{
url: binding.URL,
appID: binding.AppID,
hostname: binding.Hostname,
writeTimeout: netConf.WriteTimeout,
dialFunc: df,
scheme: "syslog",
egressMetric: egressMetric,
syslogConverter: c,
}
return w
}
// Write writes an envelope to the syslog drain connection.
func (w *TCPWriter) Write(env *loggregator_v2.Envelope) error {
conn, err := w.connection()
if err != nil {
return err
}
msgs, err := w.syslogConverter.ToRFC5424(env, w.hostname)
if err != nil {
return err
}
for _, msg := range msgs {
err = conn.SetWriteDeadline(time.Now().Add(w.writeTimeout))
if err != nil {
_ = w.Close()
return err
}
_, err = conn.Write([]byte(strconv.Itoa(len(msg)) + " " + string(msg)))
if err != nil {
_ = w.Close()
return err
}
w.egressMetric.Add(1)
}
return nil
}
func (w *TCPWriter) connection() (net.Conn, error) {
if w.conn == nil {
return w.connect()
}
return w.conn, nil
}
func (w *TCPWriter) connect() (net.Conn, error) {
conn, err := w.dialFunc(w.url.Host)
if err != nil {
return nil, err
}
w.conn = conn
log.Printf("created conn to syslog drain: %s", w.url.Host)
return conn, nil
}
// Close tears down any active connections to the drain and prevents reconnect.
func (w *TCPWriter) Close() error {
if w.conn != nil {
err := w.conn.Close()
w.conn = nil
return err
}
return nil
}
func removeNulls(msg []byte) []byte {
return bytes.Replace(msg, []byte{0}, nil, -1)
}
func appendNewline(msg []byte) []byte {
if !bytes.HasSuffix(msg, []byte("\n")) {
msg = append(msg, byte('\n'))
}
return msg
}
func generateProcessID(sourceType, sourceInstance string) string {
sourceType = strings.ToUpper(sourceType)
if sourceInstance != "" {
// 128 is the max size, 3 for [] and /, truncate to fit
// source type is almost certainly very small
if len(sourceType)+len(sourceInstance)+3 > 128 {
sourceInstance = sourceInstance[:(128 - len(sourceType) - 3)]
}
tmp := make([]byte, 0, 3+len(sourceType)+len(sourceInstance))
tmp = append(tmp, '[')
tmp = append(tmp, []byte(strings.Replace(sourceType, " ", "-", -1))...)
tmp = append(tmp, '/')
tmp = append(tmp, []byte(sourceInstance)...)
tmp = append(tmp, ']')
return string(tmp)
}
return fmt.Sprintf("[%s]", sourceType)
}