@cryptotaxi247 / netdata-1 / commits / 452acfcb5

go.d chrony fix client read/write timeout (#18215)

Ilya Mashchenko committed Jul 22, 2024 at 22:46 UTC 452acfcb52efbd98ec303666e840f70bf34ec55c
1 file changed +29 -2
src/go/plugin/go.d/modules/chrony/client.go
+29 -2
@@ -5,6 +5,7 @@ package chrony
5 import (
6 "fmt"
7 "net"
8 + "time"
9
10 "github.com/facebook/time/ntp/chrony"
11 )
@@ -16,13 +17,39 @@ func newChronyClient(c Config) (chronyClient, error) {
17 }
18
19 client := &simpleClient{
19 - conn: conn,
20 - client: &chrony.Client{Connection: conn},
20 + conn: conn,
21 + client: &chrony.Client{Connection: &connWithTimeout{
22 + Conn: conn,
23 + timeout: c.Timeout.Duration(),
24 + }},
25 }
26
27 return client, nil
28 }
29
30 +type connWithTimeout struct {
31 + net.Conn
32 + timeout time.Duration
33 +}
34 +
35 +func (c *connWithTimeout) Read(p []byte) (n int, err error) {
36 + if err := c.Conn.SetReadDeadline(c.deadline()); err != nil {
37 + return 0, err
38 + }
39 + return c.Conn.Read(p)
40 +}
41 +
42 +func (c *connWithTimeout) Write(p []byte) (n int, err error) {
43 + if err := c.Conn.SetWriteDeadline(c.deadline()); err != nil {
44 + return 0, err
45 + }
46 + return c.Conn.Write(p)
47 +}
48 +
49 +func (c *connWithTimeout) deadline() time.Time {
50 + return time.Now().Add(c.timeout)
51 +}
52 +
53 type simpleClient struct {
54 conn net.Conn
55 client *chrony.Client