Skip to content

Commit 1dafbf4

Browse files
committed
close tcp writer and read response
1 parent d43fdb5 commit 1dafbf4

3 files changed

Lines changed: 38 additions & 16 deletions

File tree

cmd/root.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ func init() {
9797
rootCmd.Flags().Uint16VarP(&clientConfig.TargetPort, "target-port", "p", 0, "target port")
9898
rootCmd.Flags().StringVarP(&clientConfig.Message, "message", "m", "defaultmessage", "message to send")
9999
rootCmd.Flags().UintVarP(&clientConfig.Timeout, "timeout", "t", 2000, "timeout in ms")
100+
rootCmd.Flags().UintVarP(&clientConfig.ReadTimeout, "read-timeout", "", 1500, "response read timeout in ms (tcp only)")
100101
rootCmd.Flags().UintVarP(&clientConfig.Attempts, "attempts", "r", 1, "number of attempts, successful or not")
101102
rootCmd.Flags().UintVar(&clientConfig.Period, "period", 5000, "send a new message every <period> ms")
102103
rootCmd.Flags().UintVar(&clientConfig.SuccThrPec, "success-threshold", 80, "percentage of successful attempts needed")

pkg/config/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ type Config struct {
2222
TargetPort uint16
2323
Message string
2424
Timeout uint
25+
ReadTimeout uint
2526
Attempts uint
2627
Period uint
2728
SuccThrPec uint

pkg/conntester/conntester.go

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"context"
66
"errors"
77
"fmt"
8+
"io"
89
"net"
910
"strings"
1011
"sync"
@@ -19,14 +20,15 @@ var ErrTestFailed = errors.New("test failed")
1920

2021
// ConnTester performs connections against a certain destination
2122
type ConnTester struct {
22-
protocol string
23-
targetHost string
24-
targetPort uint16
25-
message string
26-
timeout uint
27-
attempts uint
28-
period uint
29-
succThrPec uint
23+
protocol string
24+
targetHost string
25+
targetPort uint16
26+
message string
27+
timeout uint
28+
readTimeout uint
29+
attempts uint
30+
period uint
31+
succThrPec uint
3032

3133
logger *log.Logger
3234

@@ -44,14 +46,15 @@ func New(config *config.Config, logger *log.Logger) (*ConnTester, error) {
4446
}
4547

4648
ct := &ConnTester{
47-
protocol: config.Protocol,
48-
targetHost: config.TargetHost,
49-
targetPort: config.TargetPort,
50-
message: config.Message,
51-
timeout: config.Timeout,
52-
attempts: config.Attempts,
53-
period: config.Period,
54-
succThrPec: config.SuccThrPec,
49+
protocol: config.Protocol,
50+
targetHost: config.TargetHost,
51+
targetPort: config.TargetPort,
52+
message: config.Message,
53+
timeout: config.Timeout,
54+
readTimeout: config.ReadTimeout,
55+
attempts: config.Attempts,
56+
period: config.Period,
57+
succThrPec: config.SuccThrPec,
5558
}
5659
ct.logger = logger
5760
return ct, nil
@@ -126,12 +129,29 @@ func (c *ConnTester) send(ctx context.Context, wg *sync.WaitGroup) {
126129
c.logger.Info(fmt.Sprintf("cannot set deadline to connection to %s: %s", dstEndpoint, err))
127130
return
128131
}
132+
129133
if _, err := conn.Write([]byte(c.message)); err != nil {
130134
c.logger.Info(fmt.Sprintf("cannot send data to %s: %s", dstEndpoint, err))
131135
return
132136
}
133137
c.logger.Info(fmt.Sprintf("successful connection and data sent to %s", dstEndpoint))
134138

139+
if tcpConn, ok := conn.(*net.TCPConn); ok {
140+
if err := tcpConn.CloseWrite(); err != nil {
141+
c.logger.Info(fmt.Sprintf("error during CloseWrite to %s: %s", dstEndpoint, err))
142+
} else {
143+
c.logger.Info("TCP FIN packet sent, writing side closed")
144+
}
145+
146+
conn.SetReadDeadline(time.Now().Add(time.Duration(c.readTimeout) * time.Millisecond))
147+
_, err = io.Copy(io.Discard, conn)
148+
if err != nil && err != io.EOF {
149+
c.logger.Info(fmt.Sprintf("stopped discarding data from %s: %s", dstEndpoint, err))
150+
} else {
151+
c.logger.Info("server response fully read and discarded")
152+
}
153+
}
154+
135155
if err := conn.Close(); err != nil {
136156
c.logger.Info(fmt.Sprintf("error while closing connection to %s: %s", dstEndpoint, err))
137157
}

0 commit comments

Comments
 (0)