text stringlengths 0 2.2M |
|---|
//..
|
bool sendData(size_t dataSize)
|
// Send a specified 'dataSize' amount of data over the network.
|
// Return 'true' if data was sent successfully and 'false' otherwise.
|
{
|
(void)(dataSize);
|
//..
|
// For simplicity, 'sendData' will not actually send any data and will always
|
// return 'true'.
|
//..
|
return true;
|
}
|
//..
|
//=============================================================================
|
// MAIN PROGRAM
|
//-----------------------------------------------------------------------------
|
int main(int argc, char *argv[])
|
{
|
int test = argc > 1 ? atoi(argv[1]) : 0;
|
bool verbose = argc > 2;
|
//bool veryVerbose = argc > 3;
|
//bool veryVeryVerbose = argc > 4;
|
cout << "TEST " << __FILE__ << " CASE " << test << endl;
|
switch (test) { case 0: // Zero is always the leading case.
|
case 13: {
|
// --------------------------------------------------------------------
|
// USAGE EXAMPLE
|
// The usage example provided in the component header file must
|
// compile, link, and run on all platforms as shown.
|
//
|
// Plan:
|
// Incorporate usage example from header into driver, remove leading
|
// comment characters and replace 'assert' with 'ASSERT'.
|
//
|
// Testing:
|
// USAGE EXAMPLE
|
// --------------------------------------------------------------------
|
if (verbose) cout << endl
|
<< "TESTING USAGE EXAMPLE" << endl
|
<< "=====================" << endl;
|
// First, we create a 'RateLimiter' object having a sustained rate of 1024
|
// bytes/s, a sustained-rate time-window of 0.5s (512 bytes / 1024 bytes/s),
|
// a peak-rate of 2048 bytes/s, and a peak-rate time-window of 0.0625s
|
// (128 bytes / 2048 bytes/s):
|
//..
|
bsls::Types::Uint64 sustainedRateLimit = 1024;
|
bsls::TimeInterval sustainedRateWindow(0.5);
|
bsls::Types::Uint64 peakRateLimit = 2048;
|
bsls::TimeInterval peakRateWindow(0.0625);
|
bsls::TimeInterval now = bdlt::CurrentTime::now();
|
balb::RateLimiter rateLimiter(sustainedRateLimit,
|
sustainedRateWindow,
|
peakRateLimit,
|
peakRateWindow,
|
now);
|
//..
|
// Note that the rate limiter does not prevent the rate at any instant from
|
// exceeding either the peak-rate or the sustained rate; instead, it prevents
|
// the average rate over the peak-rate time-window from exceeding maximum
|
// peak-rate and the average rate over the sustained-rate time-window from
|
// exceeding the maximum sustained-rate.
|
//
|
// Then, we define the size of data to be send, the size of each data chunk,
|
// and a counter of data actually sent:
|
//..
|
bsls::Types::Uint64 sizeOfData = 10 * 1024; // in bytes
|
bsls::Types::Uint64 chunkSize = 64; // in bytes
|
bsls::Types::Uint64 bytesSent = 0;
|
//..
|
// Now, we send the chunks of data using a loop. For each iteration, we check
|
// whether submitting another byte would exceed the rate limiter's bandwidth
|
// limits. If not, we send an additional chunk of data and submit the number
|
// of bytes sent to the leaky bucket. Note that 'submit' is invoked only after
|
// the data has been sent.
|
//..
|
while (bytesSent < sizeOfData) {
|
now = bdlt::CurrentTime::now();
|
if (!rateLimiter.wouldExceedBandwidth(now)) {
|
if (true == sendData(chunkSize)) {
|
rateLimiter.submit(chunkSize);
|
bytesSent += chunkSize;
|
}
|
}
|
//..
|
// Finally, if submitting another byte will cause the rate limiter to exceed
|
// its bandwidth limits, then we wait until the submission will be allowed by
|
// waiting for an amount time returned by the 'calculateTimeToSubmit' method:
|
//..
|
else {
|
bsls::TimeInterval timeToSubmit =
|
rateLimiter.calculateTimeToSubmit(now);
|
bsls::Types::Uint64 uS = timeToSubmit.totalMicroseconds() +
|
(timeToSubmit.nanoseconds() % 1000 ? 1 : 0);
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.