2017-05-26 04:48:44 +08:00
|
|
|
/*
|
|
|
|
* IRateControl.h
|
|
|
|
*
|
|
|
|
* This source file is part of the FoundationDB open source project
|
|
|
|
*
|
2022-03-22 04:36:23 +08:00
|
|
|
* Copyright 2013-2022 Apple Inc. and the FoundationDB project authors
|
2018-02-22 02:25:11 +08:00
|
|
|
*
|
2017-05-26 04:48:44 +08:00
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
2018-02-22 02:25:11 +08:00
|
|
|
*
|
2017-05-26 04:48:44 +08:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2018-02-22 02:25:11 +08:00
|
|
|
*
|
2017-05-26 04:48:44 +08:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "flow/flow.h"
|
|
|
|
|
|
|
|
class IRateControl {
|
|
|
|
public:
|
|
|
|
virtual ~IRateControl() {}
|
|
|
|
// Future is Ready once you can use n units;
|
|
|
|
virtual Future<Void> getAllowance(unsigned int n) = 0;
|
|
|
|
// If all of the allowance is not used the unused units can be given back.
|
|
|
|
// For convenience, n can safely be negative.
|
|
|
|
virtual void returnUnused(int n) = 0;
|
2021-03-11 02:06:03 +08:00
|
|
|
virtual void killWaiters(const Error& e) = 0;
|
2021-01-24 17:50:09 +08:00
|
|
|
virtual void wakeWaiters() = 0;
|
2017-05-26 04:48:44 +08:00
|
|
|
virtual void addref() = 0;
|
|
|
|
virtual void delref() = 0;
|
|
|
|
};
|
|
|
|
|
2022-03-13 21:02:11 +08:00
|
|
|
// An IRateControl implementation that allows at most hands out at most windowLimit units of 'credit' in windowSeconds
|
2021-03-11 02:06:03 +08:00
|
|
|
// seconds
|
2020-10-08 10:55:05 +08:00
|
|
|
class SpeedLimit final : public IRateControl, ReferenceCounted<SpeedLimit> {
|
2017-05-26 04:48:44 +08:00
|
|
|
public:
|
2021-03-11 02:06:03 +08:00
|
|
|
SpeedLimit(int windowLimit, double windowSeconds)
|
|
|
|
: m_limit(windowLimit), m_seconds(windowSeconds), m_last_update(0), m_budget(0) {
|
2020-12-09 08:44:39 +08:00
|
|
|
m_last_update = now();
|
2017-05-26 04:48:44 +08:00
|
|
|
}
|
2021-03-11 02:06:03 +08:00
|
|
|
~SpeedLimit() override { m_stop.send(Never()); }
|
2017-05-26 04:48:44 +08:00
|
|
|
|
2020-10-08 10:55:05 +08:00
|
|
|
void addref() override { ReferenceCounted<SpeedLimit>::addref(); }
|
|
|
|
void delref() override { ReferenceCounted<SpeedLimit>::delref(); }
|
2017-05-26 04:48:44 +08:00
|
|
|
|
2020-10-08 10:55:05 +08:00
|
|
|
Future<Void> getAllowance(unsigned int n) override {
|
2017-05-26 04:48:44 +08:00
|
|
|
// Replenish budget based on time since last update
|
2020-12-09 08:44:39 +08:00
|
|
|
double ts = now();
|
2017-05-26 04:48:44 +08:00
|
|
|
// returnUnused happens to do exactly what we want here
|
|
|
|
returnUnused((ts - m_last_update) / m_seconds * m_limit);
|
|
|
|
m_last_update = ts;
|
|
|
|
m_budget -= n;
|
|
|
|
// If budget is still >= 0 then it's safe to use the allowance right now.
|
2021-03-11 02:06:03 +08:00
|
|
|
if (m_budget >= 0)
|
2017-05-26 04:48:44 +08:00
|
|
|
return Void();
|
|
|
|
// Otherise return the amount of time it will take for the budget to rise to 0.
|
2021-01-24 17:50:09 +08:00
|
|
|
return m_stop.getFuture() || delay(m_seconds * -m_budget / m_limit);
|
2017-05-26 04:48:44 +08:00
|
|
|
}
|
|
|
|
|
2020-10-08 10:55:05 +08:00
|
|
|
void returnUnused(int n) override {
|
2021-03-11 02:06:03 +08:00
|
|
|
if (n < 0)
|
2017-05-26 04:48:44 +08:00
|
|
|
return;
|
2020-12-09 08:26:09 +08:00
|
|
|
m_budget = std::min<int64_t>(m_budget + n, m_limit);
|
2017-05-26 04:48:44 +08:00
|
|
|
}
|
|
|
|
|
2021-01-24 17:50:09 +08:00
|
|
|
void wakeWaiters() override {
|
|
|
|
Promise<Void> p;
|
|
|
|
p.swap(m_stop);
|
|
|
|
p.send(Void());
|
|
|
|
}
|
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
void killWaiters(const Error& e) override {
|
2021-01-24 17:50:09 +08:00
|
|
|
Promise<Void> p;
|
|
|
|
p.swap(m_stop);
|
|
|
|
p.sendError(e);
|
2017-05-26 04:48:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
int m_limit;
|
|
|
|
double m_seconds;
|
|
|
|
double m_last_update;
|
|
|
|
int64_t m_budget;
|
2021-01-24 17:50:09 +08:00
|
|
|
Promise<Void> m_stop;
|
2017-05-26 04:48:44 +08:00
|
|
|
};
|
|
|
|
|
2022-03-13 21:02:11 +08:00
|
|
|
// An IRateControl implementation that enforces no limit
|
2020-10-08 10:55:05 +08:00
|
|
|
class Unlimited final : public IRateControl, ReferenceCounted<Unlimited> {
|
2017-05-26 04:48:44 +08:00
|
|
|
public:
|
|
|
|
Unlimited() {}
|
2021-01-26 09:55:43 +08:00
|
|
|
~Unlimited() override = default;
|
2020-10-08 10:55:05 +08:00
|
|
|
void addref() override { ReferenceCounted<Unlimited>::addref(); }
|
|
|
|
void delref() override { ReferenceCounted<Unlimited>::delref(); }
|
2017-05-26 04:48:44 +08:00
|
|
|
|
2020-10-08 10:55:05 +08:00
|
|
|
Future<Void> getAllowance(unsigned int n) override { return Void(); }
|
|
|
|
void returnUnused(int n) override {}
|
2021-01-24 17:50:09 +08:00
|
|
|
void wakeWaiters() override {}
|
2021-03-11 02:06:03 +08:00
|
|
|
void killWaiters(const Error& e) override {}
|
2017-05-26 04:48:44 +08:00
|
|
|
};
|