-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support providing custom rate-limiters
Summary: As title, I also noticed there were no unit tests for RateLimiter, so I added some for MaxConcurrentRateLimiter Reviewed By: Gownta Differential Revision: D50399284 fbshipit-source-id: 2477aed15e54582cbd798ae84a421bc05bb5a16e
- Loading branch information
1 parent
fa07253
commit ced26fe
Showing
10 changed files
with
293 additions
and
131 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/* | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* 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 | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* 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. | ||
*/ | ||
|
||
#include <folly/experimental/channels/MaxConcurrentRateLimiter.h> | ||
|
||
namespace folly { | ||
namespace channels { | ||
|
||
class MaxConcurrentRateLimiter::Token : public RateLimiter::Token { | ||
public: | ||
explicit Token( | ||
std::shared_ptr<MaxConcurrentRateLimiter> maxConcurrentRateLimiter) | ||
: maxConcurrentRateLimiter_{std::move(maxConcurrentRateLimiter)} {} | ||
|
||
Token(Token&&) = default; | ||
Token& operator=(Token&&) = default; | ||
Token(const Token&) = delete; | ||
Token& operator=(const Token&) = delete; | ||
|
||
~Token() override { | ||
if (maxConcurrentRateLimiter_) { | ||
maxConcurrentRateLimiter_->release(); | ||
} | ||
} | ||
|
||
private: | ||
std::shared_ptr<MaxConcurrentRateLimiter> maxConcurrentRateLimiter_; | ||
}; | ||
|
||
std::shared_ptr<MaxConcurrentRateLimiter> MaxConcurrentRateLimiter::create( | ||
size_t maxConcurrent) { | ||
return std::shared_ptr<MaxConcurrentRateLimiter>( | ||
new MaxConcurrentRateLimiter(maxConcurrent)); | ||
} | ||
|
||
MaxConcurrentRateLimiter::MaxConcurrentRateLimiter(size_t maxConcurrent) | ||
: maxConcurrent_(maxConcurrent) {} | ||
|
||
void MaxConcurrentRateLimiter::executeWhenReady( | ||
folly::Function<void(std::unique_ptr<RateLimiter::Token>)> func, | ||
Executor::KeepAlive<SequencedExecutor> executor) { | ||
auto state = state_.wlock(); | ||
if (state->running < maxConcurrent_) { | ||
CHECK(state->queue.empty()); | ||
state->running++; | ||
executor->add( | ||
[func = std::move(func), | ||
token = std::make_unique<MaxConcurrentRateLimiter::Token>( | ||
std::static_pointer_cast<MaxConcurrentRateLimiter>( | ||
shared_from_this()))]() mutable { func(std::move(token)); }); | ||
} else { | ||
state->queue.enqueue(QueueItem{std::move(func), std::move(executor)}); | ||
} | ||
} | ||
|
||
void MaxConcurrentRateLimiter::release() { | ||
auto state = state_.wlock(); | ||
if (!state->queue.empty()) { | ||
auto queueItem = state->queue.dequeue(); | ||
queueItem.executor->add( | ||
[func = std::move(queueItem.func), | ||
token = std::make_unique<MaxConcurrentRateLimiter::Token>( | ||
std::static_pointer_cast<MaxConcurrentRateLimiter>( | ||
shared_from_this()))]() mutable { func(std::move(token)); }); | ||
} else { | ||
state->running--; | ||
} | ||
} | ||
|
||
} // namespace channels | ||
} // namespace folly |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* 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 | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* 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 <folly/Synchronized.h> | ||
#include <folly/concurrency/UnboundedQueue.h> | ||
#include <folly/executors/SequencedExecutor.h> | ||
#include <folly/experimental/channels/RateLimiter.h> | ||
|
||
namespace folly { | ||
namespace channels { | ||
|
||
class MaxConcurrentRateLimiter : public RateLimiter { | ||
public: | ||
static std::shared_ptr<MaxConcurrentRateLimiter> create(size_t maxConcurrent); | ||
|
||
void executeWhenReady( | ||
folly::Function<void(std::unique_ptr<Token>)> func, | ||
Executor::KeepAlive<SequencedExecutor> executor) override; | ||
|
||
private: | ||
class Token; | ||
friend class Token; | ||
|
||
explicit MaxConcurrentRateLimiter(size_t maxConcurrent); | ||
void release(); | ||
|
||
struct QueueItem { | ||
folly::Function<void(std::unique_ptr<Token>)> func; | ||
Executor::KeepAlive<SequencedExecutor> executor; | ||
}; | ||
|
||
struct State { | ||
USPSCQueue<QueueItem, false /* MayBlock */, 6 /* LgSegmentSize */> queue; | ||
size_t running{0}; | ||
}; | ||
|
||
const size_t maxConcurrent_; | ||
folly::Synchronized<State> state_; | ||
}; | ||
} // namespace channels | ||
} // namespace folly |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.