1/*
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
23namespace android {
24namespace os {
25namespace incidentd {
26
27Throttler::Throttler(size_t limit, int64_t refractoryPeriodMs)
28    : mSizeLimit(limit),
29      mRefractoryPeriodMs(refractoryPeriodMs),
30      mAccumulatedSize(0),
31      mLastRefractoryMs(android::elapsedRealtime()) {}
32
33Throttler::~Throttler() {}
34
35bool Throttler::shouldThrottle() {
36    int64_t now = android::elapsedRealtime();
37    if (now > mRefractoryPeriodMs + mLastRefractoryMs) {
38        mLastRefractoryMs = now;
39        mAccumulatedSize = 0;
40    }
41    return mAccumulatedSize > mSizeLimit;
42}
43
44void Throttler::addReportSize(size_t reportByteSize) {
45    VLOG("The current request took %d bytes to dropbox", (int)reportByteSize);
46    mAccumulatedSize += reportByteSize;
47}
48
49void Throttler::dump(FILE* out) {
50    fprintf(out, "mSizeLimit=%d\n", (int)mSizeLimit);
51    fprintf(out, "mAccumulatedSize=%d\n", (int)mAccumulatedSize);
52    fprintf(out, "mRefractoryPeriodMs=%d\n", (int)mRefractoryPeriodMs);
53    fprintf(out, "mLastRefractoryMs=%d\n", (int)mLastRefractoryMs);
54}
55
56}  // namespace incidentd
57}  // namespace os
58}  // namespace android