Skip to content

Commit 90ea369

Browse files
Depend on the independent Asio instead of Boost.Asio by default (#382)
Fixes #367 ### Motivation See the difference of Asio and Boost.Asio here: https://think-async.com/Asio/AsioAndBoostAsio.html Asio is updated more frequently than Boost.Asio and its release does not need to be synchronous with other Boost components. Depending on the independent Asio could make it easier for a newer Asio release. ### Modifications Import `asio` 1.28.2 as the dependency and remove the `boost-asio` dependency from the vcpkg.json. Since the latest Asio already removed the `deadline_timer`, this patch replaces all `deadline_timer` with `steady_timer`, which uses `std::chrono` rather than Boost.Date_Time component to compute the timeout. Add a `USE_ASIO` CMake option to determine whether Asio or Boost.Asio is depended. For vcpkg users, the option is always enabled. Finally, simplify the vcpkg.json by removing some `boost-*` dependencies depended indirectly by the rest two major dependencies: - boost-accumulators: latency percentiles computation - boost-property-tree: JSON operations These two dependencies are hard to remove for now unless introducing other dependencies so they will be kept from some time.
1 parent f7e493b commit 90ea369

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+441
-427
lines changed

.github/workflows/ci-build-binary-artifacts.yaml

+2
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ jobs:
148148
mkdir -p $BUILD_DIR
149149
cmake -B $BUILD_DIR \
150150
-G "${{ matrix.generator }}" ${{ matrix.arch }} \
151+
-DUSE_ASIO=ON \
151152
-DBUILD_TESTS=OFF \
152153
-DVCPKG_TRIPLET=${{ matrix.triplet }} \
153154
-DCMAKE_INSTALL_PREFIX=${{ env.INSTALL_DIR }} \
@@ -174,6 +175,7 @@ jobs:
174175
mkdir -p $BUILD_DIR
175176
cmake -B $BUILD_DIR \
176177
-G "${{ matrix.generator }}" ${{ matrix.arch }} \
178+
-DUSE_ASIO=ON \
177179
-DBUILD_TESTS=OFF \
178180
-DVCPKG_TRIPLET=${{ matrix.triplet }} \
179181
-DCMAKE_INSTALL_PREFIX=$INSTALL_DIR_DEBUG \

.github/workflows/ci-pr-validation.yaml

+2
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ jobs:
191191
cmake \
192192
-B ./build-1 \
193193
-G "${{ matrix.generator }}" ${{ matrix.arch }} \
194+
-DUSE_ASIO=ON \
194195
-DBUILD_TESTS=OFF \
195196
-DVCPKG_TRIPLET="${{ matrix.triplet }}" \
196197
-DCMAKE_INSTALL_PREFIX="${{ env.INSTALL_DIR }}" \
@@ -232,6 +233,7 @@ jobs:
232233
cmake \
233234
-B ./build-2 \
234235
-G "${{ matrix.generator }}" ${{ matrix.arch }} \
236+
-DUSE_ASIO=ON \
235237
-DBUILD_TESTS=OFF \
236238
-DVCPKG_TRIPLET="${{ matrix.triplet }}" \
237239
-DCMAKE_INSTALL_PREFIX="${{ env.INSTALL_DIR }}" \

CMakeLists.txt

+11
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,11 @@
1919

2020
cmake_minimum_required(VERSION 3.13)
2121

22+
option(USE_ASIO "Use Asio instead of Boost.Asio" OFF)
23+
2224
option(INTEGRATE_VCPKG "Integrate with Vcpkg" OFF)
2325
if (INTEGRATE_VCPKG)
26+
set(USE_ASIO ON)
2427
set(CMAKE_TOOLCHAIN_FILE "${CMAKE_SOURCE_DIR}/vcpkg/scripts/buildsystems/vcpkg.cmake")
2528
endif ()
2629

@@ -129,6 +132,10 @@ if (INTEGRATE_VCPKG)
129132
$<IF:$<TARGET_EXISTS:zstd::libzstd_shared>,zstd::libzstd_shared,zstd::libzstd_static>
130133
Snappy::snappy
131134
)
135+
if (USE_ASIO)
136+
find_package(asio CONFIG REQUIRED)
137+
set(COMMON_LIBS ${COMMON_LIBS} asio::asio)
138+
endif ()
132139
add_definitions(-DHAS_ZSTD -DHAS_SNAPPY)
133140
if (MSVC)
134141
find_package(dlfcn-win32 CONFIG REQUIRED)
@@ -140,6 +147,10 @@ else ()
140147
include(./LegacyFindPackages.cmake)
141148
endif ()
142149

150+
if (USE_ASIO)
151+
add_definitions(-DUSE_ASIO)
152+
endif ()
153+
143154
set(LIB_NAME $ENV{PULSAR_LIBRARY_NAME})
144155
if (NOT LIB_NAME)
145156
set(LIB_NAME pulsar)

LegacyFindPackages.cmake

-4
Original file line numberDiff line numberDiff line change
@@ -176,10 +176,6 @@ if (Boost_MAJOR_VERSION EQUAL 1 AND Boost_MINOR_VERSION LESS 69)
176176
MESSAGE(STATUS "Linking with Boost:System")
177177
endif()
178178

179-
if (MSVC)
180-
set(BOOST_COMPONENTS ${BOOST_COMPONENTS} date_time)
181-
endif()
182-
183179
if (CMAKE_COMPILER_IS_GNUCC AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS 4.9)
184180
# GCC 4.8.2 implementation of std::regex is buggy
185181
set(BOOST_COMPONENTS ${BOOST_COMPONENTS} regex)

lib/AckGroupingTrackerEnabled.cc

+3-3
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ void AckGroupingTrackerEnabled::close() {
117117
this->flush();
118118
std::lock_guard<std::mutex> lock(this->mutexTimer_);
119119
if (this->timer_) {
120-
boost::system::error_code ec;
120+
ASIO_ERROR ec;
121121
this->timer_->cancel(ec);
122122
}
123123
}
@@ -168,9 +168,9 @@ void AckGroupingTrackerEnabled::scheduleTimer() {
168168

169169
std::lock_guard<std::mutex> lock(this->mutexTimer_);
170170
this->timer_ = this->executor_->createDeadlineTimer();
171-
this->timer_->expires_from_now(boost::posix_time::milliseconds(std::max(1L, this->ackGroupingTimeMs_)));
171+
this->timer_->expires_from_now(std::chrono::milliseconds(std::max(1L, this->ackGroupingTimeMs_)));
172172
auto self = shared_from_this();
173-
this->timer_->async_wait([this, self](const boost::system::error_code& ec) -> void {
173+
this->timer_->async_wait([this, self](const ASIO_ERROR& ec) -> void {
174174
if (!ec) {
175175
this->flush();
176176
this->scheduleTimer();

lib/AckGroupingTrackerEnabled.h

+1-2
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,17 @@
2222
#include <pulsar/MessageId.h>
2323

2424
#include <atomic>
25-
#include <boost/asio/deadline_timer.hpp>
2625
#include <cstdint>
2726
#include <mutex>
2827
#include <set>
2928

3029
#include "AckGroupingTracker.h"
30+
#include "AsioTimer.h"
3131

3232
namespace pulsar {
3333

3434
class ClientImpl;
3535
using ClientImplPtr = std::shared_ptr<ClientImpl>;
36-
using DeadlineTimerPtr = std::shared_ptr<boost::asio::deadline_timer>;
3736
class ExecutorService;
3837
using ExecutorServicePtr = std::shared_ptr<ExecutorService>;
3938
class HandlerBase;

lib/AsioDefines.h

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
// This header defines common macros to use Asio or Boost.Asio.
20+
#pragma once
21+
22+
#ifdef USE_ASIO
23+
#define ASIO ::asio
24+
#define ASIO_ERROR asio::error_code
25+
#define ASIO_SUCCESS (ASIO_ERROR{})
26+
#define ASIO_SYSTEM_ERROR asio::system_error
27+
#else
28+
#define ASIO boost::asio
29+
#define ASIO_ERROR boost::system::error_code
30+
#define ASIO_SUCCESS boost::system::errc::make_error_code(boost::system::errc::success)
31+
#define ASIO_SYSTEM_ERROR boost::system::system_error
32+
#endif

lib/TimeUtils.cc renamed to lib/AsioTimer.h

+9-10
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,16 @@
1616
* specific language governing permissions and limitations
1717
* under the License.
1818
*/
19+
#pragma once
1920

20-
#include "TimeUtils.h"
21+
#ifdef USE_ASIO
22+
#include <asio/steady_timer.hpp>
23+
#else
24+
#include <boost/asio/steady_timer.hpp>
25+
#endif
2126

22-
namespace pulsar {
27+
#include <memory>
2328

24-
ptime TimeUtils::now() { return microsec_clock::universal_time(); }
29+
#include "AsioDefines.h"
2530

26-
int64_t TimeUtils::currentTimeMillis() {
27-
static ptime time_t_epoch(boost::gregorian::date(1970, 1, 1));
28-
29-
time_duration diff = now() - time_t_epoch;
30-
return diff.total_milliseconds();
31-
}
32-
} // namespace pulsar
31+
using DeadlineTimerPtr = std::shared_ptr<ASIO::steady_timer>;

lib/Backoff.cc

+5-2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
#include <time.h> /* time */
2222

2323
#include <algorithm>
24+
#include <chrono>
25+
26+
#include "TimeUtils.h"
2427

2528
namespace pulsar {
2629

@@ -33,8 +36,8 @@ TimeDuration Backoff::next() {
3336

3437
// Check for mandatory stop
3538
if (!mandatoryStopMade_) {
36-
const boost::posix_time::ptime& now = boost::posix_time::microsec_clock::universal_time();
37-
TimeDuration timeElapsedSinceFirstBackoff = boost::posix_time::milliseconds(0);
39+
auto now = TimeUtils::now();
40+
TimeDuration timeElapsedSinceFirstBackoff = std::chrono::nanoseconds(0);
3841
if (initial_ == current) {
3942
firstBackoffTime_ = now;
4043
} else {

lib/Backoff.h

+4-4
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@
2020
#define _PULSAR_BACKOFF_HEADER_
2121
#include <pulsar/defines.h>
2222

23-
#include <boost/date_time/posix_time/posix_time.hpp>
23+
#include <chrono>
2424
#include <random>
2525

26-
namespace pulsar {
26+
#include "TimeUtils.h"
2727

28-
using TimeDuration = boost::posix_time::time_duration;
28+
namespace pulsar {
2929

3030
class PULSAR_PUBLIC Backoff {
3131
public:
@@ -38,7 +38,7 @@ class PULSAR_PUBLIC Backoff {
3838
const TimeDuration max_;
3939
TimeDuration next_;
4040
TimeDuration mandatoryStop_;
41-
boost::posix_time::ptime firstBackoffTime_;
41+
decltype(std::chrono::high_resolution_clock::now()) firstBackoffTime_;
4242
std::mt19937 rng_;
4343
bool mandatoryStopMade_ = false;
4444

0 commit comments

Comments
 (0)