static void test_ratelimit_test(void) { int i; RATELIMIT_DEFINE(ratelimit, 1 * USEC_PER_SEC, 10); for (i = 0; i < 10; i++) assert_se(ratelimit_test(&ratelimit)); assert_se(!ratelimit_test(&ratelimit)); sleep(1); for (i = 0; i < 10; i++) assert_se(ratelimit_test(&ratelimit)); RATELIMIT_INIT(ratelimit, 0, 10); for (i = 0; i < 10000; i++) assert_se(ratelimit_test(&ratelimit)); }
static void tar_import_report_progress(TarImport *i) { unsigned percent; assert(i); /* We have no size information, unless the source is a regular file */ if (!S_ISREG(i->st.st_mode)) return; if (i->written_compressed >= (uint64_t) i->st.st_size) percent = 100; else percent = (unsigned) ((i->written_compressed * UINT64_C(100)) / (uint64_t) i->st.st_size); if (percent == i->last_percent) return; if (!ratelimit_test(&i->progress_rate_limit)) return; sd_notifyf(false, "X_IMPORT_PROGRESS=%u", percent); log_info("Imported %u%%.", percent); i->last_percent = percent; }
static void tar_export_report_progress(TarExport *e) { unsigned percent; assert(e); /* Do we have any quota info? If not, we don't know anything about the progress */ if (e->quota_referenced == (uint64_t) -1) return; if (e->written_uncompressed >= e->quota_referenced) percent = 100; else percent = (unsigned) ((e->written_uncompressed * UINT64_C(100)) / e->quota_referenced); if (percent == e->last_percent) return; if (!ratelimit_test(&e->progress_rate_limit)) return; sd_notifyf(false, "X_IMPORT_PROGRESS=%u", percent); log_info("Exported %u%%.", percent); e->last_percent = percent; }
int manager_connect(Manager *m) { int r; assert(m); manager_disconnect(m); m->event_retry = sd_event_source_unref(m->event_retry); if (!ratelimit_test(&m->ratelimit)) { log_debug("Slowing down attempts to contact servers."); r = sd_event_add_time(m->event, &m->event_retry, clock_boottime_or_monotonic(), now(clock_boottime_or_monotonic()) + RETRY_USEC, 0, manager_retry_connect, m); if (r < 0) { log_error("Failed to create retry timer: %s", strerror(-r)); return r; } return 0; } /* If we already are operating on some address, switch to the * next one. */ if (m->current_server_address && m->current_server_address->addresses_next) manager_set_server_address(m, m->current_server_address->addresses_next); else { struct addrinfo hints = { .ai_flags = AI_NUMERICSERV|AI_ADDRCONFIG, .ai_socktype = SOCK_DGRAM, }; /* Hmm, we are through all addresses, let's look for the next host instead */ if (m->current_server_name && m->current_server_name->names_next) manager_set_server_name(m, m->current_server_name->names_next); else { ServerName *f; /* Our current server name list is exhausted, * let's find the next one to iterate. First * we try the system list, then the link list. * After having processed the link list we * jump back to the system list. However, if * both lists are empty, we change to the * fallback list. */ if (!m->current_server_name || m->current_server_name->type == SERVER_LINK) { f = m->system_servers; if (!f) f = m->link_servers; } else { f = m->link_servers; if (!f) f = m->system_servers; } if (!f) f = m->fallback_servers; if (!f) { manager_set_server_name(m, NULL); log_debug("No server found."); return 0; } manager_set_server_name(m, f); } /* Tell the resolver to reread /etc/resolv.conf, in * case it changed. */ res_init(); /* Flush out any previously resolved addresses */ server_name_flush_addresses(m->current_server_name); log_debug("Resolving %s...", m->current_server_name->string); r = sd_resolve_getaddrinfo(m->resolve, &m->resolve_query, m->current_server_name->string, "123", &hints, manager_resolve_handler, m); if (r < 0) { log_error("Failed to create resolver: %s", strerror(-r)); return r; } return 1; } r = manager_begin(m); if (r < 0) return r; return 1; } void manager_disconnect(Manager *m) { assert(m); m->resolve_query = sd_resolve_query_unref(m->resolve_query); m->event_timer = sd_event_source_unref(m->event_timer); m->event_receive = sd_event_source_unref(m->event_receive); m->server_socket = safe_close(m->server_socket); m->event_clock_watch = sd_event_source_unref(m->event_clock_watch); m->clock_watch_fd = safe_close(m->clock_watch_fd); m->event_timeout = sd_event_source_unref(m->event_timeout); sd_notifyf(false, "STATUS=Idle."); }