-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathdrain.go
More file actions
50 lines (45 loc) · 1.03 KB
/
drain.go
File metadata and controls
50 lines (45 loc) · 1.03 KB
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
package udp
import (
"fmt"
"net"
"net/url"
"sync"
)
const maxConnUses = 100
type logDrain struct {
uri string
conn *net.Conn
useCount int
mutex sync.RWMutex
}
// NewDrain returns a pointer to a new instance of a UDP-based drain.LogDrain.
func NewDrain(drainURL string) (*logDrain, error) {
u, err := url.Parse(drainURL)
if err != nil {
return nil, err
}
if u.Scheme != "udp" && u.Scheme != "syslog" {
return nil, fmt.Errorf("Invalid drain url scheme: %s", u.Scheme)
}
return &logDrain{uri: u.Host + u.Path}, nil
}
// Send forwards the provided log message to an external destination using UDP for transport.
func (d *logDrain) Send(message string) error {
d.mutex.Lock()
defer d.mutex.Unlock()
if d.useCount == maxConnUses {
(*d.conn).Close()
d.conn = nil
d.useCount = 0
}
if d.conn == nil {
conn, err := net.Dial("udp", d.uri)
if err != nil {
return fmt.Errorf("Error dialing log drain at %s over udp: %s", d.uri, err)
}
d.conn = &conn
}
fmt.Fprintln(*d.conn, message)
d.useCount++
return nil
}