This repository has been archived by the owner on Dec 5, 2023. It is now read-only.
forked from rade/dummi
-
Notifications
You must be signed in to change notification settings - Fork 29
/
main.go
158 lines (143 loc) · 3.38 KB
/
main.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 main
import (
"bufio"
"flag"
"fmt"
"net"
"net/http"
"os"
"strings"
"time"
log "github.com/Sirupsen/logrus"
"github.com/prometheus/client_golang/prometheus"
)
var (
connectionFailedCounter = prometheus.NewCounter(prometheus.CounterOpts{
Name: "num_failed_connections",
Help: "Number of failed connections.",
})
dataRecievedCounter = prometheus.NewCounter(prometheus.CounterOpts{
Name: "num_msgs_recieved",
Help: "Number of messages recieved.",
})
dataSentCounter = prometheus.NewCounter(prometheus.CounterOpts{
Name: "num_msgs_sent",
Help: "Number of messages sent.",
})
)
type config struct {
timeout time.Duration
retryInterval time.Duration
sendInterval time.Duration
}
func main() {
log.SetOutput(os.Stderr)
prometheus.MustRegister(connectionFailedCounter)
prometheus.MustRegister(dataRecievedCounter)
prometheus.MustRegister(dataSentCounter)
var (
listen bool
laddr string
c config
)
flag.BoolVar(&listen, "l", false, "listen")
flag.StringVar(&laddr, "a", "", "listening address")
flag.DurationVar(&c.timeout, "t", 5*time.Second, "connect/send/recv timeout")
flag.DurationVar(&c.retryInterval, "r", 1*time.Second, "connection retry interval")
flag.DurationVar(&c.sendInterval, "i", 2*time.Second, "message sending interval")
flag.Parse()
addrs := flag.Args()
if listen {
laddr = normalizeAddr(laddr)
log.Infof("listening on %s", laddr)
ln, err := net.Listen("tcp", laddr)
if err != nil {
log.Fatal(err)
}
go accept(ln, c)
}
for _, addr := range addrs {
go send(normalizeAddr(addr), c)
}
http.Handle("/metrics", prometheus.Handler())
log.Fatal(http.ListenAndServe(":8081", nil))
}
func normalizeAddr(addr string) string {
parts := strings.Split(addr, ":")
if len(parts) == 1 {
return addr + ":8080"
}
return addr
}
func accept(ln net.Listener, c config) {
for {
conn, err := ln.Accept()
if err != nil {
log.Errorf("accept failed: %s", err)
connectionFailedCounter.Inc()
continue
}
go recv(conn, c)
}
}
func recv(conn net.Conn, c config) {
log.Infof("accepted connection from %s", remoteAddr(conn))
r := bufio.NewReader(conn)
for {
conn.SetDeadline(time.Now().Add(c.timeout))
s, err := r.ReadString('\n')
parts := strings.Split(s, "\n")
msg := parts[0]
if err != nil {
log.Errorln(err)
break
}
if _, err := conn.Write([]byte("\n")); err != nil {
log.Errorln(err)
break
}
fmt.Printf("%s <-- %s\n", msg, remoteAddr(conn))
dataRecievedCounter.Inc()
}
conn.Close()
}
func send(addr string, c config) {
i := 1
for {
conn, err := net.DialTimeout("tcp", addr, c.timeout)
if err != nil {
log.Errorln(err)
time.Sleep(c.retryInterval)
continue
}
log.Infof("connected to %s", remoteAddr(conn))
r := bufio.NewReader(conn)
for {
conn.SetDeadline(time.Now().Add(c.timeout))
msg := fmt.Sprintf("%d", i)
if _, err := conn.Write([]byte(msg + "\n")); err != nil {
log.Errorln(err)
break
}
_, err := r.ReadString('\n')
if err != nil {
log.Errorln(err)
break
}
fmt.Printf("%s --> %s\n", msg, remoteAddr(conn))
i++
time.Sleep(c.sendInterval)
dataSentCounter.Inc()
}
conn.Close()
}
}
func remoteAddr(conn net.Conn) string {
ip := conn.RemoteAddr().(*net.TCPAddr).IP.String()
names, err := net.LookupAddr(ip)
if err == nil && len(names) > 0 {
parts := strings.Split(names[0], ".")
return parts[0] + " (" + ip + ")"
}
return ip
}