| 1 |
/* |
| 2 |
* Copyright (c) Meta Platforms, Inc. and affiliates. |
| 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 |
|
| 17 |
#include <chrono> |
| 18 |
#include <functional> |
| 19 |
#include <memory> |
| 20 |
#include <mutex> |
| 21 |
#include <stdexcept> |
| 22 |
#include <utility> |
| 23 |
|
| 24 |
#include <fmt/chrono.h> |
| 25 |
#include <fmt/core.h> |
| 26 |
#include <glog/logging.h> |
| 27 |
|
| 28 |
#include <folly/DefaultKeepAliveExecutor.h> |
| 29 |
#include <folly/Random.h> |
| 30 |
#include <folly/Synchronized.h> |
| 31 |
#include <folly/executors/GlobalExecutor.h> |
| 32 |
#include <folly/experimental/observer/Observable.h> |
| 33 |
#include <folly/experimental/observer/Observer.h> |
| 34 |
#include <folly/futures/Future.h> |
| 35 |
|
| 36 |
namespace folly { |
| 37 |
namespace observer { |
| 38 |
|
| 39 |
template <typename T> |
| 40 |
Observer<T> withJitter( |
| 41 |
Observer<T> observer, |
| 42 |
std::chrono::milliseconds lag, |
| 43 |
std::chrono::milliseconds jitter) { |
| 44 |
class WithJitterObservable { |
| 45 |
public: |
| 46 |
using element_type = T; |
| 47 |
|
| 48 |
WithJitterObservable( |
| 49 |
Observer<T> observer, |
| 50 |
std::chrono::milliseconds lag, |
| 51 |
std::chrono::milliseconds jitter) |
| 52 |
: observer_(std::move(observer)), |
| 53 |
state_(std::make_shared<Synchronized<State, std::mutex>>( |
| 54 |
State(observer_.getSnapshot().getShared()))), |
| 55 |
lag_(lag), |
| 56 |
jitter_(jitter) {} |
| 57 |
|
| 58 |
std::shared_ptr<const T> get() { return state_->lock()->laggingValue; } |
| 59 |
|
| 60 |
void subscribe(std::function<void()> callback) { |
| 61 |
handle_ = observer_.addCallback([state = state_, |
| 62 |
observer = observer_, |
| 63 |
callback = std::move(callback), |
| 64 |
lag = lag_, |
| 65 |
jitter = jitter_](auto /* snapshot */) { |
| 66 |
if (std::exchange(state->lock()->delayedRefreshPending, true)) { |
| 67 |
return; |
| 68 |
} |
| 69 |
|
| 70 |
const auto sleepFor = lag - jitter + |
| 71 |
std::chrono::milliseconds{Random::rand64(2 * jitter.count())}; |
| 72 |
|
| 73 |
auto* executor = dynamic_cast<DefaultKeepAliveExecutor*>( |
| 74 |
getGlobalCPUExecutor().get()); |
| 75 |
CHECK(executor); |
| 76 |
futures::sleep(sleepFor) |
| 77 |
.via(executor->weakRef()) |
| 78 |
.thenValue([callback, observer, state](auto&&) mutable { |
| 79 |
state->withLock([&](auto& s) { |
| 80 |
s.laggingValue = observer.getSnapshot().getShared(); |
| 81 |
s.delayedRefreshPending = false; |
| 82 |
}); |
| 83 |
callback(); |
| 84 |
}); |
| 85 |
}); |
| 86 |
} |
| 87 |
|
| 88 |
void unsubscribe() { handle_.cancel(); } |
| 89 |
|
| 90 |
private: |
| 91 |
struct State { |
| 92 |
explicit State(std::shared_ptr<const T> value) |
| 93 |
: laggingValue(std::move(value)) {} |
| 94 |
|
| 95 |
std::shared_ptr<const T> laggingValue; |
| 96 |
bool delayedRefreshPending{false}; |
| 97 |
}; |
| 98 |
|
| 99 |
Observer<T> observer_; |
| 100 |
CallbackHandle handle_; |
| 101 |
std::shared_ptr<Synchronized<State, std::mutex>> state_; |
| 102 |
const std::chrono::milliseconds lag_; |
| 103 |
const std::chrono::milliseconds jitter_; |
| 104 |
}; |
| 105 |
|
| 106 |
if (lag == std::chrono::milliseconds::zero()) { |
| 107 |
return observer; |
| 108 |
} |
| 109 |
if (jitter > lag) { |
| 110 |
throw std::invalid_argument( |
| 111 |
fmt::format("lag ({}) cannot be less than jitter ({})", lag, jitter)); |
| 112 |
} |
| 113 |
return ObserverCreator<WithJitterObservable>(std::move(observer), lag, jitter) |
| 114 |
.getObserver(); |
| 115 |
} |
| 116 |
|
| 117 |
} // namespace observer |
| 118 |
} // namespace folly |