-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnection.go
39 lines (35 loc) · 992 Bytes
/
connection.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
package logstash_logger
import (
"fmt"
"log"
"net"
"time"
)
func (l *Logstash) initLoggerConnection() {
logstashAddr := fmt.Sprintf("%s:%d", l.host, l.port)
if l.connectionType == "udp" {
netConnTemp, err := net.Dial("udp", logstashAddr)
if err != nil {
log.Printf("[Logstash Logger] Failed to connect to Logstash: %s\n", err)
return
}
l.connection = netConnTemp
} else if l.connectionType == "tcp" {
netConnTemp, err := net.Dial("tcp", logstashAddr)
if err != nil {
log.Printf("[Logstash Logger] Failed to connect to Logstash: %s\n", err)
return
}
l.connection = netConnTemp
} else {
log.Println("[Logstash Logger] Invalid 'ConnectionType' provided. Please provide one of the following: tcp, udp")
return
}
l.setTimeout()
}
func (l *Logstash) setTimeout() {
deadline := time.Now().Add(time.Duration(l.timeout) * time.Second)
l.connection.SetDeadline(deadline)
l.connection.SetWriteDeadline(deadline)
l.connection.SetReadDeadline(deadline)
}