From 95f00943106a33112425b69ab61ba0ed8af201ef Mon Sep 17 00:00:00 2001 From: Jordan Whited Date: Thu, 5 Sep 2024 13:40:12 -0700 Subject: [PATCH] cmd/stunstamp: cleanup timeout and interval constants (#13393) Updates #cleanup Signed-off-by: Jordan Whited --- cmd/stunstamp/stunstamp.go | 15 ++++++++++++--- cmd/stunstamp/stunstamp_linux.go | 10 +++++----- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/cmd/stunstamp/stunstamp.go b/cmd/stunstamp/stunstamp.go index 1aaa7ced1..7b97a66c5 100644 --- a/cmd/stunstamp/stunstamp.go +++ b/cmd/stunstamp/stunstamp.go @@ -53,7 +53,16 @@ var ( ) const ( - minInterval = time.Second + // maxTxJitter is the upper bounds for jitter introduced across probes + maxTXJitter = time.Millisecond * 400 + // minInterval is the minimum allowed probe interval/step + minInterval = time.Second * 10 + // txRxTimeout is the timeout value used for kernel timestamping loopback, + // and packet receive operations + txRxTimeout = time.Second * 2 + // maxBufferDuration is the maximum duration (maxBufferDuration / + // *flagInterval steps worth) of buffered data that can be held in memory + // before data loss occurs around prometheus unavailability. maxBufferDuration = time.Hour ) @@ -322,7 +331,7 @@ func measureSTUNRTT(conn io.ReadWriteCloser, _ string, dst netip.AddrPort) (rtt if !ok { return 0, fmt.Errorf("unexpected conn type: %T", conn) } - err = uconn.SetReadDeadline(time.Now().Add(time.Second * 2)) + err = uconn.SetReadDeadline(time.Now().Add(txRxTimeout)) if err != nil { return 0, fmt.Errorf("error setting read deadline: %w", err) } @@ -380,7 +389,7 @@ func probe(meta nodeMeta, cf *connAndMeasureFn, dstPort int) (*time.Duration, er Port: dstPort, } - time.Sleep(rand.N(400 * time.Millisecond)) // jitter across tx + time.Sleep(rand.N(maxTXJitter)) // jitter across tx rtt, err := cf.fn(cf.conn, meta.hostname, netip.AddrPortFrom(meta.addr, uint16(dstPort))) if err != nil { if isTemporaryOrTimeoutErr(err) { diff --git a/cmd/stunstamp/stunstamp_linux.go b/cmd/stunstamp/stunstamp_linux.go index 886f8909a..387805fef 100644 --- a/cmd/stunstamp/stunstamp_linux.go +++ b/cmd/stunstamp/stunstamp_linux.go @@ -107,7 +107,7 @@ func measureICMPRTT(source timestampSource, conn io.ReadWriteCloser, _ string, d } if source == timestampSourceKernel { - txCtx, txCancel := context.WithTimeout(context.Background(), time.Second*2) + txCtx, txCancel := context.WithTimeout(context.Background(), txRxTimeout) defer txCancel() buf := make([]byte, 1024) @@ -142,8 +142,8 @@ func measureICMPRTT(source timestampSource, conn io.ReadWriteCloser, _ string, d } } - rxCtx, txCancel := context.WithTimeout(context.Background(), time.Second*2) - defer txCancel() + rxCtx, rxCancel := context.WithTimeout(context.Background(), txRxTimeout) + defer rxCancel() rxBuf := make([]byte, 1024) oob := make([]byte, 1024) @@ -210,7 +210,7 @@ func measureSTUNRTTKernel(conn io.ReadWriteCloser, _ string, dst netip.AddrPort) return 0, fmt.Errorf("sendto error: %v", err) // don't wrap } - txCtx, txCancel := context.WithTimeout(context.Background(), time.Second*2) + txCtx, txCancel := context.WithTimeout(context.Background(), txRxTimeout) defer txCancel() buf := make([]byte, 1024) @@ -236,7 +236,7 @@ func measureSTUNRTTKernel(conn io.ReadWriteCloser, _ string, dst netip.AddrPort) break } - rxCtx, rxCancel := context.WithTimeout(context.Background(), time.Second*2) + rxCtx, rxCancel := context.WithTimeout(context.Background(), txRxTimeout) defer rxCancel() for {