forked from apache/pulsar
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[C++][Python] Add connection timeout configuration (apache#11029)
Fixes apache#10747 ### Motivation This PR is a catchup of apache#2852 and adds connection timeout configuration to C++ and Python client. ### Modifications - Add a `PeriodicTask` class to execute tasks periodically and the relate unit tests: `PeriodicTastTest`. - Use `PeriodicTask` to register a timer before connecting to broker asynchronously, if the connection was not established when the timer is triggered, close the socket so that `handleTcpConnected` can be triggered immediately with a failure. - Add connection timeout (in milliseconds) to both C++ and Python clients. - Add `ClientTest.testConnectTimeout` (C++) and `test_connect_timeout` (Python) and to verify the connection timeout works. ### Verifying this change - [ ] Make sure that the change passes the CI checks. This change added tests and can be verified as follows: - PeriodicTaskTest - ClientTest.testConnectTimeout - test_connect_timeout
- Loading branch information
1 parent
cadf59d
commit 6062d2f
Showing
14 changed files
with
322 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/** | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
#include "lib/PeriodicTask.h" | ||
#include <boost/date_time/posix_time/posix_time.hpp> | ||
|
||
namespace pulsar { | ||
|
||
void PeriodicTask::start() { | ||
if (state_ != Pending) { | ||
return; | ||
} | ||
state_ = Ready; | ||
if (periodMs_ >= 0) { | ||
auto self = shared_from_this(); | ||
timer_.expires_from_now(boost::posix_time::millisec(periodMs_)); | ||
timer_.async_wait([this, self](const ErrorCode& ec) { handleTimeout(ec); }); | ||
} | ||
} | ||
|
||
void PeriodicTask::stop() { | ||
State state = Ready; | ||
if (!state_.compare_exchange_strong(state, Closing)) { | ||
return; | ||
} | ||
timer_.cancel(); | ||
state_ = Pending; | ||
} | ||
|
||
void PeriodicTask::handleTimeout(const ErrorCode& ec) { | ||
if (state_ != Ready) { | ||
return; | ||
} | ||
|
||
callback_(ec); | ||
|
||
// state_ may be changed in handleTimeout, so we check state_ again | ||
if (state_ == Ready) { | ||
auto self = shared_from_this(); | ||
timer_.expires_from_now(boost::posix_time::millisec(periodMs_)); | ||
timer_.async_wait([this, self](const ErrorCode& ec) { handleTimeout(ec); }); | ||
} | ||
} | ||
|
||
} // namespace pulsar |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/** | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
#pragma once | ||
|
||
#include <atomic> | ||
#include <cstdint> | ||
#include <functional> | ||
#include <memory> | ||
|
||
#include <boost/asio.hpp> | ||
|
||
namespace pulsar { | ||
|
||
/** | ||
* A task that is executed periodically. | ||
* | ||
* After the `start()` method is called, it will trigger `callback_` method periodically whose interval is | ||
* `periodMs` in the constructor. After the `stop()` method is called, the timer will be cancelled and | ||
* `callback()` will never be called again unless `start()` was called again. | ||
* | ||
* If you don't want to execute the task infinitely, you can call `stop()` in the implementation of | ||
* `callback()` method. | ||
* | ||
* NOTE: If the `periodMs` is negative, the `callback()` will never be called. | ||
*/ | ||
class PeriodicTask : public std::enable_shared_from_this<PeriodicTask> { | ||
public: | ||
using ErrorCode = boost::system::error_code; | ||
using CallbackType = std::function<void(const ErrorCode&)>; | ||
|
||
enum State : std::uint8_t | ||
{ | ||
Pending, | ||
Ready, | ||
Closing | ||
}; | ||
|
||
PeriodicTask(boost::asio::io_service& ioService, int periodMs) : timer_(ioService), periodMs_(periodMs) {} | ||
|
||
void start(); | ||
|
||
void stop(); | ||
|
||
void setCallback(CallbackType callback) noexcept { callback_ = callback; } | ||
|
||
State getState() const noexcept { return state_; } | ||
int getPeriodMs() const noexcept { return periodMs_; } | ||
|
||
private: | ||
std::atomic<State> state_{Pending}; | ||
boost::asio::deadline_timer timer_; | ||
const int periodMs_; | ||
CallbackType callback_{trivialCallback}; | ||
|
||
void handleTimeout(const ErrorCode& ec); | ||
|
||
static void trivialCallback(const ErrorCode&) {} | ||
}; | ||
|
||
} // namespace pulsar |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.