3838#include < http/HttpConfig.h>
3939
4040using ts::TextView;
41+ using std::chrono::duration_cast;
4142
4243HostDBProcessor hostDBProcessor;
4344int HostDBProcessor::hostdb_strict_round_robin = 0 ;
@@ -54,11 +55,12 @@ unsigned int hostdb_ip_stale_interval = HOST_DB_IP_STALE;
5455unsigned int hostdb_ip_timeout_interval = HOST_DB_IP_TIMEOUT;
5556unsigned int hostdb_ip_fail_timeout_interval = HOST_DB_IP_FAIL_TIMEOUT;
5657unsigned int hostdb_serve_stale_but_revalidate = 0 ;
57- ts_seconds hostdb_hostfile_check_interval{std::chrono::hours (24 )};
58- // Epoch timestamp of the current hosts file check.
59- ts_time hostdb_current_interval{TS_TIME_ZERO};
58+ static ts_seconds hostdb_hostfile_check_interval{std::chrono::hours (24 )};
59+ // Epoch timestamp of the current hosts file check. This also functions as a
60+ // cached version of ts_clock::now().
61+ ts_time hostdb_current_timestamp{TS_TIME_ZERO};
6062// Epoch timestamp of the last time we actually checked for a hosts file update.
61- static ts_time hostdb_last_interval {TS_TIME_ZERO};
63+ static ts_time hostdb_last_timestamp {TS_TIME_ZERO};
6264// Epoch timestamp when we updated the hosts file last.
6365static ts_time hostdb_hostfile_update_timestamp{TS_TIME_ZERO};
6466static char hostdb_filename[PATH_NAME_MAX] = DEFAULT_HOST_DB_FILENAME;
@@ -420,7 +422,7 @@ HostDBBackgroundTask::wait_event(int, void *)
420422
421423 SET_HANDLER (&HostDBBackgroundTask::sync_event);
422424 if (next_sync > ts_milliseconds{100 }) {
423- eventProcessor.schedule_in (this , std::chrono:: duration_cast<std::chrono::nanoseconds >(next_sync).count (), ET_TASK);
425+ eventProcessor.schedule_in (this , duration_cast<ts_nanoseconds >(next_sync).count (), ET_TASK);
424426 } else {
425427 eventProcessor.schedule_imm (this , ET_TASK);
426428 }
@@ -552,9 +554,10 @@ HostDBProcessor::start(int, size_t)
552554 REC_EstablishStaticConfigInt32U (hostdb_round_robin_max_count, " proxy.config.hostdb.round_robin_max_count" );
553555
554556 //
555- // Set up hostdb_current_interval
557+ // Initialize hostdb_current_timestamp which is our cached version of
558+ // ts_clock::now().
556559 //
557- hostdb_current_interval = ts_clock::now ();
560+ hostdb_current_timestamp = ts_clock::now ();
558561
559562 HostDBContinuation *b = hostDBContAllocator.alloc ();
560563 SET_CONTINUATION_HANDLER (b, (HostDBContHandler)&HostDBContinuation::backgroundEvent);
@@ -693,19 +696,19 @@ probe(const Ptr<ProxyMutex> &mutex, HostDBHash const &hash, bool ignore_timeout)
693696 }
694697
695698 // If the record is stale, but we want to revalidate-- lets start that up
696- if ((!ignore_timeout && record->is_ip_stale () && record->record_type != HostDBType::HOST) ||
699+ if ((!ignore_timeout && record->is_ip_configured_stale () && record->record_type != HostDBType::HOST) ||
697700 (record->is_ip_timeout () && record->serve_stale_but_revalidate ())) {
698701 HOSTDB_INCREMENT_DYN_STAT (hostdb_total_serve_stale_stat);
699702 if (hostDB.is_pending_dns_for_hash (hash.hash )) {
700703 Debug (" hostdb" , " %s" ,
701- ts::bwprint (ts::bw_dbg, " stale {} {} {}, using with pending refresh" , record->ip_interval (),
704+ ts::bwprint (ts::bw_dbg, " stale {} {} {}, using with pending refresh" , record->ip_age (),
702705 record->ip_timestamp .time_since_epoch (), record->ip_timeout_interval )
703706 .c_str ());
704707 return record;
705708 }
706709 Debug (" hostdb" , " %s" ,
707- ts::bwprint (ts::bw_dbg, " stale {} {} {}, using while refresh" , record->ip_interval (),
708- record->ip_timestamp . time_since_epoch (), record-> ip_timeout_interval )
710+ ts::bwprint (ts::bw_dbg, " stale {} {} {}, using while refresh" , record->ip_age (), record-> ip_timestamp . time_since_epoch (),
711+ record->ip_timeout_interval )
709712 .c_str ());
710713 HostDBContinuation *c = hostDBContAllocator.alloc ();
711714 HostDBContinuation::Options copt;
@@ -962,7 +965,7 @@ HostDBContinuation::lookup_done(TextView query_name, ts_seconds answer_ttl, SRVH
962965 ip_text_buffer b;
963966 Debug (" hostdb" , " failed for %s" , hash.ip .toString (b, sizeof b));
964967 }
965- record->ip_timestamp = hostdb_current_interval ;
968+ record->ip_timestamp = hostdb_current_timestamp ;
966969 record->ip_timeout_interval = ts_seconds (std::clamp (hostdb_ip_fail_timeout_interval, 1u , HOST_DB_MAX_TTL));
967970
968971 if (is_srv ()) {
@@ -997,7 +1000,7 @@ HostDBContinuation::lookup_done(TextView query_name, ts_seconds answer_ttl, SRVH
9971000 HOSTDB_SUM_DYN_STAT (hostdb_ttl_stat, answer_ttl.count ());
9981001
9991002 // update the TTL
1000- record->ip_timestamp = hostdb_current_interval ;
1003+ record->ip_timestamp = hostdb_current_timestamp ;
10011004 record->ip_timeout_interval = std::clamp (answer_ttl, ts_seconds (1 ), ts_seconds (HOST_DB_MAX_TTL));
10021005
10031006 if (is_byname ()) {
@@ -1206,9 +1209,9 @@ HostDBContinuation::dnsEvent(int event, HostEnt *e)
12061209 }
12071210
12081211 if (!serve_stale) { // implies r != old_r
1209- hostDB. refcountcache -> put (
1210- r-> key , r. get (), r-> _record_size ,
1211- (r->ip_timestamp + r->ip_timeout_interval + ts_seconds (hostdb_serve_stale_but_revalidate)). time_since_epoch (). count () );
1212+ auto const duration_till_revalidate = r-> expiry_time (). time_since_epoch ();
1213+ auto const seconds_till_revalidate = duration_cast<ts_seconds>(duration_till_revalidate). count ();
1214+ hostDB. refcountcache -> put (r->key , r. get (), r->_record_size , seconds_till_revalidate );
12121215 } else {
12131216 Warning (" Fallback to serving stale record, skip re-update of hostdb for %.*s" , int (query_name.size ()), query_name.data ());
12141217 }
@@ -1511,22 +1514,22 @@ HostDBContinuation::do_dns()
15111514
15121515//
15131516// Background event
1514- // Just increment the current_interval. Might do other stuff
1515- // here, like move records to the current position in the cluster.
1516- //
1517+ // Increment the hostdb_current_timestamp which funcions as our cached version
1518+ // of ts_clock::now(). Might do other stuff here, like move records to the
1519+ // current position in the cluster.
15171520int
15181521HostDBContinuation::backgroundEvent (int /* event ATS_UNUSED */ , Event * /* e ATS_UNUSED */ )
15191522{
15201523 std::string dbg;
15211524
1522- // No nothing if hosts file checking is not enabled.
1525+ hostdb_current_timestamp = ts_clock::now ();
1526+
1527+ // Do nothing if hosts file checking is not enabled.
15231528 if (hostdb_hostfile_check_interval.count () == 0 ) {
15241529 return EVENT_CONT;
15251530 }
15261531
1527- hostdb_current_interval = ts_clock::now ();
1528-
1529- if ((hostdb_current_interval - hostdb_last_interval) > hostdb_hostfile_check_interval) {
1532+ if ((hostdb_current_timestamp - hostdb_last_timestamp) > hostdb_hostfile_check_interval) {
15301533 bool update_p = false ; // do we need to reparse the file and update?
15311534 char path[PATH_NAME_MAX];
15321535
@@ -1539,7 +1542,7 @@ HostDBContinuation::backgroundEvent(int /* event ATS_UNUSED */, Event * /* e ATS
15391542 hostdb_hostfile_path = path;
15401543 update_p = true ;
15411544 } else if (!hostdb_hostfile_path.empty ()) {
1542- hostdb_last_interval = hostdb_current_interval ;
1545+ hostdb_last_timestamp = hostdb_current_timestamp ;
15431546 std::error_code ec;
15441547 auto stat{ts::file::status (hostdb_hostfile_path, ec)};
15451548 if (!ec) {
@@ -1688,7 +1691,7 @@ struct ShowHostDB : public ShowCont {
16881691 CHECK_SHOW (show (" <table border=1>\n " ));
16891692 CHECK_SHOW (show (" <tr><td>%s</td><td>%d</td></tr>\n " , " Total" , r->rr_count ));
16901693 CHECK_SHOW (show (" <tr><td>%s</td><td>%d</td></tr>\n " , " Current" , r->_rr_idx .load ()));
1691- CHECK_SHOW (show (" <tr><td>%s</td><td>%s</td></tr>\n " , " Stale" , r->is_ip_stale () ? " Yes" : " No" ));
1694+ CHECK_SHOW (show (" <tr><td>%s</td><td>%s</td></tr>\n " , " Stale" , r->is_ip_configured_stale () ? " Yes" : " No" ));
16921695 CHECK_SHOW (show (" <tr><td>%s</td><td>%s</td></tr>\n " , " Timed-Out" , r->is_ip_timeout () ? " Yes" : " No" ));
16931696 CHECK_SHOW (show (" </table>\n " ));
16941697 } else {
@@ -2112,7 +2115,7 @@ ParseHostFile(ts::file::path const &path, ts_seconds hostdb_hostfile_check_inter
21122115 }
21132116 }
21142117
2115- hostdb_hostfile_update_timestamp = hostdb_current_interval ;
2118+ hostdb_hostfile_update_timestamp = hostdb_current_timestamp ;
21162119 }
21172120 }
21182121
@@ -2280,10 +2283,10 @@ HostDBRecord::serve_stale_but_revalidate() const
22802283
22812284 // ip_timeout_interval == DNS TTL
22822285 // hostdb_serve_stale_but_revalidate == number of seconds
2283- // ip_interval () is the number of seconds between now() and when the entry was inserted
2284- if ((ip_timeout_interval + ts_seconds (hostdb_serve_stale_but_revalidate)) > ip_interval ()) {
2285- Debug_bw (" hostdb" , " serving stale entry {} | {} | {} as requested by config" , ip_timeout_interval ,
2286- hostdb_serve_stale_but_revalidate, ip_interval ());
2286+ // ip_age () is the number of seconds between now() and when the entry was inserted
2287+ if ((ip_timeout_interval + ts_seconds (hostdb_serve_stale_but_revalidate)) > ip_age ()) {
2288+ Debug_bw (" hostdb" , " serving stale entry for {}, TTL: {}, serve_stale_for: {}, age: {} as requested by config" , name () ,
2289+ ip_timeout_interval, hostdb_serve_stale_but_revalidate, ip_age ());
22872290 return true ;
22882291 }
22892292
0 commit comments