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
2122type 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