Skip to content

Commit

Permalink
Add client ready callback
Browse files Browse the repository at this point in the history
  • Loading branch information
ungive committed Sep 12, 2024
1 parent 3d1402f commit afa59b2
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 3 deletions.
16 changes: 16 additions & 0 deletions client/include/loon/client.h
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,17 @@ class IClient
*/
virtual void wait_until_ready() = 0;

/**
* @brief Sets a callback for when the client is connected and ready.
*
* It is recommended to call this method before start(),
* to reliably detect client ready state.
* Do not call any client methods within the callback,
* as client locks are held when it is called,
* which would cause a deadlock.
*/
virtual void on_ready(std::function<void()> callback) = 0;

/**
* @brief Sets a callback for when the client has unrecoverably failed.
*
Expand Down Expand Up @@ -455,6 +466,11 @@ class Client : public IClient
return m_impl->wait_until_ready(timeout);
}

inline void on_ready(std::function<void()> callback) override
{
return m_impl->on_ready(callback);
}

inline void on_failed(std::function<void()> callback) override
{
return m_impl->on_failed(callback);
Expand Down
11 changes: 8 additions & 3 deletions client/src/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -326,13 +326,18 @@ void ClientImpl::on_hello(Hello const& hello)
}
}

m_cv_connection_ready.notify_all();
log(Info) << "ready" << var("base_url", m_hello->base_url());

// The connection is idling if no content was registered yet.
if (m_content.empty()) {
set_idle(true);
}

log(Info) << "ready" << var("base_url", m_hello->base_url());

// Call the callback before notifying that the connection is ready.
if (m_ready_callback) {
m_ready_callback();
}
m_cv_connection_ready.notify_all();
}

bool ClientImpl::check_request_limit(decltype(m_content)::iterator it)
Expand Down
7 changes: 7 additions & 0 deletions client/src/client.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ class ClientImpl : public IClient
wait_until_ready(lock, timeout);
}

inline void on_ready(std::function<void()> callback) override
{
std::lock_guard<std::mutex> lock(m_mutex);
m_ready_callback = callback;
}

inline void on_failed(std::function<void()> callback) override
{
std::lock_guard<std::mutex> lock(m_mutex);
Expand Down Expand Up @@ -315,6 +321,7 @@ class ClientImpl : public IClient
const ClientOptions m_options{};
std::deque<std::chrono::steady_clock::time_point>
m_no_content_request_history{};
std::function<void()> m_ready_callback{};
std::function<void()> m_failed_callback{};

bool m_started{ false };
Expand Down
9 changes: 9 additions & 0 deletions client/test/client_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,15 @@ TEST(Client, FailsWhenMinCacheDurationIsSetAndServerDoesNotCacheResponses)
EXPECT_THROW(client->wait_for_hello(), ClientNotConnectedException);
}

TEST(Client, ReadyWhenClientIsStarted)
{
auto client = create_client(false);
ExpectCalled callback;
client->on_ready(callback.get());
client->start();
EXPECT_NO_THROW(client->wait_until_ready());
}

TEST(Client, FailsWhenMinCacheDurationIsSetButResponseIsNotCached)
{
uint32_t cache_duration = 10;
Expand Down

0 comments on commit afa59b2

Please sign in to comment.