summaryrefslogtreecommitdiff
path: root/methods
diff options
context:
space:
mode:
authorDavid Kalnischkies <david@kalnischkies.de>2018-05-26 21:26:03 +0200
committerDavid Kalnischkies <david@kalnischkies.de>2018-05-29 13:04:59 +0200
commitf6655a1138a11e80884959014939a25f23a1e308 (patch)
tree4169c00124a5e97d73ed7fd44483eb54ab9b1af7 /methods
parent2fc09a90e7e62a4c3e4a67506bf90fcf4c6ccfaf (diff)
Use steady clock source for bandwidth limitation
Using the time of day for this is slightly wrong just like it is for progress, just less visible.
Diffstat (limited to 'methods')
-rw-r--r--methods/http.cc18
-rw-r--r--methods/http.h3
2 files changed, 11 insertions, 10 deletions
diff --git a/methods/http.cc b/methods/http.cc
index 1933d9d00..2e7fca307 100644
--- a/methods/http.cc
+++ b/methods/http.cc
@@ -25,6 +25,7 @@
#include <apt-pkg/proxy.h>
#include <apt-pkg/strutl.h>
+#include <chrono>
#include <cstring>
#include <iostream>
#include <sstream>
@@ -49,7 +50,7 @@ using namespace std;
unsigned long long CircleBuf::BwReadLimit=0;
unsigned long long CircleBuf::BwTickReadData=0;
-struct timeval CircleBuf::BwReadTick={0,0};
+std::chrono::steady_clock::duration CircleBuf::BwReadTick{0};
const unsigned int CircleBuf::BW_HZ=10;
// CircleBuf::CircleBuf - Circular input buffer /*{{{*/
@@ -98,18 +99,17 @@ bool CircleBuf::Read(std::unique_ptr<MethodFd> const &Fd)
unsigned long long const BwReadMax = CircleBuf::BwReadLimit/BW_HZ;
if(CircleBuf::BwReadLimit) {
- struct timeval now;
- gettimeofday(&now,0);
+ auto const now = std::chrono::steady_clock::now().time_since_epoch();
+ auto const d = now - CircleBuf::BwReadTick;
- unsigned long long d = (now.tv_sec-CircleBuf::BwReadTick.tv_sec)*1000000 +
- now.tv_usec-CircleBuf::BwReadTick.tv_usec;
- if(d > 1000000/BW_HZ) {
+ auto const tickLen = std::chrono::microseconds(std::chrono::seconds(1)) / BW_HZ;
+ if(d > tickLen) {
CircleBuf::BwReadTick = now;
CircleBuf::BwTickReadData = 0;
- }
-
+ }
+
if(CircleBuf::BwTickReadData >= BwReadMax) {
- usleep(1000000/BW_HZ);
+ usleep(tickLen.count());
return true;
}
}
diff --git a/methods/http.h b/methods/http.h
index ae6cedc13..5668f0b87 100644
--- a/methods/http.h
+++ b/methods/http.h
@@ -12,6 +12,7 @@
#include <apt-pkg/strutl.h>
+#include <chrono>
#include <iostream>
#include <memory>
#include <string>
@@ -39,7 +40,7 @@ class CircleBuf
static unsigned long long BwReadLimit;
static unsigned long long BwTickReadData;
- static struct timeval BwReadTick;
+ static std::chrono::steady_clock::duration BwReadTick;
static const unsigned int BW_HZ;
unsigned long long LeftRead() const