diff options
author | Thomas Haller <thaller@redhat.com> | 2018-04-21 15:21:38 +0200 |
---|---|---|
committer | Thomas Haller <thaller@redhat.com> | 2018-04-24 09:12:13 +0200 |
commit | 19994f34fa08d913826ad87276ea0cd8255497cd (patch) | |
tree | 276d3f6016af0b0a07884141e9140f971ae7c2da | |
parent | ebe713170c2056369d815c05250241ced158c420 (diff) | |
download | NetworkManager-th/connectivity-check-on-activated.tar.gz |
device/connectivity: refactor concheck_periodic_schedule_do()th/connectivity-check-on-activated
Instead of passing the interval for the timeout, let concheck_periodic_schedule_do()
figure it out on its own. It only depends on cur-interval and
cur-basetime.
Additionally, pass now_ns timestamp, because we already made
decisions based on this particular timestamp. We don't want to
re-evalutate the current time but ensure to use the same timestamp.
There is no change in behavior, it just seems nicer this way.
-rw-r--r-- | src/devices/nm-device.c | 54 |
1 files changed, 32 insertions, 22 deletions
diff --git a/src/devices/nm-device.c b/src/devices/nm-device.c index b665b799bf..9dca2487c5 100644 --- a/src/devices/nm-device.c +++ b/src/devices/nm-device.c @@ -2264,10 +2264,11 @@ concheck_is_possible (NMDevice *self) } static gboolean -concheck_periodic_schedule_do (NMDevice *self, gint64 interval_ns) +concheck_periodic_schedule_do (NMDevice *self, gint64 now_ns) { NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE (self); gboolean periodic_check_disabled = FALSE; + gint64 expiry, tdiff; /* we always cancel whatever was pending. */ if (nm_clear_g_source (&priv->concheck_p_cur_id)) @@ -2278,18 +2279,25 @@ concheck_periodic_schedule_do (NMDevice *self, gint64 interval_ns) goto out; } - nm_assert (interval_ns >= 0); - if (!concheck_is_possible (self)) goto out; - _LOGT (LOGD_CONCHECK, "connectivity: periodic-check: %sscheduled in %u milliseconds (%u seconds interval)", + nm_assert (now_ns > 0); + nm_assert (priv->concheck_p_cur_interval > 0); + + /* we schedule the timeout based on our current settings cur-interval and cur-basetime. + * Before calling concheck_periodic_schedule_do(), make sure that these properties are + * correct. */ + + expiry = priv->concheck_p_cur_basetime_ns + (priv->concheck_p_cur_interval * NM_UTILS_NS_PER_SECOND); + tdiff = expiry - now_ns; + + _LOGT (LOGD_CONCHECK, "connectivity: periodic-check: %sscheduled in %lld milliseconds (%u seconds interval)", periodic_check_disabled ? "re-" : "", - (guint) (interval_ns / NM_UTILS_NS_PER_MSEC), + (long long) (tdiff / NM_UTILS_NS_PER_MSEC), priv->concheck_p_cur_interval); - nm_assert (priv->concheck_p_cur_interval > 0); - priv->concheck_p_cur_id = g_timeout_add (interval_ns / NM_UTILS_NS_PER_MSEC, + priv->concheck_p_cur_id = g_timeout_add (NM_MAX ((gint64) 0, tdiff) / NM_UTILS_NS_PER_MSEC, concheck_periodic_timeout_cb, self); return TRUE; @@ -2330,7 +2338,7 @@ concheck_periodic_schedule_set (NMDevice *self, case CONCHECK_SCHEDULE_UPDATE_INTERVAL_RESTART: priv->concheck_p_cur_interval = NM_MIN (priv->concheck_p_max_interval, CONCHECK_P_PROBE_INTERVAL); priv->concheck_p_cur_basetime_ns = nm_utils_get_monotonic_timestamp_ns_cached (&now_ns); - if (concheck_periodic_schedule_do (self, priv->concheck_p_cur_interval * NM_UTILS_NS_PER_SECOND)) + if (concheck_periodic_schedule_do (self, now_ns)) concheck_start (self, NULL, NULL, TRUE); return; @@ -2358,7 +2366,7 @@ concheck_periodic_schedule_set (NMDevice *self, * new max_interval passed. We need to start a check right away (and * schedule a timeout in cur-interval in the future). */ priv->concheck_p_cur_basetime_ns = now_ns; - if (concheck_periodic_schedule_do (self, priv->concheck_p_cur_interval * NM_UTILS_NS_PER_SECOND)) + if (concheck_periodic_schedule_do (self, now_ns)) concheck_start (self, NULL, NULL, TRUE); } else { /* we are reducing the max-interval to a shorter interval that we have currently @@ -2367,14 +2375,14 @@ concheck_periodic_schedule_set (NMDevice *self, * However, since the last time we scheduled the check, not even the new max-interval * expired. All we need to do, is reschedule the timer to expire sooner. The cur_basetime * is unchanged. */ - concheck_periodic_schedule_do (self, cur_expiry - now_ns); + concheck_periodic_schedule_do (self, now_ns); } return; case CONCHECK_SCHEDULE_CHECK_EXTERNAL: /* a external connectivity check delays our periodic check. We reset the counter. */ priv->concheck_p_cur_basetime_ns = nm_utils_get_monotonic_timestamp_ns_cached (&now_ns); - concheck_periodic_schedule_do (self, priv->concheck_p_cur_interval * NM_UTILS_NS_PER_SECOND); + concheck_periodic_schedule_do (self, now_ns); return; case CONCHECK_SCHEDULE_CHECK_PERIODIC: @@ -2407,15 +2415,16 @@ concheck_periodic_schedule_set (NMDevice *self, new_expiry = exp_expiry + (priv->concheck_p_cur_interval * NM_UTILS_NS_PER_SECOND); tdiff = NM_MAX (new_expiry - now_ns, 0); priv->concheck_p_cur_basetime_ns = (now_ns + tdiff) - (priv->concheck_p_cur_interval * NM_UTILS_NS_PER_SECOND); - concheck_periodic_schedule_do (self, tdiff); - handle = concheck_start (self, NULL, NULL, TRUE); - if (old_interval != priv->concheck_p_cur_interval) { - /* we just bumped the interval already when scheduling this check. - * When the handle returns, don't bump a second time. - * - * But if we reach the timeout again before the handle returns (this - * code here) we will still bump the interval. */ - handle->is_periodic_bump_on_complete = FALSE; + if (concheck_periodic_schedule_do (self, now_ns)) { + handle = concheck_start (self, NULL, NULL, TRUE); + if (old_interval != priv->concheck_p_cur_interval) { + /* we just bumped the interval already when scheduling this check. + * When the handle returns, don't bump a second time. + * + * But if we reach the timeout again before the handle returns (this + * code here) we will still bump the interval. */ + handle->is_periodic_bump_on_complete = FALSE; + } } return; } @@ -2442,7 +2451,7 @@ concheck_periodic_schedule_set (NMDevice *self, new_expiry = priv->concheck_p_cur_basetime_ns + (priv->concheck_p_cur_interval * NM_UTILS_NS_PER_SECOND); tdiff = NM_MAX (new_expiry - nm_utils_get_monotonic_timestamp_ns_cached (&now_ns), 0); priv->concheck_p_cur_basetime_ns = now_ns + tdiff - (priv->concheck_p_cur_interval * NM_UTILS_NS_PER_SECOND); - concheck_periodic_schedule_do (self, tdiff); + concheck_periodic_schedule_do (self, now_ns); } static void @@ -2461,7 +2470,8 @@ concheck_update_interval (NMDevice *self, gboolean check_now) } if (!new_interval) { - /* this will cancel any potentially pending timeout. */ + /* this will cancel any potentially pending timeout because max-interval is zero. + * But it logs a nice message... */ concheck_periodic_schedule_do (self, 0); /* also update the fake connectivity state. */ |