-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Description
Hi guys,
I have been using the SSE for a while now exactly as describe in the example/ssesvr.cc
file.
I noticed that my main thread hung / stale sporadically, after a long and painful investigation, it looks like the send_event()
method, can hang for some reason.
My use case is: I always have a least one listener waiting for an event, and I send about 1 event per second. I can easily see a couple of successive 5s hang here and there in a 24h time period (meaning the send_event()
method take 5s ish to get back.
Not familiar with wait conditions etc, but do you guys know what could be done to avoid any locking like that? Not even sure what the root cause can be. Can the sink->write
be super long on network failure and lock the entire thing ? Any help is appreciated!
Here is a reminder of the SSE implementation :
class EventDispatcher {
public:
EventDispatcher() {
}
void wait_event(DataSink *sink) {
unique_lock<mutex> lk(m_);
int id = id_;
cv_.wait(lk, [&] { return cid_ == id; });
if (sink->is_writable()) { sink->write(message_.data(), message_.size()); }
}
void send_event(const string &message) {
lock_guard<mutex> lk(m_);
cid_ = id_++;
message_ = message;
cv_.notify_all();
}
private:
mutex m_;
condition_variable cv_;
atomic_int id_{0};
atomic_int cid_{-1};
string message_;
};