Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ func init() {
rootCmd.Flags().Uint16VarP(&clientConfig.TargetPort, "target-port", "p", 0, "target port")
rootCmd.Flags().StringVarP(&clientConfig.Message, "message", "m", "defaultmessage", "message to send")
rootCmd.Flags().UintVarP(&clientConfig.Timeout, "timeout", "t", 2000, "timeout in ms")
rootCmd.Flags().UintVarP(&clientConfig.ReadTimeout, "read-timeout", "", 1500, "response read timeout in ms (tcp only)")
rootCmd.Flags().UintVarP(&clientConfig.Attempts, "attempts", "r", 1, "number of attempts, successful or not")
rootCmd.Flags().UintVar(&clientConfig.Period, "period", 5000, "send a new message every <period> ms")
rootCmd.Flags().UintVar(&clientConfig.SuccThrPec, "success-threshold", 80, "percentage of successful attempts needed")
Expand Down
1 change: 1 addition & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type Config struct {
TargetPort uint16
Message string
Timeout uint
ReadTimeout uint
Attempts uint
Period uint
SuccThrPec uint
Expand Down
52 changes: 36 additions & 16 deletions pkg/conntester/conntester.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"context"
"errors"
"fmt"
"io"
"net"
"strings"
"sync"
Expand All @@ -19,14 +20,15 @@

// ConnTester performs connections against a certain destination
type ConnTester struct {
protocol string
targetHost string
targetPort uint16
message string
timeout uint
attempts uint
period uint
succThrPec uint
protocol string
targetHost string
targetPort uint16
message string
timeout uint
readTimeout uint
attempts uint
period uint
succThrPec uint

logger *log.Logger

Expand All @@ -44,14 +46,15 @@
}

ct := &ConnTester{
protocol: config.Protocol,
targetHost: config.TargetHost,
targetPort: config.TargetPort,
message: config.Message,
timeout: config.Timeout,
attempts: config.Attempts,
period: config.Period,
succThrPec: config.SuccThrPec,
protocol: config.Protocol,
targetHost: config.TargetHost,
targetPort: config.TargetPort,
message: config.Message,
timeout: config.Timeout,
readTimeout: config.ReadTimeout,
attempts: config.Attempts,
period: config.Period,
succThrPec: config.SuccThrPec,
}
ct.logger = logger
return ct, nil
Expand Down Expand Up @@ -126,12 +129,29 @@
c.logger.Info(fmt.Sprintf("cannot set deadline to connection to %s: %s", dstEndpoint, err))
return
}

if _, err := conn.Write([]byte(c.message)); err != nil {
c.logger.Info(fmt.Sprintf("cannot send data to %s: %s", dstEndpoint, err))
return
}
c.logger.Info(fmt.Sprintf("successful connection and data sent to %s", dstEndpoint))

if tcpConn, ok := conn.(*net.TCPConn); ok {
if err := tcpConn.CloseWrite(); err != nil {
c.logger.Info(fmt.Sprintf("error during CloseWrite to %s: %s", dstEndpoint, err))
} else {
c.logger.Info("TCP FIN packet sent, writing side closed")
}

conn.SetReadDeadline(time.Now().Add(time.Duration(c.readTimeout) * time.Millisecond))

Check failure on line 146 in pkg/conntester/conntester.go

View workflow job for this annotation

GitHub Actions / golangci

[golangci] pkg/conntester/conntester.go#L146

Error return value of `conn.SetReadDeadline` is not checked (errcheck)
Raw output
pkg/conntester/conntester.go:146:23: Error return value of `conn.SetReadDeadline` is not checked (errcheck)
		conn.SetReadDeadline(time.Now().Add(time.Duration(c.readTimeout) * time.Millisecond))
		                    ^
1 issues:
* errcheck: 1
_, err = io.Copy(io.Discard, conn)
if err != nil && err != io.EOF {
c.logger.Info(fmt.Sprintf("stopped discarding data from %s: %s", dstEndpoint, err))
} else {
c.logger.Info("server response fully read and discarded")
}
}

if err := conn.Close(); err != nil {
c.logger.Info(fmt.Sprintf("error while closing connection to %s: %s", dstEndpoint, err))
}
Expand Down
Loading