netutils/ntpclient: Fix a large number of print DNS request errors

Before attempting to sample via NTP, network connectivity should be checked to avoid frequent sampling that could cause network error logs to overflow.

Signed-off-by: zhangshuai39 <zhangshuai39@xiaomi.com>
This commit is contained in:
zhangshuai39 2025-06-06 20:44:56 +08:00 committed by Xiang Xiao
parent 13a019450a
commit 63f61d080a

View file

@ -52,6 +52,7 @@
#include <nuttx/clock.h>
#include "netutils/netlib.h"
#include "netutils/ntpclient.h"
#include "ntpv3.h"
@ -103,6 +104,8 @@
#define MAX_SERVER_SELECTION_RETRIES 3
#define MAX_RETRY_INTERVAL 120
#ifndef STR
# define STR2(x) #x
# define STR(x) STR2(x)
@ -1233,6 +1236,7 @@ static int ntpc_daemon(int argc, FAR char **argv)
FAR struct ntp_servers_s *srvs;
int exitcode = EXIT_SUCCESS;
int retries = 0;
int retry_delay = 1;
int nsamples;
int ret;
@ -1302,21 +1306,24 @@ static int ntpc_daemon(int argc, FAR char **argv)
clock_gettime(CLOCK_REALTIME, &start_realtime);
clock_gettime(CLOCK_MONOTONIC, &start_monotonic);
/* Collect samples. */
nsamples = 0;
for (i = 0; i < CONFIG_NETUTILS_NTPCLIENT_NUM_SAMPLES; i++)
if (netlib_check_ipconnectivity(NULL, 1, 1) > 0)
{
/* Get next sample. */
/* Collect samples. */
ret = ntpc_get_ntp_sample(srvs, samples, nsamples);
if (ret < 0)
for (i = 0; i < CONFIG_NETUTILS_NTPCLIENT_NUM_SAMPLES; i++)
{
errval = errno;
}
else
{
++nsamples;
/* Get next sample. */
ret = ntpc_get_ntp_sample(srvs, samples, nsamples);
if (ret < 0)
{
errval = errno;
}
else
{
++nsamples;
}
}
}
@ -1405,6 +1412,7 @@ static int ntpc_daemon(int argc, FAR char **argv)
sleep(CONFIG_NETUTILS_NTPCLIENT_POLLDELAYSEC);
retries = 0;
retry_delay = 1;
}
continue;
@ -1424,7 +1432,14 @@ static int ntpc_daemon(int argc, FAR char **argv)
if (errval != EINTR)
{
sleep(1);
ninfo("Retry %d in %d seconds...\n", retries, retry_delay);
sleep(retry_delay);
retry_delay *= 2;
if (retry_delay > MAX_RETRY_INTERVAL)
{
retry_delay = MAX_RETRY_INTERVAL;
}
}
/* Keep retrying. */