Skip to content

fix: stream connections longer than 5 minutes are dropped #244

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Sep 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions contract-tests/sse-contract-tests/include/event_outbox.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,11 @@ class EventOutbox : public std::enable_shared_from_this<EventOutbox> {
private:
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lints.

RequestType build_request(
std::size_t counter,
std::variant<launchdarkly::sse::Event, launchdarkly::sse::Error> ev);
std::variant<launchdarkly::sse::Event, launchdarkly::sse::Error> event);
void on_resolve(beast::error_code ec, tcp::resolver::results_type results);
void on_connect(beast::error_code ec,
tcp::resolver::results_type::endpoint_type);
void on_flush_timer(boost::system::error_code ec);
void on_write(beast::error_code ec, std::size_t);
void do_shutdown(beast::error_code ec, std::string what);
void do_shutdown(beast::error_code ec);
};
18 changes: 12 additions & 6 deletions contract-tests/sse-contract-tests/src/entity_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,30 @@ std::optional<std::string> EntityManager::create(ConfigParams const& params) {
}

if (params.body) {
client_builder.body(std::move(*params.body));
client_builder.body(*params.body);
}

if (params.readTimeoutMs) {
client_builder.read_timeout(
std::chrono::milliseconds(*params.readTimeoutMs));
}

Copy link
Contributor Author

@cwaldren-ld cwaldren-ld Sep 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This builder option was missing, but it's already implemented. Easy win to speed up tests.

if (params.initialDelayMs) {
client_builder.initial_reconnect_delay(
std::chrono::milliseconds(*params.initialDelayMs));
}

client_builder.logger([this](std::string msg) {
LD_LOG(logger_, LogLevel::kDebug) << std::move(msg);
});

client_builder.receiver([copy = poster](launchdarkly::sse::Event e) {
copy->post_event(std::move(e));
client_builder.receiver([copy = poster](launchdarkly::sse::Event event) {
copy->post_event(std::move(event));
});

client_builder.errors(
[copy = poster](launchdarkly::sse::Error e) { copy->post_error(e); });
client_builder.errors([copy = poster](launchdarkly::sse::Error event) {
copy->post_error(event);
});

auto client = client_builder.build();
if (!client) {
Expand All @@ -53,7 +59,7 @@ std::optional<std::string> EntityManager::create(ConfigParams const& params) {
return std::nullopt;
}

client->run();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Renamed run to async_connect to make it clear this is kicking off an asynchronous operation.

client->async_connect();

entities_.emplace(id, std::make_pair(client, poster));
return id;
Expand Down
26 changes: 12 additions & 14 deletions contract-tests/sse-contract-tests/src/event_outbox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ auto const kOutboxCapacity = 1023;
EventOutbox::EventOutbox(net::any_io_executor executor,
std::string callback_url)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lints.

: callback_url_{std::move(callback_url)},
callback_port_{},
callback_host_{},
callback_counter_{0},
executor_{executor},
resolver_{executor},
Expand All @@ -29,7 +27,7 @@ EventOutbox::EventOutbox(net::any_io_executor executor,
callback_port_ = uri_components->port();
}

void EventOutbox::do_shutdown(beast::error_code ec, std::string what) {
void EventOutbox::do_shutdown(beast::error_code ec) {
event_stream_.socket().shutdown(tcp::socket::shutdown_both, ec);
flush_timer_.cancel();
}
Expand All @@ -54,20 +52,18 @@ void EventOutbox::run() {

void EventOutbox::stop() {
beast::error_code ec = net::error::basic_errors::operation_aborted;
std::string reason = "stop";
shutdown_ = true;
net::post(executor_,
beast::bind_front_handler(&EventOutbox::do_shutdown,
shared_from_this(), ec, reason));
net::post(executor_, beast::bind_front_handler(&EventOutbox::do_shutdown,
shared_from_this(), ec));
}

EventOutbox::RequestType EventOutbox::build_request(
std::size_t counter,
std::variant<launchdarkly::sse::Event, launchdarkly::sse::Error> ev) {
std::variant<launchdarkly::sse::Event, launchdarkly::sse::Error> event) {
RequestType req;

req.set(http::field::host, callback_host_);
req.method(http::verb::get);
req.method(http::verb::post);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

get was just plain wrong, but accepted by the contract tests.

req.target(callback_url_ + "/" + std::to_string(counter));

nlohmann::json json;
Expand All @@ -93,13 +89,15 @@ EventOutbox::RequestType EventOutbox::build_request(
break;
case Error::UnrecoverableClientError:
msg.comment = "unrecoverable client error";
case Error::ReadTimeout:
msg.comment = "read timeout";
default:
msg.comment = "unspecified error";
}
json = msg;
}
},
std::move(ev));
std::move(event));

req.body() = json.dump();
req.prepare_payload();
Expand All @@ -109,7 +107,7 @@ EventOutbox::RequestType EventOutbox::build_request(
void EventOutbox::on_resolve(beast::error_code ec,
tcp::resolver::results_type results) {
if (ec) {
return do_shutdown(ec, "resolve");
return do_shutdown(ec);
}

beast::get_lowest_layer(event_stream_)
Expand All @@ -121,7 +119,7 @@ void EventOutbox::on_resolve(beast::error_code ec,
void EventOutbox::on_connect(beast::error_code ec,
tcp::resolver::results_type::endpoint_type) {
if (ec) {
return do_shutdown(ec, "connect");
return do_shutdown(ec);
}

boost::system::error_code dummy;
Expand All @@ -131,7 +129,7 @@ void EventOutbox::on_connect(beast::error_code ec,

void EventOutbox::on_flush_timer(boost::system::error_code ec) {
if (ec && shutdown_) {
return do_shutdown(ec, "flush");
return do_shutdown(ec);
}

if (!outbox_.empty()) {
Expand All @@ -154,7 +152,7 @@ void EventOutbox::on_flush_timer(boost::system::error_code ec) {

void EventOutbox::on_write(beast::error_code ec, std::size_t) {
if (ec) {
return do_shutdown(ec, "write");
return do_shutdown(ec);
}
outbox_.pop();
on_flush_timer(boost::system::error_code{});
Expand Down
3 changes: 2 additions & 1 deletion contract-tests/sse-contract-tests/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ int main(int argc, char* argv[]) {
launchdarkly::Logger logger{
std::make_unique<ConsoleBackend>("sse-contract-tests")};

const std::string default_port = "8123";
std::string const default_port = "8123";
std::string port = default_port;
if (argc == 2) {
port =
Expand All @@ -38,6 +38,7 @@ int main(int argc, char* argv[]) {
srv.add_capability("report");
srv.add_capability("post");
srv.add_capability("reconnection");
srv.add_capability("read-timeout");
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We were already passing the parameter along, just hadn't advertised the capability.


net::signal_set signals{ioc, SIGINT, SIGTERM};

Expand Down
6 changes: 4 additions & 2 deletions libs/client-sdk/src/data_sources/streaming_data_source.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ static char const* DataSourceErrorToString(launchdarkly::sse::Error error) {
return "server responded with an invalid redirection";
case sse::Error::UnrecoverableClientError:
return "unrecoverable client-side error";
case sse::Error::ReadTimeout:
return "read timeout reached";
}
}

Expand Down Expand Up @@ -138,7 +140,7 @@ void StreamingDataSource::Start() {

client_builder.logger([weak_self](auto msg) {
if (auto self = weak_self.lock()) {
LD_LOG(self->logger_, LogLevel::kDebug) << msg;
LD_LOG(self->logger_, LogLevel::kDebug) << "sse-client: " << msg;
}
});

Expand All @@ -163,7 +165,7 @@ void StreamingDataSource::Start() {
kCouldNotParseEndpoint);
return;
}
client_->run();
client_->async_connect();
}

void StreamingDataSource::ShutdownAsync(std::function<void()> completion) {
Expand Down
4 changes: 3 additions & 1 deletion libs/common/src/config/logging_builder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ LoggingBuilder::BasicLogging& LoggingBuilder::BasicLogging::Tag(
return *this;
}
LoggingBuilder::BasicLogging::BasicLogging()
: level_(Defaults<AnySDK>::LogLevel()), tag_(Defaults<AnySDK>::LogTag()) {}
: level_(GetLogLevelEnum(std::getenv("LD_LOG_LEVEL"),
Defaults<AnySDK>::LogLevel())),
tag_(Defaults<AnySDK>::LogTag()) {}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally, we can just run LD_LOG_LEVEL=debug ./binary but the Console Logger constructed in the client passes in a default value, so the environment variable isn't inspected.

Now it works like you'd want.


LoggingBuilder::CustomLogging& LoggingBuilder::CustomLogging::Backend(
std::shared_ptr<ILogBackend> backend) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ class Builder {
class Client {
public:
virtual ~Client() = default;
virtual void run() = 0;
virtual void async_connect() = 0;
virtual void async_shutdown(std::function<void()> completion) = 0;
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ enum class Error {
NoContent = 1,
InvalidRedirectLocation = 2,
UnrecoverableClientError = 3,
ReadTimeout = 4,
};
}
Loading