2018-12-31 20:04:05 +01:00
|
|
|
//
|
2018-12-31 23:02:34 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2019
|
2018-12-31 20:04:05 +01:00
|
|
|
//
|
|
|
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
//
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "td/actor/actor.h"
|
|
|
|
|
2019-02-12 21:48:16 +01:00
|
|
|
#include "td/utils/common.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/Heap.h"
|
2018-08-02 12:22:05 +02:00
|
|
|
#include "td/utils/Slice.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/Time.h"
|
|
|
|
|
|
|
|
#include <set>
|
|
|
|
|
|
|
|
namespace td {
|
2018-07-03 19:28:00 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
class Timeout final : public Actor {
|
|
|
|
public:
|
|
|
|
using Data = void *;
|
|
|
|
using Callback = void (*)(Data);
|
|
|
|
Timeout() {
|
|
|
|
register_actor("Timeout", this).release();
|
|
|
|
}
|
|
|
|
|
|
|
|
void set_callback(Callback callback) {
|
|
|
|
callback_ = callback;
|
|
|
|
}
|
|
|
|
void set_callback_data(Data &&data) {
|
|
|
|
data_ = data;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool has_timeout() const {
|
|
|
|
return Actor::has_timeout();
|
|
|
|
}
|
|
|
|
void set_timeout_in(double timeout) {
|
|
|
|
Actor::set_timeout_in(timeout);
|
|
|
|
}
|
2019-12-17 17:17:57 +01:00
|
|
|
void set_timeout_at(double timeout) {
|
|
|
|
Actor::set_timeout_at(timeout);
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
void cancel_timeout() {
|
|
|
|
if (has_timeout()) {
|
|
|
|
Actor::cancel_timeout();
|
|
|
|
callback_ = Callback();
|
|
|
|
data_ = Data();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
friend class Scheduler;
|
|
|
|
|
2018-10-26 16:11:20 +02:00
|
|
|
Callback callback_{};
|
|
|
|
Data data_{};
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
void timeout_expired() override {
|
|
|
|
CHECK(!has_timeout());
|
|
|
|
CHECK(callback_ != Callback());
|
|
|
|
Callback callback = callback_;
|
|
|
|
Data data = data_;
|
|
|
|
callback_ = Callback();
|
|
|
|
data_ = Data();
|
|
|
|
|
|
|
|
callback(data);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// TODO optimize
|
|
|
|
class MultiTimeout final : public Actor {
|
|
|
|
struct Item : public HeapNode {
|
|
|
|
int64 key;
|
|
|
|
|
|
|
|
explicit Item(int64 key) : key(key) {
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator<(const Item &other) const {
|
|
|
|
return key < other.key;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
public:
|
|
|
|
using Data = void *;
|
|
|
|
using Callback = void (*)(Data, int64);
|
2018-08-01 19:31:20 +02:00
|
|
|
explicit MultiTimeout(Slice name) {
|
|
|
|
register_actor(name, this).release();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void set_callback(Callback callback) {
|
|
|
|
callback_ = callback;
|
|
|
|
}
|
|
|
|
void set_callback_data(Data data) {
|
|
|
|
data_ = data;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool has_timeout(int64 key) const;
|
|
|
|
|
|
|
|
void set_timeout_in(int64 key, double timeout) {
|
|
|
|
set_timeout_at(key, Time::now() + timeout);
|
|
|
|
}
|
|
|
|
|
|
|
|
void add_timeout_in(int64 key, double timeout) {
|
|
|
|
add_timeout_at(key, Time::now() + timeout);
|
|
|
|
}
|
|
|
|
|
|
|
|
void set_timeout_at(int64 key, double timeout);
|
|
|
|
|
|
|
|
void add_timeout_at(int64 key, double timeout); // memcache semantics, doesn't replace old timeout
|
|
|
|
|
|
|
|
void cancel_timeout(int64 key);
|
|
|
|
|
2018-11-27 02:10:52 +01:00
|
|
|
void run_all();
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
private:
|
|
|
|
friend class Scheduler;
|
|
|
|
|
|
|
|
Callback callback_;
|
|
|
|
Data data_;
|
|
|
|
|
|
|
|
KHeap<double> timeout_queue_;
|
|
|
|
std::set<Item> items_;
|
|
|
|
|
|
|
|
void update_timeout();
|
|
|
|
|
|
|
|
void timeout_expired() override;
|
2018-11-27 02:10:52 +01:00
|
|
|
|
|
|
|
vector<int64> get_expired_keys(double now);
|
2018-12-31 20:04:05 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace td
|