forked from juneym/gor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
output_tcp.go
57 lines (44 loc) · 898 Bytes
/
output_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
package gor
import (
"fmt"
"io"
"log"
"net"
"strconv"
"strings"
)
type TCPOutput struct {
address string
limit int
}
func NewTCPOutput(options string) io.Writer {
o := new(TCPOutput)
optionsArr := strings.Split(options, "|")
o.address = optionsArr[0]
if len(optionsArr) > 1 {
o.limit, _ = strconv.Atoi(optionsArr[1])
}
if o.limit > 0 {
return NewLimiter(o, o.limit)
} else {
return o
}
}
func (o *TCPOutput) Write(data []byte) (n int, err error) {
conn, err := o.connect(o.address)
defer conn.Close()
if err != nil {
n, err = conn.Write(data)
}
return
}
func (o *TCPOutput) connect(address string) (conn net.Conn, err error) {
conn, err = net.Dial("tcp", address)
if err != nil {
log.Println("Connection error ", err, o.address)
}
return
}
func (o *TCPOutput) String() string {
return fmt.Sprintf("TCP output %s, limit: %d", o.address, o.limit)
}