Freshen streams when we write to them
This commit is contained in:
13
index.cc
13
index.cc
@@ -5,7 +5,7 @@ namespace firesse {
|
||||
void Index::Add(Stream* stream) {
|
||||
std::lock_guard l(mu_);
|
||||
|
||||
// Implicitly freshest
|
||||
stream->last_message_time_ = std::chrono::steady_clock::now();
|
||||
stream->fresher_ = nullptr;
|
||||
stream->staler_ = freshest_;
|
||||
if (stream->staler_) {
|
||||
@@ -36,4 +36,15 @@ void Index::Remove(Stream* stream) {
|
||||
}
|
||||
}
|
||||
|
||||
void Index::Freshen(Stream* stream) {
|
||||
std::lock_guard l(mu_);
|
||||
if (freshest_ == stream) {
|
||||
// Shortcut
|
||||
stream->last_message_time_ = std::chrono::steady_clock::now();
|
||||
return;
|
||||
}
|
||||
Remove(stream);
|
||||
Add(stream);
|
||||
}
|
||||
|
||||
} // namespace firesse
|
||||
|
||||
3
index.h
3
index.h
@@ -9,9 +9,10 @@ class Index {
|
||||
public:
|
||||
void Add(Stream* stream);
|
||||
void Remove(Stream* stream);
|
||||
void Freshen(Stream* stream);
|
||||
|
||||
private:
|
||||
std::mutex mu_;
|
||||
std::recursive_mutex mu_;
|
||||
Stream* freshest_ = nullptr;
|
||||
Stream* stalest_ = nullptr;
|
||||
};
|
||||
|
||||
@@ -22,7 +22,7 @@ void Stream::OnClose(const std::function<void()>& callback) {
|
||||
bool Stream::WriteEvent(const std::string& data, uint64_t id, const std::string& type) {
|
||||
{
|
||||
std::lock_guard l(mu_);
|
||||
last_message_time_ = std::chrono::steady_clock::now();
|
||||
index_->Freshen(this);
|
||||
}
|
||||
|
||||
return request_->InTransaction<bool>([=]() {
|
||||
|
||||
Reference in New Issue
Block a user