blob: 1abf26785733a39788555b5f1a31ffdefd57b2f6 [file] [log] [blame]
Yi Jin4e843102018-02-14 15:36:18 -08001/*
2 * Copyright (C) 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16#define DEBUG false
17#include "Log.h"
18
19#include "Throttler.h"
20
21#include <utils/SystemClock.h>
22
23Throttler::Throttler(size_t limit, int64_t refractoryPeriodMs)
24 : mSizeLimit(limit),
25 mRefractoryPeriodMs(refractoryPeriodMs),
26 mAccumulatedSize(0),
27 mLastRefractoryMs(android::elapsedRealtime()) {}
28
29Throttler::~Throttler() {}
30
31bool Throttler::shouldThrottle() {
32 int64_t now = android::elapsedRealtime();
33 if (now > mRefractoryPeriodMs + mLastRefractoryMs) {
34 mLastRefractoryMs = now;
35 mAccumulatedSize = 0;
36 }
37 return mAccumulatedSize > mSizeLimit;
38}
39
40void Throttler::addReportSize(size_t reportByteSize) {
41 VLOG("The current request took %d bytes to dropbox", (int)reportByteSize);
42 mAccumulatedSize += reportByteSize;
43}
44
45void Throttler::dump(FILE* out) {
46 fprintf(out, "mSizeLimit=%d\n", (int)mSizeLimit);
47 fprintf(out, "mAccumulatedSize=%d\n", (int)mAccumulatedSize);
48 fprintf(out, "mRefractoryPeriodMs=%d\n", (int)mRefractoryPeriodMs);
49 fprintf(out, "mLastRefractoryMs=%d\n", (int)mLastRefractoryMs);
50}