tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

add_delayms.patch (4593B)


      1 From a8797622ec030dcfb8a51d2d473111bc872a4fa5 Mon Sep 17 00:00:00 2001
      2 From: David P <daparks@mozilla.com>
      3 Date: Mon, 3 Feb 2025 10:51:45 -0800
      4 Subject: [PATCH] Bug 1936020: Part 10 - Add --delaysMs to content analysis
      5 demo agent r=#dlp-reviewers!
      6 
      7 Allows us to delay at a granularity finer than one second.
      8 
      9 diff --git a/demo/agent.cc b/demo/agent.cc
     10 index 3e168b0915a0c..4460a164722f5 100644
     11 --- a/demo/agent.cc
     12 +++ b/demo/agent.cc
     13 @@ -25,7 +25,7 @@ constexpr char kPathSystem[] = "brcm_chrm_cas";
     14 std::string path = kPathSystem;
     15 bool use_queue = false;
     16 bool user_specific = false;
     17 -std::vector<unsigned long> delays = {0};  // In seconds.
     18 +std::vector<unsigned long> delays = {0};  // In milliseconds.
     19 unsigned long num_threads = 8u;
     20 std::string save_print_data_path = "";
     21 RegexArray toBlock, toWarn, toReport;
     22 @@ -34,6 +34,7 @@ static std::string modeStr;
     23 
     24 // Command line parameters.
     25 constexpr const char* kArgDelaySpecific = "--delays=";
     26 +constexpr const char* kArgDelayMsSpecific = "--delaysMs=";
     27 constexpr const char* kArgPath = "--path=";
     28 constexpr const char* kArgQueued = "--queued";
     29 constexpr const char* kArgThreads = "--threads=";
     30 @@ -80,23 +81,22 @@ bool ParseCommandLine(int argc, char* argv[]) {
     31       }
     32       path = kPathUser;
     33       user_specific = true;
     34 -    } else if (arg.find(kArgDelaySpecific) == 0) {
     35 -      std::string delaysStr = arg.substr(strlen(kArgDelaySpecific));
     36 +    } else if ((arg.find(kArgDelaySpecific) == 0) ||
     37 +               (arg.find(kArgDelayMsSpecific) == 0)) {
     38 +      bool isSecs = (arg.find(kArgDelaySpecific) == 0);
     39 +      std::string delaysStr = arg.substr(strlen(isSecs ? kArgDelaySpecific : kArgDelayMsSpecific));
     40 +      unsigned long scale = isSecs ? 1000 : 1;
     41       delays.clear();
     42       size_t posStart = 0, posEnd;
     43       unsigned long delay;
     44       while ((posEnd = delaysStr.find(',', posStart)) != std::string::npos) {
     45         delay = std::stoul(delaysStr.substr(posStart, posEnd - posStart));
     46 -        if (delay > 30) {
     47 -            delay = 30;
     48 -        }
     49 +        delay = std::min(delay*scale, 30*1000ul);
     50         delays.push_back(delay);
     51         posStart = posEnd + 1;
     52       }
     53       delay = std::stoul(delaysStr.substr(posStart));
     54 -      if (delay > 30) {
     55 -          delay = 30;
     56 -      }
     57 +      delay = std::min(delay*scale, 30*1000ul);
     58       delays.push_back(delay);
     59     } else if (arg.find(kArgPath) == 0) {
     60       path = arg.substr(strlen(kArgPath));
     61 @@ -132,6 +132,7 @@ void PrintHelp() {
     62     << "Data containing the string 'block' blocks the request data from being used." << std::endl
     63     << std::endl << "Options:"  << std::endl
     64     << kArgDelaySpecific << "<delay1,delay2,...> : Add delays to request processing in seconds. Delays are limited to 30 seconds and are applied round-robin to requests. Default is 0." << std::endl
     65 +    << kArgDelayMsSpecific << "<delay1,delay2,...> : Like --delays but takes durations in milliseconds." << std::endl
     66     << kArgPath << " <path> : Used the specified path instead of default. Must come after --user." << std::endl
     67     << kArgQueued << " : Queue requests for processing in a background thread" << std::endl
     68     << kArgThreads << " : When queued, number of threads in the request processing thread pool" << std::endl
     69 diff --git a/demo/handler.h b/demo/handler.h
     70 index 88599963c51b0..c578d72012848 100644
     71 --- a/demo/handler.h
     72 +++ b/demo/handler.h
     73 @@ -131,9 +131,9 @@ class Handler : public content_analysis::sdk::AgentEventHandler {
     74     unsigned long delay = delays_[nextDelayIndex % delays_.size()];
     75     if (delay > 0) {
     76       aout.stream() << "Delaying response to " << event->GetRequest().request_token()
     77 -                    << " for " << delay << "s" << std::endl<< std::endl;
     78 +                    << " for " << delay << "ms" << std::endl<< std::endl;
     79       aout.flush();
     80 -      std::this_thread::sleep_for(std::chrono::seconds(delay));
     81 +      std::this_thread::sleep_for(std::chrono::milliseconds(delay));
     82     }
     83 
     84     // Send the response back to Google Chrome.
     85 @@ -485,7 +485,7 @@ class QueuingHandler : public Handler {
     86       aout.stream() << "Thread: " << std::this_thread::get_id()
     87                     << std::endl;
     88       aout.stream() << "Delaying request processing for "
     89 -                    << handler->delays()[handler->nextDelayIndex() % handler->delays().size()] << "s" << std::endl << std::endl;
     90 +                    << handler->delays()[handler->nextDelayIndex() % handler->delays().size()] << "ms" << std::endl << std::endl;
     91       aout.flush();
     92 
     93       handler->AnalyzeContent(aout, std::move(event));
     94 -- 
     95 2.37.1.windows.1