88
99 "github.com/hamba/logger/v2"
1010 lctx "github.com/hamba/logger/v2/ctx"
11+ "github.com/nitrado/connqc/tcp"
12+ "github.com/nitrado/connqc/udp"
1113)
1214
1315// Client attempts to hold a connection with a server, sending probe messages at a configured interval.
@@ -33,11 +35,27 @@ func NewClient(backoff, sendInterval, readTimeout, writeTimeout time.Duration, l
3335
3436// Run sends probe messages to the server continuously.
3537// If the connection fails, it retries at the configured backoff interval.
36- func (c * Client ) Run (ctx context.Context , addr string ) {
38+ func (c * Client ) Run (ctx context.Context , protocol , addr string ) {
39+ var (
40+ conn net.Conn
41+ err error
42+ idx int
43+ )
3744 for {
38- conn , err := net .Dial ("tcp" , addr )
45+ log := c .log .With (lctx .Str ("protocol" , protocol ), lctx .Str ("addr" , addr ), lctx .Int ("reconnect" , idx ))
46+ idx ++
47+
48+ switch protocol {
49+ case "tcp" :
50+ conn , err = tcp .Connect (addr )
51+ case "udp" :
52+ conn , err = udp .Connect (addr )
53+ default :
54+ log .Error ("Unexpected protocol" )
55+ return
56+ }
3957 if err != nil {
40- c . log .Error ("Could not connect to server" , lctx .Str ( "address" , addr ))
58+ log .Error ("Could not connect to server" , lctx .Err ( err ))
4159
4260 select {
4361 case <- ctx .Done ():
@@ -48,7 +66,7 @@ func (c *Client) Run(ctx context.Context, addr string) {
4866 }
4967
5068 if err = c .handleConn (ctx , conn ); err != nil {
51- c . log .Error ("Connection error" , lctx .Error ( "error" , err ))
69+ log .Error ("Connection error" , lctx .Err ( err ))
5270 }
5371
5472 select {
@@ -64,7 +82,7 @@ type expectation struct {
6482 probe Probe
6583}
6684
67- func (c * Client ) handleConn (ctx context.Context , conn net.Conn ) error { //nolint:funlen
85+ func (c * Client ) handleConn (ctx context.Context , conn net.Conn ) error { //nolint:funlen // Simplify readability.
6886 defer func () { _ = conn .Close () }()
6987
7088 readCh := make (chan readResponse )
@@ -86,10 +104,10 @@ func (c *Client) handleConn(ctx context.Context, conn net.Conn) error { //nolint
86104 Data : fmt .Sprintf ("Hello %d" , id ),
87105 }
88106 if err := enc .Encode (p ); err != nil {
89- return fmt .Errorf ("writing Message : %w" , err )
107+ return fmt .Errorf ("writing message : %w" , err )
90108 }
91109
92- c .log .Debug ( "Sent probe " , lctx .Interface ("probe" , p ))
110+ c .log .Info ( "Message sent " , lctx .Interface ("probe" , p ))
93111
94112 id ++
95113 expect = append (expect , expectation {timestamp : time .Now (), probe : p })
@@ -98,7 +116,7 @@ func (c *Client) handleConn(ctx context.Context, conn net.Conn) error { //nolint
98116 return nil
99117 }
100118 if resp .err != nil {
101- return fmt .Errorf ("reading Message : %w" , resp .err )
119+ return fmt .Errorf ("reading response : %w" , resp .err )
102120 }
103121
104122 var (
@@ -115,7 +133,12 @@ func (c *Client) handleConn(ctx context.Context, conn net.Conn) error { //nolint
115133 break
116134 }
117135
118- c .log .Warn ("Message lost" , lctx .Uint64 ("id" , exp .probe .ID ), lctx .Str ("data" , exp .probe .Data ))
136+ c .log .Warn ("Message dropped" ,
137+ lctx .Str ("error" , "unexpected ID" ),
138+ lctx .Uint64 ("expected_id" , resp .probe .ID ),
139+ lctx .Uint64 ("id" , exp .probe .ID ),
140+ lctx .Str ("data" , exp .probe .Data ),
141+ )
119142 }
120143 if ! found {
121144 c .log .Error ("No expectation found" )
@@ -152,12 +175,10 @@ func (c *Client) readLoop(conn net.Conn, ch chan readResponse) {
152175
153176 p , ok := msg .(Probe )
154177 if ! ok {
155- ch <- readResponse {err : err }
178+ ch <- readResponse {err : fmt . Errorf ( "message not a probe: %T" , msg ) }
156179 continue
157180 }
158181
159- c .log .Debug ("Received probe" , lctx .Interface ("probe" , p ))
160-
161182 ch <- readResponse {timestamp : time .Now (), probe : p }
162183 }
163184}
0 commit comments