Skip to content

Commit 92e9fcd

Browse files
committed
refactor: coalesce cold-start GetStatus poll with std::call_once
1 parent 9f31667 commit 92e9fcd

2 files changed

Lines changed: 13 additions & 4 deletions

File tree

libs/server-sdk/src/data_components/big_segments/big_segment_store_wrapper.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,12 @@ BigSegmentStoreStatus BigSegmentStoreWrapper::GetStatus() {
142142
}
143143
}
144144
// No poll has completed yet; do one now so the caller gets an accurate
145-
// staleness reading rather than a default.
146-
return PollStoreAndUpdateStatus();
145+
// staleness reading rather than a default. call_once coalesces concurrent
146+
// first-callers onto a single store query.
147+
std::call_once(first_poll_once_,
148+
[this] { PollStoreAndUpdateStatus(); });
149+
std::lock_guard lock(status_mutex_);
150+
return *last_status_;
147151
}
148152

149153
BigSegmentStoreStatus BigSegmentStoreWrapper::PollStoreAndUpdateStatus() {

libs/server-sdk/src/data_components/big_segments/big_segment_store_wrapper.hpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,17 +138,22 @@ class BigSegmentStoreWrapper
138138
// Cancels the pending poll delay on destruction, ending the poll loop.
139139
async::CancellationSource poll_cancel_;
140140

141+
// Internally thread-safe.
141142
MembershipCache cache_;
142143

144+
// Broadcasts status changes to listeners registered via OnStatusChange.
145+
boost::signals2::signal<void(BigSegmentStoreStatus)> status_signal_;
146+
147+
// Coalesces the cold-start fallback poll in GetStatus().
148+
std::once_flag first_poll_once_;
149+
143150
// In-flight store queries keyed by context key. Protected by load_mutex_.
144151
std::mutex load_mutex_;
145152
std::unordered_map<std::string, std::shared_ptr<InFlightQuery>> in_flight_;
146153

147154
// Protected by status_mutex_; nullopt until the first poll completes.
148155
std::mutex status_mutex_;
149156
std::optional<BigSegmentStoreStatus> last_status_;
150-
151-
boost::signals2::signal<void(BigSegmentStoreStatus)> status_signal_;
152157
};
153158

154159
} // namespace launchdarkly::server_side::data_components

0 commit comments

Comments
 (0)